Scheduling
Pass a TimeSpan or a UTC DateTime when you publish:
// in 15 minutesawait publisher.PublishAsync<RemindAbandonedCart, int>(cartId, TimeSpan.FromMinutes(15));
// at a time (UTC)await publisher.PublishAsync<SendNewsletter, int>(issueId, issue.SendAtUtc);
// or with the static API, without injecting a publisher (see Jobs)await RemindAbandonedCart.PublishAsync(cartId, TimeSpan.FromMinutes(15));A TimeSpan is counted from the publisher’s clock (IJobPublisher.TimeProvider, the system clock unless you register
another TimeProvider). A time in the past runs now. A local DateTime (DateTime.Now, DateTimeKind.Local) is
converted to UTC; one without a kind (DateTimeKind.Unspecified) is taken as UTC.
Where the job waits
Section titled “Where the job waits”In RabbitMQ. The message is published at once, to the queue it will run from, with its delivery time
(x-opt-delivery-time): the quorum queue stores it and delivers it when it’s due. No app holds it in memory, and nothing
polls for due jobs, so:
- a scheduled job survives restarts and deployments of every app;
- it runs in whichever app is consuming its queue then, not in the one that published it;
- it’s on time to the broker’s clock, not the apps’. Keep the broker’s clock synchronised.
Retries work the same way: a failed job is published again with its next retry time, to the same kind of queue.
Which queue
Section titled “Which queue”The publisher decides, from the job’s timeout:
- with a timeout of
LongRunningThreshold(10 s) or more: a long-running queue; - otherwise: an immediate queue.
Jobs are spread round robin over the queues of their kind. See Queues and workers.
Cancelling a scheduled job
Section titled “Cancelling a scheduled job”A published job can’t be taken back. To cancel one, make the job check whether it’s still wanted when it runs (the cart was bought, the newsletter was withdrawn) and return.
Recurring jobs
Section titled “Recurring jobs”For something that runs on a schedule, use periodic jobs, defined in code, rather than a job that publishes itself again.