We have a custom command in a django app that performs synchronization of data with an external service.
The command is started hourly.
Usually, the command is finished within half an hour or less, but recently, we ran into a situation where the process took several hours. In the meantime, the command was started several times again in the background, causing inconsistent access to the models (because our code was not designed for this situation).
Is it possible to prevent django from running the command if it is already running?
One way I think of solving this problem is to use a file as a mutex for the command.
But this does not seem very elegant to me, as it could cause any amount of extra trouble in case the command gets interrupted and the file might not be cleaned up properly.
What is the best way to approach this problem? Is there a pythonic / django-ish way to do this?