Using Azure Batch, my project adds jobs to a pool using an event based design with functions and queues. When the job is finished, it is still "active", even though all tasks are completed.
A (single using an app service plan) function is triggered on a timer which reads an X amount of messages from the queue. The function:
- Creates a pool (if it does not exist)
- Creates a job
- Adds the tasks to that job
That works good. However, once the tasks have finished, the job status remains active, even though all tasks have finished. I want the jobs to terminate/cleanup/set the status to "completed".
And I want my functions to be short-lived and do not want any statefullness. So I am not using foreach (CloudTask task in job.CompletedTasks())
to await the status of the tasks.
Another approach is to use task dependencies, which require batchClient.Utilities.CreateTaskStateMonitor()
and thus a statefull approach.
What is the best way to use Azure Batch in an event based design? And specifically, how to terminate/cleanup the jobs once the tasks are finished?
Final solution with code after fpark's answer:
If you try to create a job with
OnAllTasksComplete.TerminateJob
directly, you will receive the following error:So set the
job.OnAllTasksComplete
when all tasks have been added.It takes around two minutes (in my case) for the job to set it's status to
Completed
after all the tasks are completed.You can have the job "auto complete" once all tasks complete under the job. There is a property called OnAllTasksComplete on the CloudJob object.
You will want to initially set this property to
NoAction
(the default), while you are adding tasks to the job. After you have added all the tasks to the job, you can update that value toTerminateJob
and then callCommit()/CommitAsync()
. Note that if you retain the CloudJob that you initially submitted, you will need toRefresh()/RefreshAsync()
first before modifying the properties and committing. Alternativley you canGetJob()/GetJobAsync()
, modify, then commit.For event-based designs, you can take a look at enabling Batch service analytics and see if that is appropriate for your scenario.