def handle_notification(task_id, event): # alias for all complete status if event in TaskStatus.complete(): event = "ended" # exit early if not a triggering event if event not in GlobalNotifications.events: return task = Tasks().find_one({"_id": task_id}) or RequestedTasks().find_one( {"_id": task_id}) if not task: return # serialize/unserialize task so we use a safe version from now-on task = json.loads(json.dumps(task, cls=Encoder)) global_notifs = GlobalNotifications.entries.get(event, {}) task_notifs = task.get("notification", {}).get(event, {}) # exit early if we don't have notification requests for the event if not global_notifs and not task_notifs: return for method, recipients in list(task_notifs.items()) + list( global_notifs.items()): func = { "mailgun": handle_mailgun_notification, "webhook": handle_webhook_notification, "slack": handle_slack_notification, }.get(method) if func and recipients: func(task, recipients)
def task_canceled_event_handler(task_id, payload): logger.info(f"Task Cancelled: {task_id}") # if canceled event carries a `canceled_by` and we have none on the task # then store it, otherwise keep what's in the task (manual request) canceled_by = None task = Tasks().find_one({"_id": task_id}, {"canceled_by": 1}) if payload.get("canceled_by") and task and not task.get("canceled_by"): canceled_by = payload.get("canceled_by") save_event( task_id, TaskStatus.canceled, get_timestamp_from_event(payload), task_log=payload.get("log"), canceled_by=canceled_by, )