Ejemplo n.º 1
0
def _process_results():
    """Process the results from an Async job."""
    async = get_current_async()
    callbacks = async .get_callbacks()

    if not isinstance(async .result.payload, AsyncException):
        callback = callbacks.get('success')
def all_done():
    """Will be run if the async task runs successfully."""
    from furious.context import get_current_async

    async = get_current_async()

    logging.info('async task complete, value returned: %r', async .result)
Ejemplo n.º 3
0
def all_done():
    """Will be run if the async task runs successfully."""
    from furious.context import get_current_async

    async = get_current_async()

    logging.info('async task complete, value returned: %r', async.result)
Ejemplo n.º 4
0
def _process_results():
    """Process the results from an Async job."""
    async = get_current_async()
    callbacks = async.get_callbacks()

    if not isinstance(async.result.payload, AsyncException):
        callback = callbacks.get('success')
def log_results():
    """This is the callback that is run once the Async task is finished. It
    takes the output from grep and logs it."""
    from furious.context import get_current_async

    # Get the recently finished Async object.
    async = get_current_async()

    # Pull out the result data and log it.
    for result in async.result:
        logging.info(result)
Ejemplo n.º 6
0
def log_results():
    """This is the callback that is run once the Async task is finished. It
    takes the output from grep and logs it."""
    from furious.context import get_current_async

    # Get the recently finished Async object.
    async = get_current_async()

    # Pull out the result data and log it.
    for result in async.result:
        logging.info(result)
Ejemplo n.º 7
0
def _process_results():
    """Process the results from an Async job."""
    async = get_current_async()
    callbacks = async .get_callbacks()

    if isinstance(async .result, AsyncException):
        error_callback = callbacks.get('error')
        if not error_callback:
            raise async .result.exception, None, async .result.traceback

        return _execute_callback(async, error_callback)
Ejemplo n.º 8
0
def _process_results():
    """Process the results from an Async job."""
    async = get_current_async()
    callbacks = async.get_callbacks()

    if isinstance(async.result, AsyncException):
        error_callback = callbacks.get('error')
        if not error_callback:
            raise async.result.exception, None, async.result.traceback

        return _execute_callback(async, error_callback)
Ejemplo n.º 9
0
    def _initialize_recursion_depth(self):
        """Ensure recursion info is initialized, if not, initialize it."""
        from furious.context import get_current_async

        recursion_options = self._options.get('_recursion', {})

        current_depth = recursion_options.get('current', 0)
        max_depth = recursion_options.get('max', MAX_DEPTH)

        try:
            executing_async = get_current_async()

            # If this async is within an executing async, use the depth off
            # that async.  Otherwise use the depth set in the async's options.
            current_depth = executing_async.recursion_depth

            # If max_depth does not equal MAX_DEPTH, it is custom. Otherwise
            # use the max_depth from the containing async.
            if max_depth == MAX_DEPTH:
                executing_options = executing_async.get_options().get(
                    '_recursion', {})
                max_depth = executing_options.get('max', max_depth)

        except errors.NotInContextError:
            # This Async is not being constructed inside an executing Async.
            pass

        # Store the recursion info.
        self.update_options(_recursion={
            'current': current_depth,
            'max': max_depth
        })
Ejemplo n.º 10
0
    def _initialize_recursion_depth(self):
        """Ensure recursion info is initialized, if not, initialize it."""
        from furious.context import get_current_async

        recursion_options = self._options.get('_recursion', {})

        current_depth = recursion_options.get('current', 0)
        max_depth = recursion_options.get('max', MAX_DEPTH)

        try:
            executing_async = get_current_async()

            # If this async is within an executing async, use the depth off
            # that async.  Otherwise use the depth set in the async's options.
            current_depth = executing_async.recursion_depth

            # If max_depth does not equal MAX_DEPTH, it is custom. Otherwise
            # use the max_depth from the containing async.
            if max_depth == MAX_DEPTH:
                executing_options = executing_async.get_options().get(
                    '_recursion', {})
                max_depth = executing_options.get('max', max_depth)

        except errors.NotInContextError:
            # This Async is not being constructed inside an executing Async.
            pass

        # Store the recursion info.
        self.update_options(_recursion={'current': current_depth,
                                        'max': max_depth})
