Example #1
0
def test_Task_delay_reserved_options_in_kwargs(option, expected):
    with mock.patch('girder_worker.task.celery.Task.apply_async',
                    spec=True) as mock_apply_async:
        t = Task()
        t.name = 'example.task'
        t.delay(**{option: expected})
        margs, mkwargs = mock_apply_async.call_args
        assert 'headers' in mkwargs
        assert option in mkwargs['headers']
        assert mkwargs['headers'][option] == expected
Example #2
0
def test_Task_apply_async_reserved_headers_in_kwargs(header, expected):
    with mock.patch('girder_worker.task.celery.Task.apply_async',
                    spec=True) as mock_apply_async:
        t = Task()
        t.name = 'example.task'
        t.apply_async((), {header: expected})
        margs, mkwargs = mock_apply_async.call_args
        assert 'headers' in mkwargs
        assert header in mkwargs['headers']
        assert mkwargs['headers'][header] == expected
Example #3
0
def test_Task_apply_async_does_not_meddle_with_headers_on_builtin_tasks(name):
    kwargs = dict(RESERVED_OPTIONS)

    with mock.patch('girder_worker.task.celery.Task.apply_async',
                    spec=True) as mock_apply_async:
        t = Task()
        t.name = name
        t.apply_async((), kwargs, **{})
        mock_apply_async.assert_called_once()

    # Expected behavior is that reserved options will be popped out of kwargs
    # This tests to make sure that we never meddle with headers on builtin tasks
    for k, _ in RESERVED_OPTIONS:
        assert k in kwargs
Example #4
0
def test_Task_apply_async_reserved_in_options_with_existing_header_option(
        header, expected):
    with mock.patch('girder_worker.task.celery.Task.apply_async',
                    spec=True) as mock_apply_async:
        t = Task()
        t.name = 'example.task'
        t.apply_async((), {}, **{
            header: expected,
            'headers': {
                'some': 'header'
            }
        })
        margs, mkwargs = mock_apply_async.call_args
        assert 'headers' in mkwargs
        assert header in mkwargs['headers']
        assert mkwargs['headers'][header] == expected
Example #5
0
def girder_before_task_publish(sender=None, body=None, exchange=None,
                               routing_key=None, headers=None, properties=None,
                               declare=None, retry_policy=None, **kwargs):

    if is_builtin_celery_task(sender):
        return

    job = None

    try:
        context = get_context()
        if 'jobInfoSpec' not in headers:
            job = context.create_task_job(
                Task.girder_job_defaults(), sender=sender, body=body, exchange=exchange,
                routing_key=routing_key, headers=headers, properties=properties, declare=declare,
                retry_policy=retry_policy, **kwargs)

        if 'girder_api_url' not in headers:
            context.attach_girder_api_url(sender=sender, body=body,
                                          exchange=exchange,
                                          routing_key=routing_key,
                                          headers=headers,
                                          properties=properties,
                                          declare=declare,
                                          retry_policy=retry_policy,
                                          **kwargs)

        if 'girder_client_token' not in headers:
            context.attach_girder_client_token(sender=sender,
                                               body=body,
                                               exchange=exchange,
                                               routing_key=routing_key,
                                               headers=headers,
                                               properties=properties,
                                               declare=declare,
                                               retry_policy=retry_policy,
                                               **kwargs)
        if 'girder_result_hooks' in headers:
            if job is not None:
                for result_hook in headers['girder_result_hooks']:
                    if isinstance(result_hook, ResultTransform):
                        result_hook.job = job

            # Celery task headers are not automatically serialized by celery
            # before being passed off to ampq for byte packing. We will have
            # to do that here.
            p = jsonpickle.pickler.Pickler()
            headers['girder_result_hooks'] = \
                [p.flatten(grh) for grh in headers['girder_result_hooks']]

        # Finally,  remove all reserved_options from headers
        for key in Task.reserved_options:
            headers.pop(key, None)
    except Exception:
        logger.exception('An error occurred in girder_before_task_publish.')
        raise
Example #6
0
def test_Task_AsynResult_of_type_GirderAsyncResult():
    assert isinstance(Task().AsyncResult('BOGUS_TASK_ID'), GirderAsyncResult)
Example #7
0
def _task_with_request(*args, **kwargs):
    task = Task()
    task.name = 'example.task'
    task.request_stack = celery.utils.threads.LocalStack()
    task.push_request(*args, **kwargs)
    return task