import jenkins import boto3 import time import os import json from datetime import datetime from datetime import timedelta from boto3.dynamodb.conditions import Key # this script also needs in env variables # AWS_ACCESS_KEY_ID= # AWS_SECRET_ACCESS_KEY= # AWS_SESSION_TOKEN= # #AWS_SECURITY_TOKEN= - the same value as AWS_SESSION_TOKEN # table name in dynamo to check status DYNAMODB_TABLE_NAME = 'prod_feed_ingestion_status' FEED_NAME = 'spotify_backfill_sme_streams' # connect to jenkins. # JENKINGS_PASS - jenkins token can be generated in UI # https://scheduler.theorchard.io/user//configure server = jenkins.Jenkins( 'https://scheduler.theorchard.io', username='', # put jenkins username password=os.environ.get('JENKINGS_PASS') ) start_date = '2018-09-01' # the most earliest date to backfill end_date = '2018-11-30' # the most recent date to backfill start_date_obj = datetime.strptime(start_date, '%Y-%m-%d') end_date_obj = datetime.strptime(end_date, '%Y-%m-%d') skip = (datetime.now() - end_date_obj).days print(skip) # can use skip value for jenkins exec job dynamodb = boto3.resource('dynamodb', region_name='us-east-1') dynamodb_table = dynamodb.Table(DYNAMODB_TABLE_NAME) client = boto3.client('swf', region_name='us-east-1') def count_executions(): """Return number of active executions.""" return len(client.list_open_workflow_executions( domain='prod_swf_feed_ingestion', startTimeFilter={ # todo set appropriate dates to monitor swf 'oldestDate': datetime(2020, 4, 23), 'latestDate': datetime(2020, 5, 30) }, typeFilter={ 'name': 'spotify_backfill_feed_ingestion', 'version': '3.0' }, )['executionInfos']) while start_date != end_date: response = dynamodb_table.query( KeyConditionExpression=Key('feed_name').eq(FEED_NAME) & Key('date').eq(end_date)) if response['Items'] and response['Items'][0]['status'] == 'INGESTED': print(end_date, response['Items'][0]['status']) else: while count_executions() >= 3: # todo set the max allowed concurrent executions print('sleeping') time.sleep(50) print('Launching for', end_date) # in case you using flow exec job , not prod-swf-ingestion-exec # use skip parameter server.build_job( 'prod-swf-ingestion-exec', {"CONTEXT": json.dumps( { "context_date": end_date}), "WORKFLOW": "spotify_backfill"}) time.sleep(60 * 7) # todo set sleep time between launches if count_executions() >= 3: time.sleep(60 * 20) end_date_obj = end_date_obj - timedelta(days=1) skip += 1 end_date = end_date_obj.strftime('%Y-%m-%d')