#! /usr/bin/env python """Helper script to launch a feed sender workflow for multiple days.""" import argparse import datetime import json import subprocess # for mocking in unit tests log = print def exec_multiple_days(flow, days, skip, dry_run=False): """Execute SWF Sender Flow for range of days. Will execute a workflow for last N days. The parameters describe the slice of days going backwards from today. So if skip=1 and days=5, it skips today and works on the 5 days before that. Args: flow (str): Name of flow. days (int): Number of past days to run workflow for. skip (int): Number of past days to skip starting from today. dry_run (bool): Output command statements without running. """ # since days can include current day, subtract 1 in the offset. This # prevents offsets from always being at least one day back. days_offset = datetime.timedelta(days=days - 1) skip_offset = datetime.timedelta(days=skip) date_N_days_ago = datetime.date.today() date_N_days_ago -= days_offset date_N_days_ago -= skip_offset for i in range(days): context_date = date_N_days_ago + datetime.timedelta(days=i) context = { 'context_date': context_date.strftime('%Y-%m-%d')} if dry_run: context_string = "'{context}'".format( # noqa context=json.dumps(context)) call = ['garcon', 'exec', flow, '--context', context_string] log(' '.join(call)) else: context_string = '{context}'.format(context=json.dumps(context)) call = ['garcon', 'exec', flow, '--context', context_string] subprocess.call(call) if __name__ == '__main__': parser = argparse.ArgumentParser(description='Date Range Flow Exec') parser.add_argument('flow', help='name of the workflow') parser.add_argument( '--days', type=int, default=5, help=( 'number of days to sync in the db from the start date defined by ' 'the skip flag (Default: 5)')) parser.add_argument( '--skip', type=int, default=1, help=( 'number of days backwards from today to skip in the sync ' '(Default: 1)')) parser.add_argument( '--dry-run', action='store_true', help='Output the calls without running them') args = parser.parse_args() exec_multiple_days(args.flow, args.days, args.skip, args.dry_run)