"""Invoke commit_mechanicals lambda task method.""" from hooks.lambda_hook import OrchLambdaHook from lib import config from lib import constants from tasks.accounting_run_commit import helpers def invoke_commit_mechanicals_lambda_task(dag_run: dict, **kwargs) -> None: """Invoke commit_mechanicals lambda if a run is in a distribution accounting_period. Args: dag_run (dict): the dag's config """ event = helpers.get_event_from_params(dag_run, **kwargs) accounting_run_id = event.target_id task_instance = kwargs.get('task_instance') contract_type = task_instance.xcom_pull( task_ids='contract_type_branch', key='contract_type' ) if not contract_type: accounting_period, _ = helpers.get_event_records(accounting_run_id) contract_type = accounting_period.get('contract_type') if contract_type != constants.CONTRACT_TYPES.DISTRIBUTION: print(f'DO NOT COMMIT MECHANICALS IN {contract_type} ACCOUNTING PERIOD') return commit_mechanicals_event = helpers.create_event( accounting_run_id, constants.DAG_COMMIT_MECHANICALS_EVENT_NAME) hook = OrchLambdaHook(config.COMMIT_MECHANICALS_LAMBDA_NAME) response = hook.invoke_lambda(commit_mechanicals_event.to_json()) if not response.function_response.succeeded: raise Exception(response.error_message)