Prevent Scheduler from starting another task before previous task finishes

Hi Everyone,

Kindly find below query I have;

  • Scala/Akka Application
  • I have a task that processes entries in a batch.
  • Assuming the scheduler runs after every 5 seconds and the current task being executed takes 7 seconds.
    i. Will the scheduler start another instance of the Task before the previous task finishes?
    ii. How can I prevent a scheduler from starting an instance of a task before the previous once completes?

Code samples
val task = new Runnable { def run() { processBatch } }

scheduler.schedule(
initialDelay = 0 seconds,
interval = 5 seconds,
runnable = task)

You could use an actor to executie the task and have the scheduler sending a trigger message to that actor.

In any way, you will end up with a queue of things to be processed as execution of the task takes more time than the scheduler triggers them and that needs some thought (what happens after a crash)

I’d recommend the timers api inside actor https://doc.akka.io/docs/akka/current/actors.html#timers-scheduled-messages

Use startSingeTimer, and start a new after the task has completed.

Since it seems to be long running (blocking?) task you should use dedicated dispatcher.
https://doc.akka.io/docs/akka/current/dispatchers.html#blocking-needs-careful-management

@olger, thanks so much for your valuable feedback. I will definitely look into your suggestion.

@patriknw, thanks so much for your valuable feedback. I will definitely also look into your suggestion.