"""Terminate active SWF executions from command line.""" import sys from concurrent.futures import ThreadPoolExecutor from datetime import datetime import boto3 session = boto3.session.Session(profile_name='default') client = session.client('swf') def get_workflow_types(): """Get film transparency ETL workflows.""" response = client.list_workflow_types( domain='dev', registrationStatus='REGISTERED' ) etl_types = response['typeInfos'] ft_etls = [] for etl in etl_types: name = etl['workflowType']['name'] if name.startswith('ows_ft_etl_'): ft_etls.append(name) return ft_etls def get_list_of_workflows_by_name(ft_etls): """Get active film transparency ETL workflows.""" open_workflows = [] etl_count = len(ft_etls) futures = [] with ThreadPoolExecutor(max_workers=etl_count) as exc: for etl_name in ft_etls: kwargs = { 'domain': 'dev', 'startTimeFilter': { 'oldestDate': datetime(2018, 1, 1) }, 'typeFilter': { 'name': etl_name } } futures.append( exc.submit(client.list_open_workflow_executions, **kwargs)) for future in futures: open_workflows.extend(future.result()['executionInfos']) return open_workflows open_workflows = get_list_of_workflows_by_name(get_workflow_types()) if not open_workflows: print('No active SWF workflows.') sys.exit() for i, workflow in enumerate(open_workflows): print(i, workflow['execution']['workflowId']) workflow_index = input( 'Select index of workflow to terminate (press e to exit): ') if workflow_index == 'e': print('Exiting...') sys.exit() workflow_index = int(workflow_index) workflow = open_workflows[workflow_index] workflow_id = workflow['execution']['workflowId'] client.terminate_workflow_execution( domain='dev', workflowId=workflow_id ) print('Workflow {} was terminated.'.format(workflow_id))