Search code examples
azureazure-batch

Azure batch service add webhook on job completion


I've set up a batch service for media file encoding with ffmpeg. Each job can contain multiple tasks, each task will encode one file. I use the task specific resource- and output-file system, so the batch service automatically fetches and delivers the files from and to the blob storage.

However: how do I know that a job or task has completed or failed? Since the job can take very long - even more so on low priority nodes - I need some sort of webhook or event. Continuous polling on the job status is not viable.

The options I could think of:

  1. after running the ffmpeg command, connect a curl command. Something like:

"commandLine" : "/bin/bash -c "ffmpeg -i inputFile outputFile && curl https://my-webhook-receiver.org ""

Technically it works, but I'm worried about timing. The curl request is probably(?) done before the batch service pushes the result file back to the blob storage. If it's a big file, and it takes maybe half a minute to upload, I will get notified before the file exists on the output container.

  1. Use the blob storage event system. This has the advantage that the result file obviously must've have arrived. However, what if the job failed? It won't get triggered ever then...

  2. Batch alert system. You apparently can create alerts for certain batch events (e.g. task completion) and you can hook it up to an action group and finally a webhook. Is that the right call? It feels kinda hacky and not the right way to use this system.

Isn't there a way to connect azure batch with e.g. azure event grid directly? What is the "correct" way to let my server know, the encoded file is ready?


Solution

  • There are a few ways to handle this, although admittedly some of these solutions are not very elegant:

    1. Create a task dependency on each task. The dependent task is the one that invokes the webhook. You can make it such that the dependent task is invoked even if the task it is depending on fails with certain exit codes. You can also create a "merge task" that is dependent on all tasks in the job that can let you know when everything completes.
    2. Use a job manager task instead. Job managers are typically used to monitor progression of a workflow and spawn other tasks, so you would be able to query status of task completion (success or failure) and send your webhook commands via this task or a task spawned by the job manager.
    3. Use job release mechanisms to run actions when a job completes. This does not solve your per-task notification problem, but can be used as a job completion signal.