Skip to content

Periodic jobs

Put [PeriodicJob] on a job class, and every app that registers the class runs it on that schedule. One app runs each run, however many apps define it.

// every 5 minutes, and every day at 06:00 in Sarajevo
[PeriodicJob("cleanup.sessions", 300)]
[PeriodicJob("report.daily", "0 0 6 * * ?", TimeZone = "Europe/Sarajevo")]
public class Housekeeping(AppDbContext db) : JobAsync<Housekeeping>
{
public override Task Perform(CancellationToken cancellationToken) => ...;
}

There’s nothing to publish: a periodic job starts when the first app that defines it starts.

id The periodic job’s name: the same in every app, unique among the app’s periodic jobs.
cronExpression Six fields, with seconds first: second minute hour day-of-month month day-of-week. 0 */15 * * * * is every 15 minutes, 0 0 3 * * MON-FRI 03:00 on weekdays.
intervalSeconds Instead of a cron expression: runs at multiples of the interval since 1970-01-01 UTC, so 60 runs at every full minute and 3600 at every full hour.
TimeZone The cron expression’s time zone: UTC if not set. IANA (Europe/Sarajevo) or Windows ids on .NET; only Windows ids on .NET Framework.
Config A string passed to the job’s argument. The job then takes a string: JobAsync<TJob, string>.
TimeoutMs The job’s timeout, as when publishing it. 10 s or more runs it in the long-running queues.
DisableConcurrentExecution Skips runs while the job of an earlier run hasn’t finished. Needs a TimeoutMs. See Runs that don’t overlap.

A class can have several attributes, e.g. one per config:

[PeriodicJob("sync.eu", "0 */10 * * * *", Config = "eu")]
[PeriodicJob("sync.us", "0 */10 * * * *", Config = "us")]
public class SyncRegion(IRegionSync sync) : JobAsync<SyncRegion, string>
{
public override Task Perform(string region, CancellationToken cancellationToken) => sync.RunAsync(region, cancellationToken);
}

Invalid attributes (a bad cron expression, an unknown time zone, a Config on a job without a string argument, a duplicate id) fail at startup.

In a time zone with daylight saving time, the expression matches the zone’s local times, like Hangfire and Quartz.NET:

  • Clocks go forward: the local times they skip run once, at the end of the gap. A job at 02:30 runs at 03:00 that day.
  • Clocks go back: a local time that occurs twice runs the first time. If the expression has an interval in its seconds, minutes or hours (*/15, 1-3), it runs both times, so the interval goes on: 0 */30 * * * * still runs every 30 minutes.

Each run is a message in the periodic queue, delivered by RabbitMQ at the run’s time. The app consuming the queue publishes the next run, then the job (an ordinary job, retried if it fails), then acknowledges the run. Between runs no app holds anything.

  • One consumer at a time. The periodic queue has a single active consumer: of the apps that define periodic jobs, one gets the runs, and another takes over if it goes away.
  • Same run times everywhere. Run times depend only on the schedule, never on when an app started, so every app computes the same ones. Every app with workers also checks every minute (PeriodicSeedInterval) that the next run of each of its periodic jobs is in the broker, and publishes it otherwise. The copies this makes have the same time, and the consumer drops all but one.
  • Late runs run once. If no app ran for a while, the first run that comes due runs; the runs missed meanwhile are skipped, not caught up.

A run can happen twice after the consumer fails over (it remembers the runs it handled in memory), like any job: periodic jobs should be idempotent too. See Guarantees.

A run publishes the job whether or not the job of the run before it has finished. A report every 5 minutes that once takes 12 minutes runs alongside the next two. With DisableConcurrentExecution, those runs are skipped instead:

[PeriodicJob("report.orders", "0 */5 * * * *", DisableConcurrentExecution = true, TimeoutMs = 30 * 60_000)]
public class OrdersReport(ReportBuilder reports) : JobAsync<OrdersReport>
{
public override Task Perform(CancellationToken cancellationToken) => reports.BuildAsync(cancellationToken);
}

The job still runs in the immediate or long-running queues, on any app’s worker. When it has finished, it reports back to the periodic queue. Until then, the runs that come due publish only the run after them.

  • Finished means succeeded, or out of retries. A job that failed and waits for a retry hasn’t finished: the runs stay skipped until the retry succeeds, or the job goes to the failed queue.
  • The timeout is required. If the report is lost, the job counts as finished once all its attempts could have run: each attempt’s timeout, plus the delays between the retries (about three weeks with the default 25 retries).
  • Stop on cancellation. When a job runs past its timeout, the worker stops waiting for it, and it counts as failed. A job that ignores its cancellation token then runs on, and can overlap the next run.
  • Failover. Each run carries until when the job may still run, so an app that takes over the periodic queue skips the same runs. It can skip one run too many, if the job’s report went to the app before it.

A periodic job’s version is a hash of its tag, schedule, config, timeout and DisableConcurrentExecution. Change the attribute and you have a new version: once the new code is deployed, runs of the old version are dropped when they come due, and the chain continues with the new one.

During a rolling deployment the old and new apps drop each other’s runs; the chain comes back within a minute (PeriodicSeedInterval) once one version is left. Runs in that window can be skipped.

To stop a periodic job, remove its attribute. Its pending run is dropped when it comes due.