0

I'm revising one of our company's custom operator in a way so that it logs its activity to a database. As part of that, I generate a unique number for use in initially populating the table row before the task runs, and then use that same number to update the row with the results of the task run.

I leaned towards using callbacks to handle this logging functionality (it isn't hard to pass my own functions to the super constructor). The on_execute_callback function would generate the unique number and insert to the logging table, and either on_success_callback or on_failure_callback would update that row using the unique number.

The only problem is that I'm not sure how to pass the value generated in on_execute_callback to execute or any other callback. The params object passed within the functions' context parameter is specific to the task, not the task instance. I've looked around in the airflow.models.taskinstance module, but there isn't anything obvious about storing user data.

Is there a method I'm missing here for passing task-instance-specific data throughout the various methods? Is there a better way I should be doing this instead?

  • You could update the [`context` dictionary](https://github.com/apache/airflow/blob/3d96ad62f91e662b62481441d8eec2994651e122/airflow/models/taskinstance.py#L1395) passed to the `on_execute_callback`. – Oluwafemi Sule Apr 20 '22 at 09:10
  • The same context dictionary is used for `pre_execute`, `post_execute`, `on_execute_callback`, and `execute()` itself. However, `post_execute` can't seem to access whether or not the task succeeded or not (the `status` of the task instance isn't updated until after it is called). Only `on_failure_callback` and `on_success_callback` contain this data. The problem is that a [new `context` dictionary](https://github.com/apache/airflow/blob/70eede5dd6924a4eb74b7600cce2c627e51a3b7e/airflow/models/taskinstance.py#L1720) is generated for these two , meaning that any data I pass in context is lost. – RocketeerReporter Apr 20 '22 at 15:55
  • Have you considered pushing an xcom value for the task instance in the on_execute_callback for the unique identifier? – Oluwafemi Sule Apr 25 '22 at 14:29
  • I haven't yet. I'm looking into it now. – RocketeerReporter Apr 26 '22 at 16:57

0 Answers0