"""CLI functions to run decider, worker and exec processes.""" import argparse import importlib import json import time import boto.swf.layer2 as swf from garcon import activity from garcon import decider from feed_sender import flows def execute_flow(flow, context, **kwargs): """Launch the workflow execution. Args: flow (module): Garcon flow module. context (str): Initial context parsed from JSON. kwargs: Extensible call API. """ start_kwargs = dict( workflow_id=flow.workflow_id(json.loads(context)), input=context) if hasattr(flow, 'timeout'): start_kwargs.update(execution_start_to_close_timeout=str(flow.timeout)) return swf.WorkflowType( name=flow.name, domain=flow.domain, version=flow.version, task_list=flow.name).start(**start_kwargs) def run_decider(flow, **kwargs): """Launch the SWF decider process. Args: flow (module): Garcon flow module. kwargs: Extensible call API. """ worker = decider.DeciderWorker(flow) while True: worker.run() time.sleep(1) def run_activity_worker(flow, **kwargs): """Launch the activity worker process. Args: flow (module): Garcon flow module. kwargs: Extensible call API. """ worker = activity.ActivityWorker(flow) worker.run() _COMMANDS = {'exec': execute_flow, 'decider': run_decider, 'worker': run_activity_worker} def garcon(*args): """Entry point for the Garcon command line integration.""" parser = argparse.ArgumentParser(description='Garcon command line util') parser.add_argument('cmd', choices=_COMMANDS.keys(), help='garcon command') parser.add_argument('flow', choices=flows.__all__, help='name of the flow') parser.add_argument('-c', '--context', help='initial context [json]') # parse cl args (allows easy unit testing) args = parser.parse_args(args) if args else parser.parse_args() # import the flow module flow = importlib.import_module( '.{}.flow'.format(args.flow), flows.__name__) # look for the flow class, if it exists, otherwise fall back # on the flow module itself. flow_class = getattr(flow, 'Flow', None) if flow_class: flow = flow_class() # execute command args.context = args.context or '{}' _COMMANDS[args.cmd](flow=flow, context=args.context)