"""Sales Data ETL Command Line Interface.""" from time import sleep from garcon.activity import ActivityWorker from garcon.decider import DeciderWorker from flows import config as base_config from flows import util from flows.flow import DatabaseParam from flows.sales_data import config from flows.sales_data import log from flows.sales_data.flow import Flow def init_execute_parser(parser): """Setup the 'execute sales_data' sub-command to the CLI. Args: parser (ArgumentParser): sub-command argument parser. """ parser.add_argument( '-u', '--upc', action='append', dest='upcs', metavar='UPC', type=util.upc_cli_type, help='Release UPC(s) to process. This can be used multiple times.') parser.add_argument( '-a', '--accounting-period-id', dest='accounting_period_id', type=int, help='Accounting period id to ingest. If none, lookup will be used.') def run_execute(correlation_id, upcs, accounting_period_id): """CLI entry point to the 'execute sales_data' sub-command. This triggers a new SWF execution with required context data and adds a log entry. Args: correlation_id (str): new correlation ID for this ETL. upcs (tuple): UPCs to process. accounting_period_id (str): sales_data period id to process. Returns: str: workflow execution's run ID. Raises: ValueError: required value is missing or invalid. """ upcs = tuple(upcs) if upcs else util.get_serviced_upcs(query_based=True) upcs = DatabaseParam('upcs', data=upcs) context = { 'correlation_id': correlation_id, 'upcs': upcs, 'accounting_period_id': accounting_period_id} log.create(**context) context['correlation_id'] = '{}.1'.format(correlation_id) response = util.start_swf_execution( context, config.SWF_WORKFLOW_TIMEOUT, config.SWF_WORKFLOW_NAME, config.SWF_WORKFLOW_VERSION, config.SWF_WORKFLOW_ID) log.add_run_id(correlation_id, response['runId']) return response['runId'] def init_worker_parser(parser): """Setup the 'worker sales_data' sub-command to the CLI. Args: parser (argparse.ArgumentParser): sub-command argument parser. """ pass def run_worker(): """CLI entry point to the 'worker sales_data' sub-command.""" worker = ActivityWorker(Flow( base_config.SWF_DOMAIN, config.SWF_WORKFLOW_NAME, config.SWF_WORKFLOW_VERSION)) worker.run() def init_decider_parser(parser): """Setup the 'decider sales_data' sub-command to the CLI. Args: parser (argparse.ArgumentParser): sub-command argument parser. """ pass def run_decider(): """CLI entry point to the 'decider sales_data' sub-command.""" decider = DeciderWorker(Flow( base_config.SWF_DOMAIN, config.SWF_WORKFLOW_NAME, config.SWF_WORKFLOW_VERSION)) while True: decider.run() sleep(1)