"""Helper tasks which check concurrent possibility of the flow. Apple Music streams and iTunes can't run in the same time because of the apple id mapping process. But we can launch these etls for different licensors. """ from feed_ingestion.util.aws.swf import get_names_of_running_workflows_by_type # list of etls which it is needed to check for possibility to run concurrently nonconcurrent_workflow_types = [ 'apple_music_feed_ingestion', 'itunes_feed_ingestion'] def check_concurrent_status(domain, licensor, max_concurrent_executions): """Check if there is any active apple or itunes executions for a licensor. Args: domain (str): The flow domain (f.e. prod_feed_ingestion). licensor (str): The licensor to ingest. max_concurrent_executions (int): Max concurrent executions which is allowed for pair flow and licensor. Returns: dict: {} or STOP_RESPONSE from check_ingested_status decorator. """ def get_flow_licensor(full_flow_name): """Return licensor name from full flow name execution. Example of flow name execution with licensor - apple_music_feed_ingestion_sme-2020-05-12. """ return full_flow_name.split('-')[0].split('_')[-1] # get all full flow names by domain and flow_type result = [] for flow_type_name in nonconcurrent_workflow_types: result.extend( get_names_of_running_workflows_by_type(domain, flow_type_name)) # count only executions which belong to this licensor count_of_active_flows = len([ get_flow_licensor(full_flow_name) for full_flow_name in result if get_flow_licensor(full_flow_name) == licensor]) # There will be also the current execution if count_of_active_flows > max_concurrent_executions: return {'stop': True}