"""Invoke the reserves_schedule lambda task method.""" from hooks.lambda_hook import OrchLambdaHook from lib import config from lib.constants import CONTRACT_TYPES from lib.constants import DAG_SCHEDULE_RESERVES from tasks.accounting_run_commit import helpers def invoke_reserves_schedule_lambda_task(dag_run: dict, *args, **kwargs) -> None: """Invoke reserves_schedule lambda if a run is in a distribution accounting_period. Args: dag_run (dict): a 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 != CONTRACT_TYPES.DISTRIBUTION: print(f'DO NOT SCHEDULE RESERVES IN {contract_type} ACCOUNTING PERIOD') return schedule_reserves_event = helpers.create_event( accounting_run_id, DAG_SCHEDULE_RESERVES ) hook = OrchLambdaHook(config.RESERVES_SCHEDULE_LAMBDA_NAME) response = hook.invoke_lambda(schedule_reserves_event.to_json()) if not response.function_response.succeeded: raise Exception(response.error_message)