1

I have a Spring Boot microservice deployed in Azure that is supposed to be running on a fixed rate with the @Scheduled Spring Annotation. When I run it locally, it performs exactly as expected. When deployed in Azure, it seems to be a mixed bag as to when it will run as scheduled.

During off-peak hours (~00:00 - 8:00AM) it seems to work as scheduled with a little variation here and there. However, during peak business hours (12:00PM - 18:00PM) the scheduled times can vary DRASTICALLY.

A service that should be running once every minute will run potentially every 5 minutes during this time. It's required that the service stay up and running (can't just kick off the service anew when scheduled), it has a list of customers that it loops through (the list is fetched from a DB whenever it first starts or gets through the list). It works a certain number of customers every time it is scheduled and moves on to the next fixed set of customers until it goes through them all and then starts the process anew.

Is this due to throttling during peak hours? Does anyone know of a good way to keep my service firing on its schedule, or an Azure alternative to the @Scheduled annotation?

Thanks

1 Answers1

0

Use fixedRate

fixedRate : makes Spring run the task on periodic intervals even if the last invocation may still be running.

fixedDelay : specifically controls the next execution time when the last execution finishes. In code:

@Scheduled(fixedDelay=5000)
public void updateEmployeeInventory(){

}    

@Scheduled(fixedRate=5000)
public void updateEmployeeInventory(){

}

I've got the answer from there How wait @Scheduled till previous task is not finished?

  • Thanks for the reply! I am using a fixedRate, specifically passing in a fixed rate string : ```@Scheduled(fixedRateString = "${scheduled.rate.in.milliseconds}")``` Got it from the Spring documentation: https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/scheduling/annotation/Scheduled.html – Joseph Dunavan Jul 01 '21 at 18:20
  • Don't your task start yet or just performs a very long time? – Дмитрий Морозов Jul 01 '21 at 18:24
  • It appears that the task won't start reliably. Once it has finally started then it will run as quickly as I expect it to. This issue only presents itself in Azure, not on my local machine. – Joseph Dunavan Jul 01 '21 at 18:33
  • You can try to add @Async annotation above the method. Because all Schedulers perform on the same thread. – Дмитрий Морозов Jul 01 '21 at 18:41
  • That's a good suggestion! I found my issue. The final operation in my scheduled task is to perform an API call. The host is holding on to my request for too long and is timing out during peak hours. I suppose they can't handle the volume. Like you suspected, since they are holding on to the call the scheduler can't continue on the same three. Unfortunately making it async won't help, I don't think. If the host can't handle the load, making it async will make the schedule run but the request still won't complete successfully. All well, I'll talk to the host and see if they can fix it! – Joseph Dunavan Jul 01 '21 at 20:35