Pausing workflow execution

Hi,

I am new to Temporal.
I am building a webapp where users can pause/unpause workflow execution.
Is there a built in way to do this in temporal? E.g a single command to the scheduler?
Or do I need to implement this myself using “pause” checks within my flow?

Currently there is no system level “pause”. We plan to add one in the future.

So the pausing should be part of your workflow logic. You can use a signal or update to ask workflow to pause/unpause.

Thanks!

Is there an example implementation in Go/TS?

Depends how granular you want the pause to be.

To pause a scheduled activity would be more involved, the activity code would need to query the workflow and check if it should be running and fail, which get complicated when considering the retry policy.

The simplest version of pause would be to simply wait until the workflow is unpaused before scheduling the next activity.

import * as workflow from '@temporalio/workflow';
import type * as activities from './activities';

const pauseSignal = workflow.defineSignal<[boolean]>('pauseToggle');
const { foo, bar } = workflow.proxyActivities<typeof activities>({ /* options */ });

export async function myWorkflow() {
  let paused = false;
  workflow.setHandler(pauseSignal, (st) => { paused = st });
  await workflow.condition(() => !paused);
  await foo();
  await workflow.condition(() => !paused);
  await bar();
}

You can also implement this via interceptors and avoid leaking into workflow logic:

export function interceptors(): workflow.WorkflowInterceptors {
  let paused = false;
  workflow.setHandler(pauseSignal, (st) => { paused = st });
  return {
    outbound: [
      {
        async scheduleActivity(input, next) {
          await workflow.condition(() => !paused);
          return next(input);
        },
      },
    ],
  };
}

Follow this sample to see how to install the interceptors: samples-typescript/interceptors-opentelemetry at f16fa9d4962c31231ea63c0a7c7dada3c52a840a · temporalio/samples-typescript · GitHub

To expand on this, you could also set a query handler that reports whether the workflow is currently paused and pause child workflows and other workflow APIs.

2 Likes