def handle_an_error():
    """Will be run if the async task raises an unhandled exception."""
    import os

    from furious.context import get_current_async

    async = get_current_async()
    async_exception = async .result.payload
    exc_info = async_exception.traceback
    logging.info('async job blew up, exception info: %r', exc_info)

    retries = int(os.environ['HTTP_X_APPENGINE_TASKRETRYCOUNT'])
    if retries < 2:
        raise Exception(async_exception.error)
    else:
        logging.info('Caught too many errors, giving up now.')
Ejemplo n.º 12
0
def handle_an_error():
    """Will be run if the async task raises an unhandled exception."""
    import os

    from furious.context import get_current_async

    async = get_current_async()
    async_exception = async.result.payload
    exc_info = async_exception.traceback
    logging.info('async job blew up, exception info: %r', exc_info)

    retries = int(os.environ['HTTP_X_APPENGINE_TASKRETRYCOUNT'])
    if retries < 2:
        raise Exception(async_exception.error)
    else:
        logging.info('Caught too many errors, giving up now.')
Ejemplo n.º 13
0
def run_job():
    """Takes an async object and executes its job."""
    async = get_current_async()
    async_options = async.get_options()

    job = async_options.get('job')
    if not job:
        raise Exception('This async contains no job to execute!')

    function_path, args, kwargs = job

    if args is None:
        args = ()

    if kwargs is None:
        kwargs = {}

    function = path_to_reference(function_path)

    try:
        async.executing = True
        async.result = AsyncResult(payload=function(*args, **kwargs),
                                   status=AsyncResult.SUCCESS)
    except Abort as abort:
        logging.info('Async job was aborted: %r', abort)
        async.result = AsyncResult(status=AsyncResult.ABORT)

        # QUESTION: In this eventuality, we should probably tell the context we
        # are "complete" and let it handle completion checking.
        _handle_context_completion_check(async)
        return
    except AbortAndRestart as restart:
        logging.info('Async job was aborted and restarted: %r', restart)
        raise
    except Exception as e:
        async.result = AsyncResult(payload=encode_exception(e),
                                   status=AsyncResult.ERROR)

    _handle_results(async_options)
    _handle_context_completion_check(async)
Ejemplo n.º 14
0
def run_job():
    """Takes an async object and executes its job."""
    async = get_current_async()
    async_options = async .get_options()

    job = async_options.get('job')
    if not job:
        raise Exception('This async contains no job to execute!')

    __, args, kwargs = job

    if args is None:
        args = ()

    if kwargs is None:
        kwargs = {}

    function = async ._decorate_job()

    try:
        async .executing = True
        async .result = AsyncResult(payload=function(*args, **kwargs),
                                    status=AsyncResult.SUCCESS)
    except Abort as abort:
        logging.info('Async job was aborted: %r', abort)
        async .result = AsyncResult(status=AsyncResult.ABORT)

        # QUESTION: In this eventuality, we should probably tell the context we
        # are "complete" and let it handle completion checking.
        _handle_context_completion_check(async)
        return
    except AbortAndRestart as restart:
        logging.info('Async job was aborted and restarted: %r', restart)
        raise
    except BaseException as e:
        async .result = AsyncResult(payload=encode_exception(e),
                                    status=AsyncResult.ERROR)

    _handle_results(async_options)
    _handle_context_completion_check(async)
