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
- Removing the @Scheduled annotation
- Adding a HTTP endpoint (controller method) that exposes the functionality of the task
- Scheduling this endpoint to be called every minute using Azure Logic Apps (I also considered using WebJobs, but this is not yet supported for App Service on Linux).
This seems like a lot of additional complexity, and am wondering if there's a simpler solution?