Continuations
A continuation is a job that starts when another job has finished. It can have continuations of its own:
var import = JobInfo.For<ImportFile, int>(fileId);
import.ContinueWith(JobInfo.For<RebuildReports>()) .ContinueWith(JobInfo.For<SendReport>());
await publisher.PublishAsync(import);ContinueWith returns the continuation, so a chain reads in order: the import, then the reports, then the mail. Publish
the first job with IJobPublisher; the continuations go with it.
The first job can be scheduled. Its continuations wait with it, and start when it has finished:
var import = JobInfo.For<ImportFile, int>(fileId, DateTime.UtcNow.AddHours(2)); // runs in 2 hours
import.ContinueWith(JobInfo.For<RebuildReports>()); // runs once the import has finished
await publisher.PublishAsync(import);Conditions
Section titled “Conditions”By default a continuation runs if the job succeeded. It can also run on failure, or either way:
var import = JobInfo.For<ImportFile, int>(fileId);
import.ContinueWith(JobInfo.For<RebuildReports>());import.ContinueWith(JobInfo.For<AlertOps, int>(fileId), JobContinuation.OnFailure);import.ContinueWith(JobInfo.For<CleanUpUpload, int>(fileId), JobContinuation.OnFinish);| Condition | Runs when the job |
|---|---|
OnSuccess (default) |
succeeded |
OnFailure |
failed |
OnFinish |
finished, whether it succeeded or failed |
- A job has finished when it succeeded, or when it failed and went to the failed queue: out of retries, or a message that couldn’t be read. A failed attempt that will be retried doesn’t count: a job that succeeds on its second attempt succeeded, and its continuations start then.
- A continuation whose condition isn’t met is skipped, logged and counted (
massivejobs.continuation.skipped). A skipped continuation counts as failed, so in a chain ofOnSuccesscontinuations a failure skips the rest of the chain, and anOnFailurecontinuation further down still runs.
Delays
Section titled “Delays”A continuation can wait after its job has finished:
var import = JobInfo.For<ImportFile, int>(fileId);
import.ContinueWith(JobInfo.For<SendReport>(), delay: TimeSpan.FromHours(1)); // an hour after the importimport.ContinueWith(JobInfo.For<AlertOps, int>(fileId), JobContinuation.OnFailure, TimeSpan.FromMinutes(5)); // 5 minutes after it failed- The delay counts from the job’s finish, not from its publish: a job that waits for retries delays its continuations too.
- In a chain, each delay counts from its own job. In import → (1 hour) → report → (10 minutes) → mail, the mail goes out 10 minutes after the report has finished.
- The broker holds the continuation until then, like a scheduled job. A delay is in whole milliseconds (rounded), at most ten years; zero runs it at once.
How it works
Section titled “How it works”There’s no state to keep anywhere: a continuation travels inside its job’s message, and in the messages of its retries. When the job has finished, the worker that ran it publishes the continuations whose condition is met, then acknowledges the job. A continuation runs as an ordinary job, in the immediate or long-running queues by its timeout, on any app’s worker.
- Register every job in every app that runs jobs, continuations included. The apps share the queues, so a
continuation can reach any of them. An app that doesn’t register it delays it for one that does, and after
UnknownJobTimeoutsends it to the failed queue, as with any job (see Unknown jobs). The app that publishes the job needs its continuations registered too. - The continuation is published as it was written, from the tag and the JSON the job’s message carries. A job never fails because its app doesn’t know one of its continuations.
- At least once. If publishing the continuations fails, the job goes back to its queue and runs again.
- A continuation that can’t be read (a corrupt message) goes to the failed queue as it is, with the reason; its job is done.
- A job requeued from the failed queue (
IFailedJobs.RequeueAsync) starts over without its continuations: they ran, or were skipped, when it failed. - Retries per job apply to continuations like to any job (see Retries of one job).
Limits
Section titled “Limits”- Continuations start when their job has finished, or a delay after it. A continuation can’t have a run time of its own (the first job can), and a run of a periodic job can’t have continuations.
- Add continuations before publishing the job. They’re part of its message; a published message can’t change.
- The default serializer only. An app that replaces
IJobSerializercan’t publish continuations. - Upgrading from 1.0: a 1.0 app ignores continuations. If it runs or retries a job with continuations, they’re lost. Publish continuations once every app that runs jobs is on 1.1.