Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Schedule a job for the first day of each month

I need to schedule a task for the first day of each month. Up until now, I have been using this:

system.scheduler.schedule(0.microseconds, 30.days, schedulerActor, "update")

But as you may have guessed it, this ends up sometimes running the task twice a month (march) or none a month (february). Is there a better way to schedule the task for the first day of each month using Akka Scheduler?


1 Answers

Built-in Akka scheduler is more a delayer than a scheduler. I would recommend using akka-quartz-scheduler. This module allows you to actually schedule tasks to run when you want.

The usage is simple. Some config:

akka {
  quartz {
    schedules {
      YourScheduleName {
        description = "A cron job that fires off every first of the month at 5AM"
        expression = "0 0 5 1 1/1 ? *"
      }
    }
  }
}

And then in the code:

case object Tick
val yourActor = system.actorOf(Props[YourActor])
QuartzSchedulerExtension(system).schedule("YourScheduleName", yourActor, Tick)
like image 113
hveiga Avatar answered Nov 19 '25 09:11

hveiga



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!