def test_subscription(self): from celery_pubsub import publish, subscribe res = publish('dummy', 4, 8, a15=16, a23=42).get() self.assertListEqual(sorted(res), sorted(['e'])) subscribe('dummy', job_c) res = publish('dummy', 4, 8, a15=16, a23=42).get() self.assertListEqual(sorted(res), sorted(['e', 'c'])) celery_pubsub.unsubscribe('dummy', job_c) res = publish('dummy', 4, 8, a15=16, a23=42).get() self.assertListEqual(sorted(res), sorted(['e']))
def test_subscription_redundant(self): jobs_init = celery_pubsub.pubsub._pubsub_manager.get_jobs( 'redundant.test').tasks celery_pubsub.subscribe('redundant.test', job_a) jobs_before = celery_pubsub.pubsub._pubsub_manager.get_jobs( 'redundant.test').tasks celery_pubsub.subscribe('redundant.test', job_a) jobs_after = celery_pubsub.pubsub._pubsub_manager.get_jobs( 'redundant.test').tasks celery_pubsub.unsubscribe('redundant.test', job_a) jobs_end = celery_pubsub.pubsub._pubsub_manager.get_jobs( 'redundant.test').tasks self.assertListEqual(jobs_before, jobs_after) self.assertListEqual(jobs_init, jobs_end)
def test_subscription(subscriber, job_c, celery_worker): from celery_pubsub import publish, subscribe res = publish("dummy", 4, 8, a15=16, a23=42).get() assert sorted(res) == sorted(["e"]) subscribe("dummy", job_c) res = publish("dummy", 4, 8, a15=16, a23=42).get() assert sorted(res) == sorted(["e", "c"]) unsubscribe("dummy", job_c) res = publish("dummy", 4, 8, a15=16, a23=42).get() assert sorted(res) == sorted(["e"])
def test_subscription_redundant(subscriber, job_a, celery_worker): import celery_pubsub jobs_init = celery_pubsub.pubsub._pubsub_manager.get_jobs( "redundant.test").tasks celery_pubsub.subscribe("redundant.test", job_a) jobs_before = celery_pubsub.pubsub._pubsub_manager.get_jobs( "redundant.test").tasks celery_pubsub.subscribe("redundant.test", job_a) jobs_after = celery_pubsub.pubsub._pubsub_manager.get_jobs( "redundant.test").tasks celery_pubsub.unsubscribe("redundant.test", job_a) jobs_end = celery_pubsub.pubsub._pubsub_manager.get_jobs( "redundant.test").tasks assert jobs_before == jobs_after assert jobs_init == jobs_end
return "e" @celery.task def job_f(*args, **kwargs): print "job_f: {} {}".format(args, kwargs) return "f" @celery.task def job_g(*args, **kwargs): print "job_g: {} {}".format(args, kwargs) return "g" celery_pubsub.subscribe('index.high', job_a) celery_pubsub.subscribe('index.low', job_b) celery_pubsub.subscribe('index', job_c) celery_pubsub.subscribe('index.#', job_d) celery_pubsub.subscribe('#', job_e) celery_pubsub.subscribe('index.*.test', job_f) celery_pubsub.subscribe('index.#.test', job_g) class PubsubTest(unittest.TestCase): def test_subscription(self): from celery_pubsub import publish, subscribe res = publish('dummy', 4, 8, a15=16, a23=42).get() self.assertListEqual(sorted(res), sorted(['e'])) subscribe('dummy', job_c)
def subscriber(job_a, job_b, job_c, job_d, job_e, job_f, job_g): subscribe("index.high", job_a) subscribe("index.low", job_b) subscribe("index", job_c) subscribe("index.#", job_d) subscribe("#", job_e) subscribe("index.*.test", job_f) subscribe("index.#.test", job_g)
import celery_pubsub from kpi_notificator import celery_app from actions.models import Action from workers.operations.tasks.run_operation import run_operation from workers.notify.tasks.send_notification import send_notification @celery_app.task def trigger_event_worker(trigger_check, metric_log, threshold_): print(f"trigger_event_worker: Received Trigger Event {trigger_check} " f"{metric_log} {threshold_}") actions = (Action.objects.filter(trigger=trigger_check.trigger, trigger_status=trigger_check.status)) for action in actions: for operation in action.operations.all(): run_operation.delay(operation.key, trigger_check, metric_log) print(f"trigger_event_worker: Publishing Operation task " f"with args operation.key={operation.key}, " f"{trigger_check}, {metric_log}") for notification in action.notifications.all(): send_notification.delay(notification, trigger_check, metric_log) print(f"trigger_event_worker: Notification task " f"with args notification={notification}, " f"{trigger_check}, {metric_log}") # todo: bad place for subscribe celery_pubsub.subscribe('trigger-event', trigger_event_worker)
def register_tasks(): celery_pubsub.subscribe('connections.new.*', new_connection)
import celery_pubsub from kpi_notificator import celery_app from trigger.models import Trigger from workers.trigger_handlers.tasks.trigger_worker import trigger_worker @celery_app.task def trigger_finder(metric_log): triggers = (Trigger.objects.filter(active=True, metric__key=metric_log.metric.key)) print(f"trigger_finder: found {triggers} " f"for {metric_log.metric.key} metric") for trigger in triggers: trigger_worker.delay(trigger, metric_log) # todo: bad place for subscribe celery_pubsub.subscribe('metric.loaded', trigger_finder)