Ejemplo n.º 15
0
def run_job():
    """Takes an async object and executes its job."""
    async = get_current_async()
    async_options = async .get_options()

    job = async_options.get('job')
    if not job:
        raise Exception('This async contains no job to execute!')

    function_path, args, kwargs = job

    if args is None:
        args = ()

    if kwargs is None:
        kwargs = {}

    function = path_to_reference(function_path)

    try:
        async .executing = True
        async .result = function(*args, **kwargs)
    except Abort as abort:
        logging.info('Async job was aborted: %r', abort)
        async .result = None
        return
    except AbortAndRestart as restart:
        logging.info('Async job was aborted and restarted: %r', restart)
        raise
    except Exception as e:
        async .result = encode_exception(e)

    results_processor = async_options.get('_process_results')
    if not results_processor:
        results_processor = _process_results

    processor_result = results_processor()
    if isinstance(processor_result, (Async, Context)):
        processor_result.start()
Ejemplo n.º 16
0
def run_job():
    """Takes an async object and executes its job."""
    async = get_current_async()
    async_options = async.get_options()

    job = async_options.get('job')
    if not job:
        raise Exception('This async contains no job to execute!')

    function_path, args, kwargs = job

    if args is None:
        args = ()

    if kwargs is None:
        kwargs = {}

    function = path_to_reference(function_path)

    try:
        async.executing = True
        async.result = function(*args, **kwargs)
    except Abort as abort:
        logging.info('Async job was aborted: %r', abort)
        async.result = None
        return
    except AbortAndRestart as restart:
        logging.info('Async job was aborted and restarted: %r', restart)
        raise
    except Exception as e:
        async.result = encode_exception(e)

    results_processor = async_options.get('_process_results')
    if not results_processor:
        results_processor = _process_results

    processor_result = results_processor()
    if isinstance(processor_result, (Async, Context)):
        processor_result.start()
Ejemplo n.º 17
0
    def _get_parent_id(self):
        """If this async is in within another async set that async id as the
        parent.
        """
        parent_id = self._options.get('parent_id')
        if parent_id:
            return parent_id

        from furious.context import get_current_async

        try:
            async = get_current_async()
        except errors.NotInContextError:
            async = None

        if async:
            parent_id = ":".join([async .parent_id.split(":")[0], async .id])
        else:
            parent_id = self.request_id

        self.update_options(parent_id=parent_id)

        return parent_id
Ejemplo n.º 18
0
    def _get_parent_id(self):
        """If this async is in within another async set that async id as the
        parent.
        """
        parent_id = self._options.get('parent_id')
        if parent_id:
            return parent_id

        from furious.context import get_current_async

        try:
            async = get_current_async()
        except errors.NotInContextError:
            async = None

        if async:
            parent_id = ":".join([async.parent_id.split(":")[0], async.id])
        else:
            parent_id = self.request_id

        self.update_options(parent_id=parent_id)

        return parent_id
Ejemplo n.º 19
0
def state_machine_success():
    """A positive result!  Iterate!"""
    from furious.async import Async
    from furious.context import get_current_async

    result = get_current_async().result

    if result == 'ALPHA':
        logging.info('Inserting continuation for state %s.', result)
        return Async(target=complex_state_generator_alpha, args=[result])

    elif result == 'BRAVO':
        logging.info('Inserting continuation for state %s.', result)
        return Async(target=complex_state_generator_bravo, args=[result])

    logging.info('Done working, stop now.')
Ejemplo n.º 20
0
def state_machine_success():
    """A positive result!  Iterate!"""
    from furious. async import Async
    from furious.context import get_current_async

    result = get_current_async().result

    if result == 'ALPHA':
        logging.info('Inserting continuation for state %s.', result)
        return Async(target=complex_state_generator_alpha, args=[result])

    elif result == 'BRAVO':
        logging.info('Inserting continuation for state %s.', result)
        return Async(target=complex_state_generator_bravo, args=[result])

    logging.info('Done working, stop now.')
Ejemplo n.º 21
0
def log_results():
    from furious.context import get_current_async

    async = get_current_async()
    for result in async.result:
        logging.info(result)
Ejemplo n.º 22
0
def _fake_result_returning_callback():
    from furious.context import get_current_async

    return get_current_async().result.payload