How to handle Cronjobs for a Docker-based Django app running on a Linux-based Webapp in Azure, using multiple instances

Alexander Lindgren 31 Reputation points
2021-11-19T08:35:42.213+00:00

I have a Docker-based Django application running on a Linux based Webapp in Azure, which can scale from 1-5 instances. I want to run cronjobs which run management commands inside the Django application, but:

  • I can't run it inside the Docker environment, as the cronjobs then will be run multiple times if the Webbapp is scaled over multiple instances.
  • I can't use Azure's Webjobs as it's not yet available for Linux-based Webapps.

The only option I see right now is creating a parallell mirrored Webapp that's restricted to only using one instance from Azure, thus not creating the issue with having cronjobs being run multiple times - but this seems sub-optimal as I need to deploy two web-apps every time I make code change, to have both the "original" application as well as the "cronjobs-application" being run on the same updated version of the codebase.

How can I implement a better solution for this?

EDIT: I've seen that maybe Time triggered Azure functions could work for this, but can I access the codebase that's running in Docker in my Webapp?

Azure App Configuration
Azure App Configuration
An Azure service that provides hosted, universal storage for Azure app configurations.
237 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Pramod Valavala 20,636 Reputation points Microsoft Employee
    2021-11-20T11:28:07.347+00:00

    @Alexander Lindgren The best approach would be to leverage a distributed lock like blob storage leases which is what Azure WebJobs and Functions use for Timer Triggered functions themselves.

    Only one instance of your web app would be able to acquire the lease at a time and would have to renew the lock periodically. If the instance holding the lock goes down (say it crashes), another instance could get the lease and continue processing as required. This would of course require the instances poll the status of the lease periodically as well.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.