In my Spring Boot app I have a scheduled task that runs every minute. It runs some queries to find any notifications that are due, and then sends them by email
@Service
public class EmailSender {
@Scheduled(cron = "0 * * * * *")
public void runTask() {
// send some emails
}
}
It works fine when I test it locally, but when I deploy it to Azure, 2 copies of each email are sent. This is because in Azure we run (at least) 2 containers, both of which have the scheduling enabled.
I looked for a per-container environment variable, which the scheduler would check, and only run the job if this variable is set to "true", but couldn't find any reliable way to achieve this.
I'm now considering the following instead
This seems like a lot of additional complexity, and am wondering if there's a simpler solution?
Spring, by default, cannot handle scheduler synchronization over multiple instances – it executes the jobs simultaneously on every node instead. So there are two possible solutions:
You can use Spring ShedLock to handle this though. Here there is a guide you can use: Spring ShedLock. You can find all the code need to implement this lock there.
It works by using a parameter in a database, that gives the information to the other nodes about the execution status of that task. In this way ShedLock prevents your scheduled tasks from being executed more than once at the same time. If a task is being executed on one node, it acquires a lock which prevents the execution of the same task from another node.
You can find more info in their Github Page about how to handle it with different databases.
You can create two different profiles, for example, one is prod
and the other is cron
. Then you can put the annotation @Profile("cron")
on scheduler classes, so that they will be ran only on the second profile.
Then you have to build the application twice:
-Dspring.profiles.active=prod
and you deploy it normally.-Dspring.profiles.active=prod,cron
, and you have to deploy this in
an environment that is not autoscaling so that you can be sure there
will be only one instance of this.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With