def register_signals(APPENLIGHT_CLIENT):

    def prerun_signal(sender, task_id, task, args, kwargs, *aargs, **kwds):
        task._appenlight_start_time = datetime.utcnow()

    def postrun_signal(sender, task_id, task, args, kwargs, retval, *aargs, **kwds):
        end_time = datetime.utcnow()
        start_time = getattr(task, '_appenlight_start_time')
        fake_environ = {'appenlight.view_name': 'celery:' + sender.name}
        gather_data(APPENLIGHT_CLIENT, fake_environ, gather_exception=False,
                    start_time=start_time, end_time=end_time)

    def failure_signal(sender, task_id, exception, args, kwargs, traceback,
                       einfo, *aargs, **kwds):
        end_time = datetime.utcnow()
        start_time = getattr(sender, '_appenlight_start_time')
        fake_environ = {'appenlight.view_name': 'celery:' + sender.name}
        gather_data(APPENLIGHT_CLIENT, fake_environ,
                    start_time=start_time, end_time=end_time)

    def after_setup_logger_signal(sender=None, logger=None, loglevel=None,
                                  logfile=None, format=None,
                                  colorize=None, **kwargs):
        if APPENLIGHT_CLIENT.config['logging'] and APPENLIGHT_CLIENT.config['enabled']:
            APPENLIGHT_CLIENT.register_logger(logger)

    task_prerun.connect(prerun_signal, weak=False)
    task_postrun.connect(postrun_signal, weak=False)
    task_failure.connect(failure_signal, weak=False)
    task_success.connect(postrun_signal, weak=False)
    after_setup_logger.connect(after_setup_logger_signal, weak=False)
    return True
Esempio n. 2
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)
def register_signals(APPENLIGHT_CLIENT):
    def prerun_signal(sender, task_id, task, args, kwargs, *aargs, **kwds):
        task._appenlight_start_time = datetime.utcnow()

    def postrun_signal(sender, task_id, task, args, kwargs, retval, *aargs,
                       **kwds):
        end_time = datetime.utcnow()
        start_time = getattr(task, '_appenlight_start_time')
        fake_environ = {'appenlight.view_name': 'celery:' + sender.name}
        gather_data(APPENLIGHT_CLIENT,
                    fake_environ,
                    gather_exception=False,
                    start_time=start_time,
                    end_time=end_time)

    def failure_signal(sender, task_id, exception, args, kwargs, traceback,
                       einfo, *aargs, **kwds):
        end_time = datetime.utcnow()
        start_time = getattr(sender, '_appenlight_start_time')
        fake_environ = {'appenlight.view_name': 'celery:' + sender.name}
        gather_data(APPENLIGHT_CLIENT,
                    fake_environ,
                    start_time=start_time,
                    end_time=end_time)

    def after_setup_logger_signal(sender=None,
                                  logger=None,
                                  loglevel=None,
                                  logfile=None,
                                  format=None,
                                  colorize=None,
                                  **kwargs):
        if APPENLIGHT_CLIENT.config['logging'] and APPENLIGHT_CLIENT.config[
                'enabled']:
            APPENLIGHT_CLIENT.register_logger(logger)

    task_prerun.connect(prerun_signal, weak=False)
    task_postrun.connect(postrun_signal, weak=False)
    task_failure.connect(failure_signal, weak=False)
    task_success.connect(postrun_signal, weak=False)
    after_setup_logger.connect(after_setup_logger_signal, weak=False)
    return True
Esempio n. 4
0
    task = ManagedTask.objects.get(celery_task_id=task_id)
    task.celery_task_status = 5
    task.is_enable = False
    task.save(check_enable=False)


def on_task_rejected(*args, **kwargs):
    task = kwargs['sender']

    task_id = task.request.id
    task = ManagedTask.objects.get(celery_task_id=task_id)
    task.celery_task_status = 6
    task.is_enable = False
    task.save(check_enable=False)


def on_task_worker_shutting_down(*args, **kwargs):
    ManagedTask.objects.filter(is_enable=True).update(celery_task_status=6)


worker_ready.connect(on_worker_ready, dispatch_uid='on_worker_ready')
task_prerun.connect(on_task_prerun, dispatch_uid='on_task_prerun')
task_postrun.connect(on_task_finished, dispatch_uid='on_task_postrun')

task_success.connect(on_task_success, dispatch_uid='on_task_success')
task_retry.connect(on_task_retry, dispatch_uid='on_task_retry')
task_failure.connect(on_task_failure, dispatch_uid='on_task_failure')
task_revoked.connect(on_task_revoked, dispatch_uid='on_task_revoked')
task_rejected.connect(on_task_rejected, dispatch_uid='on_task_rejected')
worker_shutting_down.connect(on_task_worker_shutting_down,
                             dispatch_uid='on_task_worker_shutting')
Esempio n. 5
0
    A decorator to memoize functions on a per-request basis.
    Arguments to the memoized function should NOT be objects
    Use primitive types as arguments
    """

    def wrapped(*args, **kwargs):
        # if no request, skip cache
        if app.env.request is None:
            return func(*args, **kwargs)

        if not hasattr(_cache, "items"):
            _cache.items = {}
        cache_key = (func, repr(args), repr(kwargs))
        if cache_key in _cache.items:
            rv = _cache.items[cache_key]
        else:
            rv = func(*args, **kwargs)
            _cache.items[cache_key] = rv
        return rv

    return wrapped


def clear_cache(**kwargs):
    _cache.items = {}


request_finished.connect(clear_cache)
task_failure.connect(clear_cache)
task_success.connect(clear_cache)
Esempio n. 6
0
 def _install_patches(self):
     Task.apply_async = task_apply_async_wrapper
     before_task_publish.connect(before_task_publish_handler)
     task_prerun.connect(task_prerun_handler)
     task_success.connect(task_success_handler)
     task_failure.connect(task_failure_handler)
def init_celery():
    celery.finalize()
    if celery.conf.CELERY_ALWAYS_EAGER:
        task_success.connect(check_result_serialization)