Ejemplo n.º 1
0
    def __init__(self, parent, **kwargs):
        super().__init__(parent, **kwargs)
        import celery
        from celery.signals import (
            before_task_publish,
            after_task_publish,
            task_prerun,
            task_retry,
            task_success,
            task_failure,
            task_revoked,
        )

        before_task_publish.connect(receivers.receiver_before_task_publish)
        after_task_publish.connect(receivers.receiver_after_task_publish)
        task_prerun.connect(receivers.receiver_task_pre_run)
        task_retry.connect(receivers.receiver_task_retry)
        task_success.connect(receivers.receiver_task_success)
        task_failure.connect(receivers.receiver_task_failure)
        task_revoked.connect(receivers.receiver_task_revoked)
        if celery.VERSION > (4, ):

            from celery.signals import task_unknown, task_rejected

            task_unknown.connect(receivers.receiver_task_unknown)
            task_rejected.connect(receivers.receiver_task_rejected)
Ejemplo n.º 2
0
    def test_defer_task(self):
        expected_uuid = "00000000-0000-0000-0000-000000000000"

        request = self.factory.get("/foo")
        request.user = AnonymousUser()

        @shared_task
        def test_task(value):  # pragma: no cover
            pass

        from celery.signals import before_task_publish, after_task_publish

        before_task_publish.connect(receivers.receiver_before_task_publish)
        after_task_publish.connect(receivers.receiver_after_task_publish)
        try:
            with structlog.threadlocal.tmp_bind(self.logger):
                self.logger.bind(request_id=expected_uuid)
                with self.assertLogs(
                    logging.getLogger("django_structlog.celery.receivers"), logging.INFO
                ) as log_results:
                    test_task.delay("foo")
        finally:
            before_task_publish.disconnect(receivers.receiver_before_task_publish)
            after_task_publish.disconnect(receivers.receiver_after_task_publish)

        self.assertEqual(1, len(log_results.records))
        record = log_results.records[0]
        self.assertEqual("task_enqueued", record.msg["event"])
        self.assertEqual("INFO", record.levelname)
        self.assertIn("child_task_id", record.msg)
        self.assertEqual(expected_uuid, record.msg["request_id"])
Ejemplo n.º 3
0
 def register_signals(self):
     ''' Register celery signals for task publishing '''
     # register signals
     before_task_publish.connect(
         self.before_task_publish_action,
         sender=CeleryConstants.get_analyze_task_name())
     after_task_publish.connect(
         self.after_task_publish_action,
         sender=CeleryConstants.get_analyze_task_name())
Ejemplo n.º 4
0
 def connect_traced_handlers(self):
     if self._propagate:
         before_task_publish.connect(self._prepublish, weak=False)
         after_task_publish.connect(self._postpublish, weak=False)
     task_prerun.connect(self._start_span, weak=False)
     task_failure.connect(self._tag_error, weak=False)
     task_retry.connect(self._tag_retry, weak=False)
     task_postrun.connect(self._finish_span, weak=False)
     log.debug('Registered CeleryTracing signal handlers.')
Ejemplo n.º 5
0
    def install(cls):
        if hasattr(cls, "_instance"):
            return
        self = cls()
        # Make sure to keep a reference alive so this is not garbage collected.
        cls._instance = self

        worker_ready.connect(self.on_worker_ready)
        beat_init.connect(self.on_beat_init)
        before_task_publish.connect(self.on_before_task_publish)
        after_task_publish.connect(self.on_after_task_publish)
Ejemplo n.º 6
0
 def add_publish_signals(cls):
     from celery.signals import before_task_publish, after_task_publish
     before_task_publish.connect(cls.before_task_publish)
     after_task_publish.connect(cls.after_task_publish)
Ejemplo n.º 7
0
    def __init__(self, get_response=None):
        self.get_response = get_response
        from celery.signals import before_task_publish, after_task_publish

        before_task_publish.connect(receiver_before_task_publish)
        after_task_publish.connect(receiver_after_task_publish)
Ejemplo n.º 8
0
 def register_signals(self):
     ''' Register celery signals for task publishing '''
     # register signals
     before_task_publish.connect(self.before_task_publish_action, sender = CeleryConstants.get_analyze_task_name())
     after_task_publish.connect(self.after_task_publish_action, sender = CeleryConstants.get_analyze_task_name())