Example #1
0
def execute_hooks(event_name, *args, **kwargs):
    """Execute action hooks based upon parent task state.
    """
    # skip tasks that aren't part of pipeline
    if not isinstance(kwargs['sender'], PipelineTask):
        return

    task_kwargs = deepcopy(kwargs['kwargs'])  # inception!
    state = task_kwargs.get('_pipeline_chain_state', {})

    if 'hooks' in state:

        hooks = [
            h for h in state['hooks'] if \
            h.event == event_name
        ]

        if not hooks:
            return []

        logger.debug('task has hooks: {}'.format(hooks))
        context = task_kwargs['_pipeline_chain_state']['build_context']

        callbacks = []

        for hook in hooks:
            try:
                should_execute = safe_eval(
                    hook.predicate,
                    context.eval_context
                )
            except:
                should_execute = False

            if should_execute:
                source = kwargs['args'][0]  # wtf
                callbacks.append(hook.task_action.prepare(source, context))
            else:
                logger.debug('hook {} should not execute.'.format(hook.task_action))

        if len(callbacks):
            logger.debug('Executing some hooks: {}'.format(callbacks))
            canvas = group(*callbacks)
            return canvas.apply_async()

    return []
Example #2
0
 def evaluate(self, expression):
     """Evaluate an expression using context.
     """
     return safe_eval(expression, self.eval_context)