Workerless outcomes

Decide a job's outcome outside a worker, from an API endpoint, webhook or approval step.

A job's outcome can be decided outside a worker with client.applyJobOutcome: an API endpoint, a webhook or an approval step completes the job, continues its chain or reschedules it, instead of a worker running it. This covers approval workflows, third-party callbacks and jobs that wait for an external event. Deferred start pairs well with this: schedule a job to reject itself after a timeout, and let a user's decision settle it earlier.

An approval request either continues with process-request or ends the chain with a rejection:

src/workerless-outcomes.ts

const jobTypes = defineJobTypes<{
  /*
   * await-approval --+--> (approved) --> process-request
   *                  |
   *                  +--> (rejected or timed out) --> output { rejected }
   */
  "await-approval": {
    entry: true;
    input: { requestId: string };
    output: { rejected: true; reason: string };
    continueWith: { typeName: "process-request" };
  };
  "process-request": {
    input: { requestId: string };
    output: { processed: true };
  };
}>();

Create the request with a deferred start, so it waits two hours before a worker can pick it up:

src/workerless-outcomes.ts

const requestApproval = async (requestId: string) =>
  stateAdapter.withTransaction(async (txCtx) =>
    client.createChain({
      ...txCtx,
      typeName: "await-approval",
      input: { requestId },
      schedule: { afterMs: 2 * 60 * 60 * 1000 },
    }),
  );

The worker handles the timeout case: if nobody decided in time, await-approval runs and rejects the request. It also processes approved requests:

src/workerless-outcomes.ts

"await-approval": {
  attemptHandler: async ({ applyJobOutcome }) => {
    await stateAdapter.withTransaction(async (txCtx) =>
      applyJobOutcome({ ...txCtx, output: { rejected: true, reason: "timeout" } }),
    );
  },
},
"process-request": {
  attemptHandler: async ({ job, applyJobOutcome }) => {
    await stateAdapter.withTransaction(async (txCtx) => {
      processedRequests.push(job.input.requestId);
      return applyJobOutcome({ ...txCtx, output: { processed: true } });
    });
  },
},

The decision settles the job early without a worker, for example from an API endpoint. It reads the chain's tail job with a locked read, so no worker or other caller changes the job before the write, and leaves a request that has already moved past await-approval untouched. Approval continues the chain with process-request, which a worker then runs; rejection completes the chain with an output:

src/workerless-outcomes.ts

const decideApproval = async (chainId: string, approved: boolean) =>
  stateAdapter.withTransaction(async (txCtx) => {
    const job = await client.getChainTailJob({
      ...txCtx,
      chainId,
      chainTypeName: "await-approval",
      lock: "exclusive",
    });
    if (job?.typeName !== "await-approval" || job.status === "completed") return;

    if (approved) {
      await client.applyJobOutcome({
        ...txCtx,
        job,
        continueWith: { typeName: "process-request", input: { requestId: job.input.requestId } },
      });
      return;
    }
    await client.applyJobOutcome({
      ...txCtx,
      job,
      output: { rejected: true, reason: "manager denied" },
    });
  });

This pattern lets you interweave external actions with your chains — waiting for user input, third-party callbacks, or manual approval steps.

How it works

client.applyJobOutcome takes the job and one outcome, the same outcomes as the attempt handler's applyJobOutcome: { output: ... } finishes the chain, { continueWith: {...} } adds the next job, and { reschedule } brings the job back later, as Scheduling explains. The job is either one you read earlier or { id, typeName } when you know it without a read. With output or continueWith, its typeName must name a single job type, which types the outcome; check job.typeName first to narrow a job read from a chain. applyJobOutcomes takes a batch of { job, ...outcome } items and applies them all or none.

applyJobOutcome locks the job and validates it as stored (a continueWith outcome first locks the head jobs of its blocker chains), and applies any outcome to a blocked, pending or running job: which jobs to act on is yours to decide, from a locked read and the job's status. It throws JobNotFoundError if the job does not exist, JobAlreadyCompletedError if it is completed, JobTypeMismatchError if typeName differs from the stored job's, and JobStatusMismatchError if you pass a job read earlier whose status has changed since. applyJobOutcomes validates every item before the first write and throws JobsNotFoundError, JobsAlreadyCompletedError, JobsStatusMismatchError or JobsTypeMismatchError listing every offending id; the validation, blocker, unknown job type and job ID errors it throws are the batch variants too, each listing every offending item. Both require a transaction context. The Redis state adapter keeps workers off a locked job for at most 60 seconds and makes a second caller run its transaction again instead of waiting; see Locks. The completed job has completedBy: null, distinguishing it from worker-completed jobs.

If a worker is already processing the job when client.applyJobOutcome applies an outcome to it, its attempt is lost. The handler's job.signal aborts when the notify adapter delivers the attempt-lost notification, and its applyJobOutcome, when it runs, writes nothing and throws, so the handler's transaction rolls back: JobAlreadyCompletedError after output or continueWith, and JobTakenByAnotherWorkerError after reschedule, which returns the job to pending. If the handler is inside a fenced transaction, client.applyJobOutcome waits for it to end, and throws JobAlreadyCompletedError if that transaction continued the chain, while a locked getChainTailJob that waited returns the continuation; on Redis, the one that commits second runs again instead.

The runnable code is in examples/guides/src/workerless-outcomes.ts. Related: Tx hooks and Scheduling.

esc

Search guides, integrations and every exported symbol.

↑ ↓ to navigate↵ to open