Esempio n. 1
0
    def handle_task(self, full_task):
        task = full_task['cloudify_task']
        ctx = task['kwargs'].pop('__cloudify_context')

        handler = self.handler(cloudify_context=ctx,
                               args=task.get('args', []),
                               kwargs=task['kwargs'],
                               process_registry=self._registry)

        self._print_task(ctx, 'Started handling', handler)
        try:
            self._validate_not_cancelled(handler, ctx)
            rv = handler.handle_or_dispatch_to_subprocess_if_remote()
            result = {'ok': True, 'result': rv}
            status = 'SUCCESS - result: {0}'.format(result)
        except exceptions.StopAgent:
            result = STOP_AGENT
            status = 'Stopping agent'
        except exceptions.OperationRetry as e:
            result = {'ok': False, 'error': serialize_known_exception(e)}
            status = 'Operation rescheduled'
        except exceptions.ProcessKillCancelled:
            self._print_task(ctx, 'Task kill-cancelled', handler)
            return NO_RESPONSE
        except Exception as e:
            error = serialize_known_exception(e)
            result = {'ok': False, 'error': error}
            status = 'ERROR - result: {0}'.format(result)
            logger.error(
                'ERROR - caught: %r%s', e, '\n{0}'.format(error['traceback'])
                if error.get('traceback') else '')
        self._print_task(ctx, 'Finished handling', handler, status)
        return result
Esempio n. 2
0
def main():
    dispatch_dir = sys.argv[1]
    with open(os.path.join(dispatch_dir, 'input.json')) as f:
        dispatch_inputs = json.load(f)
    cloudify_context = dispatch_inputs['cloudify_context']
    args = dispatch_inputs['args']
    kwargs = dispatch_inputs['kwargs']
    dispatch_type = cloudify_context['type']
    threading.current_thread().setName('Dispatch-{0}'.format(dispatch_type))
    handler_cls = TASK_HANDLERS[dispatch_type]
    handler = None
    try:
        handler = handler_cls(cloudify_context=cloudify_context,
                              args=args,
                              kwargs=kwargs)
        handler.setup_logging()
        payload = handler.handle()
        payload_type = 'result'
    except BaseException as e:
        payload_type = 'error'
        payload = serialize_known_exception(e)

        logger = logging.getLogger(__name__)
        logger.error('Task {0}[{1}] raised:\n{2}'.format(
            handler.cloudify_context['task_name'],
            handler.cloudify_context.get('task_id', '<no-id>'),
            payload['traceback']))

    with open(os.path.join(dispatch_dir, 'output.json'), 'w') as f:
        json.dump({'type': payload_type, 'payload': payload}, f)
Esempio n. 3
0
 def handle_task(self, full_task):
     task = full_task['cloudify_task']
     ctx = task['kwargs'].pop('__cloudify_context')
     self._print_task(ctx, 'Started handling')
     handler = self.handler(cloudify_context=ctx,
                            args=task.get('args', []),
                            kwargs=task['kwargs'],
                            process_registry=self._registry)
     try:
         rv = handler.handle_or_dispatch_to_subprocess_if_remote()
         result = {'ok': True, 'result': rv}
         status = 'SUCCESS - result: {0}'.format(result)
     except SUPPORTED_EXCEPTIONS as e:
         error = serialize_known_exception(e)
         result = {'ok': False, 'error': error}
         status = 'ERROR - result: {0}'.format(result)
         logger.error('ERROR - caught: {0}\n{1}'.format(
             repr(e), error['traceback']))
     self._print_task(ctx, 'Finished handling', status)
     return result
Esempio n. 4
0
    def handle_task(self, full_task):
        task = full_task['cloudify_task']
        ctx = task['kwargs'].pop('__cloudify_context')

        self._print_task(ctx, 'Started handling')
        handler = self.handler(cloudify_context=ctx,
                               args=task.get('args', []),
                               kwargs=task['kwargs'],
                               process_registry=self._registry)
        try:
            rv = handler.handle_or_dispatch_to_subprocess_if_remote()
            result = {'ok': True, 'result': rv}
            status = 'SUCCESS - result: {0}'.format(result)
        except SUPPORTED_EXCEPTIONS as e:
            error = serialize_known_exception(e)
            result = {'ok': False, 'error': error}
            status = 'ERROR - result: {0}'.format(result)
            logger.error(
                'ERROR - caught: {0}\n{1}'.format(
                    repr(e), error['traceback']))
        self._print_task(ctx, 'Finished handling', status)
        return result