enragedginger / akka-quartz-scheduler

Quartz Extension and utilities for cron-style scheduling in Akka

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Scheduler triggers job on start

estsauver opened this issue · comments

Hi there,

I'm seeing an issue where a job is triggered on initialization.

  val scheduler = QuartzSchedulerExtension(system)
  scheduler.schedule("EverySunday", treatmentRunner, Run)
  scheduler.schedule("EveryNight",
                     lotteryNotificationRunner,
                     Run,
                     startDate = Some(new DateTime("2017-05-26T00:00").toDate))

  scheduler.start()

This is what the scheduling looks like:

  quartz {
    schedules {
      EverySunday {
        description = "a job that fires every Sunday"
        expression = "0 0 17 ? * TUE" // 5 PM on sunday
        timezone = "Africa/Nairobi"
      }
      EveryNight {
        description = "Every night at 7pm"
        expression = "0 0 19 * * ?"
        timezone = "Africa/Nairobi"
      }
    }

There's no other place that could be sending this message to this actor. I'm a little confused as to why the "LotteryNotificationRunner" is executing on startup.

This seems to only occur when a startDate is specified.

Hi @estsauver, the startAt date/time functionality simply specifies the earliest time at which the trigger may fire. The date you have listed, May 26th, is hard coded and is three days before you posted this ticket. My assumption is that because that date is in the past, and your EveryNight trigger would have fired multiple times since then, Quartz is firing off the message on startup. Typically, the startAt functionality is only used to specify dates or times in the future.

Does that help?

@estsauver What happens if instead of hardcoding May 26th as the date, you set the startAt value to something like new DateTime().minusHours(1)? Does the issue still occur?

Hey @enragedginger,

That makes sense. The case we were using this for was launching a campaign on a certain day. We didn't realize that every time we deployed the app and restarted after this day, it would trigger again.

Basically, as it is now, this functionality makes software function in a totally expected way before the start date, and then any restarts after trigger that functionality again. For anything that's somewhat expensive, this makes akka-scheduler quite difficult to use. Imagine if this task was "rebuild production in ec2" or "execute a trade of 100$".

The semantics of this are just wrong. When I say "Execute this function every day at midnight, starting after June 1" and it executes 12 times today, that is really broken.

Yeah, I can see how that would be frustrating. Unfortunately, this is baked into Quartz which we're merely wrapping. For your use case, I recommend comparing whatever your hardcoded date is with the current date/time and using whichever of the two is later. Something like this should do the trick:

val riiiiiiiightNow = new DateTime()
val startAt = if (thatHardCodedDate.isAfter(riiiiiiiightNow)) thatHardCodedDate else riiiiiiiightNow

Does that work for you?

Closing due to inactivity.