Skip to content

post_work#

After a task has been completed, the worker calls the post_work method. This method allows the user to perform an operation after each task is completed. You can use it to close an APM transaction or to send a metric.

Definition#

1
2
3
4
5
6
def post_work(
    self,
    task: sergeant.objects.Task,
    success: bool,
    exception: typing.Optional[BaseException],
) -> None

Examples#

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
def post_work(
    self,
    task,
    success,
    exception,
):
    self.my_logger.debug(f'stopped working on {task.kwargs["url"]}: {time.time()}, exception: {exception}')

    if exception is not None:
        self.apm_client.capture_exception()

    self.apm_client.end_transaction(
        result='success' if success else 'failure',
    )