-3

I want to design a system that performs certain actions at a predefined time in the future.

i.e. at 2pm on April 10th 2020, do X

I'm looking for patterns that would ensure that the intended action is triggered at the right time & only triggered once.

Ideally, I think it would be best if the action has no knowledge of the scheduling system, and that the scheduling system has little to no knowledge of the action.

Therefore, the action should be idempotent, and handle that itself, then we can, to a certain extent ignore the triggered only once requirement.

The scheduler would run off a data store containing records like:

{
   action: <some way to know the correct action to perform>
   trigger_after: <timestamp>
   triggered: <boolean>
}

Then the scheduler would run every N seconds (however accurate you need it to be)

Then we trigger any jobs that match where trigger_after < now() and triggered = False

Then we update the triggered status when the actions have been dispatched.

What am I missing? What patterns are out there that perform something similar?

  • 1
    Does this answer your question? [Choosing the right Design Pattern](https://softwareengineering.stackexchange.com/questions/227868/choosing-the-right-design-pattern) – gnat Apr 09 '20 at 11:39
  • other than maybe Pattern is not the right term, no. – Guy Bowden Apr 09 '20 at 11:58

1 Answers1

0

Your design works perfectly for events that trigger only once, but if you have repeated jobs (do Y at midnight and midday on every weekday), then you might run into problems.

If you need to support those, you could look into the specification system that cron uses.

Bart van Ingen Schenau
  • 71,712
  • 20
  • 110
  • 179