""" ETL Theatrical Command Line Interface. All required methods to setup and execute the command line for theatrical related tasks are in here. """ from datetime import date from datetime import timedelta 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.theatrical import config from flows.theatrical import log from flows.theatrical.flow import Flow def init_execute_parser(parser): """Setup the 'execute theatrical' sub-command to the CLI. Args: parser (argparse.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( '-s', '--start-date', dest='date_start', metavar='YYYY-MM-DD', type=util.date_cli_type, help=('Starting date of data to process, inclusive. ' "Default is today's date.")) parser.add_argument( '-e', '--end-date', dest='date_end', metavar='YYYY-MM-DD', type=util.date_cli_type, help=('Ending date of data to process, exclusive. ' "Default is tomorrow's date.")) parser.add_argument( '-a', '--with-archive', dest='with_archive', action='store_true', help=('Ending date of data to process, exclusive. ' "Default is tomorrow's date.")) def run_execute(correlation_id, upcs, date_start, date_end, with_archive): """CLI entry point to the 'execute theatrical' sub-command. Args: correlation_id (str): new correlation ID for this ETL. date_start (str): starting date context. date_end (str): ending date context. upcs (tuple): UPCs to process. with_archive (bool): if True the flow will reuse source csv files. Returns: str: workflow execution's run ID. """ today = date.today() if not date_start: if upcs or date_end or with_archive: raise ValueError( 'start-date is required if any other parameter is passed.') # run without start-date parameter is allowed on Wednesday or Saturday if today.weekday() == 2: days_delta = 2 elif today.weekday() == 5: days_delta = 1 else: raise ValueError( 'Theatrical flow can be started only on Wednesday or Saturday.' ) date_start = (today - timedelta(days=5)).strftime('%Y-%m-%d') date_end = (today - timedelta(days=days_delta)).strftime('%Y-%m-%d') if not date_end: date_end = (today + timedelta(days=1)).strftime('%Y-%m-%d') if date_end < date_start: raise ValueError("end-date can't be less then start-date.") upcs = tuple(upcs) if upcs else util.get_serviced_upcs(query_based=True) upcs = DatabaseParam('upcs', data=upcs) context = { 'correlation_id': correlation_id, 'date_start': date_start, 'date_end': date_end, 'upcs': upcs, 'with_archive': with_archive} # create new record for run log.create(**context) context['correlation_id'] = '{}.1'.format(correlation_id) # run theatrical flow response = util.start_swf_execution( context, config.SWF_WORKFLOW_TIMEOUT, config.SWF_WORKFLOW_NAME, config.SWF_WORKFLOW_VERSION, config.SWF_WORKFLOW_ID) # update log record with run id log.add_run_id(correlation_id, response['runId']) return response['runId'] def init_worker_parser(parser): """Setup the 'worker theatrical' sub-command to the CLI. Args: parser (argparse.ArgumentParser): sub-command argument parser. """ pass def run_worker(): """CLI entry point to the 'worker theatrical' 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 theatrical' sub-command to the CLI. Args: parser (argparse.ArgumentParser): sub-command argument parser. """ pass def run_decider(): """CLI entry point to the 'decider theatrical' sub-command.""" decider = DeciderWorker(Flow( base_config.SWF_DOMAIN, config.SWF_WORKFLOW_NAME, config.SWF_WORKFLOW_VERSION)) while True: decider.run() sleep(1)