Skip to content

Commit 1c0e9ff

Browse files
author
[Arusey]
committed
CON-72-story(notifications): admin receives notification when device is not seen in a while
- setup queue manager for when notifications are sent - implement notifications when device is not seen in a while [Delivers CON-72]
1 parent 4314f15 commit 1c0e9ff

File tree

9 files changed

+99
-13
lines changed

9 files changed

+99
-13
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from flask_socketio import send, emit
2+
from admin_notifications.models import AdminNotification
3+
from manage import socketio
4+
import celery
5+
6+
7+
@celery.task(name="create-notification")
8+
def create_notification(title, message, location_id):
9+
notification = AdminNotification(
10+
title=title,
11+
message=message,
12+
location_id=location_id,
13+
status="unread"
14+
)
15+
notification.save()
16+
new_notification = {"title": title, "message": message}
17+
return socketio.emit('notification', {'notification': new_notification}, broadcast=True) # noqa 501
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def event_auto_cancelled_notification(event_name, room_name):
2+
return {
3+
"title": "Event Auto cancelled.",
4+
"message": f"An event {event_name} in {room_name} \
5+
has been auto cancelled."
6+
}
Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
from celery.schedules import crontab
2-
from cworker import celery_scheduler
3-
4-
celery_scheduler.conf.beat_schedule = {
1+
from datetime import timedelta
2+
beat_schedule = {
53
'run-check-device-last-seen-hourly': {
6-
'task': 'check_device_last_seen',
7-
'schedule': crontab(minute='*')
4+
'task': 'check-device-last-seen',
5+
'schedule': timedelta(seconds=5)
86
}
97
}

admin_notifications/socket_handler.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from flask_socketio import send
2+
from admin_notifications.models import AdminNotification
3+
4+
5+
def serialize_message(notification):
6+
return {
7+
"title": notification.title,
8+
"message": notification.message,
9+
}
10+
11+
12+
def send_notifications():
13+
query = AdminNotification.query
14+
notifications = query.filter_by(status="unread").all()
15+
notifications = [serialize_message(notification)
16+
for notification in notifications]
17+
return send(notifications, broadcast=True)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""add activity column to devices table
2+
3+
Revision ID: 1b53553ddacf
4+
Revises: c65718cd4ed8
5+
Create Date: 2019-06-22 12:59:24.591339
6+
7+
"""
8+
from alembic import op
9+
import sqlalchemy as sa
10+
from sqlalchemy.dialects import postgresql
11+
12+
13+
# revision identifiers, used by Alembic.
14+
revision = '1b53553ddacf'
15+
down_revision = 'c65718cd4ed8'
16+
branch_labels = None
17+
depends_on = None
18+
19+
20+
def upgrade():
21+
# ### commands auto generated by Alembic - please adjust! ###
22+
activitytype = postgresql.ENUM('online', 'offline', name='activitytype')
23+
activitytype.create(op.get_bind())
24+
op.add_column('devices', sa.Column('activity', sa.Enum(
25+
'online', 'offline', name='activitytype'), nullable=True))
26+
27+
# ### end Alembic commands ###
28+
29+
30+
def downgrade():
31+
# ### commands auto generated by Alembic - please adjust! ###
32+
op.drop_column('devices', 'activity')
33+
op.execute("DROP TYPE activitytype;")
34+
# ### end Alembic commands ###

cworker.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,26 @@
11
import os
2-
32
from celery import Celery
43
from app import create_app
5-
4+
from admin_notifications.helpers.queue_manager import beat_schedule
65

76
app = create_app(os.getenv('APP_SETTINGS') or 'default')
87
app.app_context().push()
98

109

10+
app.config.update(
11+
CELERY_BROKER_URL='redis://localhost:6379',
12+
CELERY_RESULT_BACKEND='redis://localhost:6379',
13+
CELERY_ACCEPT_CONTENT=['pickle'],
14+
CELERYBEAT_SCHEDULE=beat_schedule
15+
)
16+
17+
1118
def make_celery(app):
12-
celery = Celery(app.name, broker=app.config['CELERY_BROKER_URL'])
19+
celery = Celery(app.name, broker=app.config['CELERY_BROKER_URL'], include=['admin_notifications.helpers.device_last_seen', 'admin_notifications.helpers.create_notification'], # noqa 501
20+
backend=app.config['CELERY_BROKER_URL'])
21+
1322
celery.conf.update(app.config)
23+
celery.conf.enable_utc = False
1424
TaskBase = celery.Task
1525

1626
class ContextTask(TaskBase):

helpers/email/email_setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def __init__(
3030
html=self.template,
3131
sender=self.sender)
3232

33-
@celery.task
33+
@celery.task(name='asynchronous-email-notifications')
3434
def send_async_email(msg_dict):
3535
mail = Mail()
3636
msg = Message()

manage.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
import bugsnag
44
from flask_script import Manager, Shell
55
from bugsnag.flask import handle_exceptions
6+
from flask_socketio import SocketIO
67

78

89
# Configure bugnsag
910
bugsnag.configure(
10-
api_key=os.getenv('BUGSNAG_API_TOKEN'),
11-
release_stage="development",
12-
project_root="app"
11+
api_key=os.getenv('BUGSNAG_API_TOKEN'),
12+
release_stage="development",
13+
project_root="app"
1314
)
1415

1516
# local imports
@@ -18,6 +19,7 @@
1819
app = create_app(os.getenv('APP_SETTINGS') or 'default')
1920
handle_exceptions(app)
2021
manager = Manager(app)
22+
socketio = SocketIO(app)
2123

2224

2325
def make_shell_context():
@@ -30,4 +32,5 @@ def make_shell_context():
3032

3133

3234
if __name__ == '__main__':
35+
socketio.run(app, debug=True)
3336
manager.run()

utilities/utility.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ class QuestionType(enum.Enum):
8989
missingitem = "missing_items"
9090

9191

92+
9293
class StatusType(enum.Enum):
9394
read = "read"
9495
unread = "unread"

0 commit comments

Comments
 (0)