My project uses playframework and runs scheduled task every 5 minutes. It works fine when there is only one application.
However, when i use nginx to configure 2 play applications. Both the 2 play applications run scheduled task.
How to only run one scheduled task in these 2 play applications?
I see three solutions for this problem:
Use an external service to make the scheduling (like a cron tab). Every 5 mins, the external service calls a private endpoint of your application and one application will run the task
Use an external db (like a sql database, zookeeper...) to have a distributed lock where only one application can get the lock at a given time. When the other tries to get the lock to run the task, the task is aborted since the lock is already taken.
Use akka cluster capabilities to create a distributed scheduler. For this you can use the sharding capabilities so that only one node will treat the job messages.
Depending on what you are already using, the first two solutions may be simpler to implement. With akka, you might need to dive in a lot of new things to achieve this.