Processes in ppacer can be either triggered manually or run on a schedule.
Schedules are defined via any Go type which satisfies the following interface
from
ppacer/core/dag/schedule
package:
As we can see, a schedule is any type that can indicate when it starts via the
Start method. It can also determine when the next execution is going to be
scheduled based on the current time and the previous scheduled execution time
using Next method. The String method provides a way to serialize the
schedule name or highlight. Mostly to keep that information in the database and
to show it on the UI.
Standard ppacer/core/dag/schedule package provides implementation for
commonly used schedules.
Fixed interval schedule
Possibly the simplest regular schedule is fixed interval schedule, which starts
at given time and ticks every fixed time duration.
There are also few helper schedules based on Fixed schedule -
schedule.EveryMinute, schedule.Hourly and schedule.Daily. More details
can be found in API reference for ppacer/dag/schedule package.
cron
The second example is based on iconic *-UNIX scheduler
cron. Cron uses syntax composed of five
elements representing schedule expression:
Cron’s schedule expression format was influential on modern schedulers like
Apache Airflow and Dagster where this format is supported or even used as
default way of defining schedules.
In ppacer cron expressions are implemented via schedule.Cron type in
ppacer/core/dag/schedule package. Instead of using string, to initialize cron
expression, you can use fluent API. For example, it can looks like the
following:
By default schedule.NewCron() return default schedule * * * * * which ticks
every minute.
Custom schedule
There are cases when cron or other regular schedules aren’t a good fit for
defining a schedule for our processes.
Simple custom schedule
Let’s say we have a process that needs to run on Friday afternoon (6 pm), just
before the weekend, and then on Monday morning (8 am), just after the weekend.
Such simple schedule cannot be expressed neither by cron, nor by fixed interval
schedule. Let’s try to implement this schedule.
We can easily implement this schedule given we can use previous schedule point.
That’s usually on the hot path, because we always has previous schedule point,
except for the first point. Calculating Next for the first point is also
fairly straightforward:
As a really quick test we can try to generate few time points from that
schedule. Let’s start at 2024-03-31 13:30 (Sunday):
Cron modification
Now we try to define a bit more complex scheduling logic depending on cron
schedule modifications. Let’s say our DAG needs to run on everyday at 10:15 am
but in June and August it shouldn’t run on Fridays and on Christmas eve it
should run at 08:00 am.
In the above code we defined MyCustomSched type which uses regular
schedule.Cron schedule and additional custom scheduling logic in Next
method to reflect our specific schedule implementation. The whole code snippet
is complete working Go program including example uses of MyCustomSched
schedule for several dates.