Skip to content

Support durable schedule in Activity.retry #10849

Open

Description

@thewilkybarkid

What is the problem this feature would solve?

Activity.retry leaves out the schedule option (Omit<Effect.Retry.Options<E>, "schedule">), so a retried activity can't wait between attempts. That's understandable: Effect.retry waits with the in-memory clock. The wait is lost if the process restarts, and a long wait keeps the workflow in memory instead of suspending it.

Backing off between attempts is still common, though, for example when calling an unreliable external API. At the moment every workflow has to build it by hand from CurrentAttempt and DurableClock.sleep:

Effect.gen(function* () {
  const currentAttempt = yield* Activity.CurrentAttempt

  if (currentAttempt === 1) {
    return
  }

  yield* DurableClock.sleep({
    name: `sleep-${executionId}-${currentAttempt}`,
    duration: Duration.min(
      Duration.times(Duration.minutes(10), Math.pow(2, currentAttempt - 2)),
      Duration.hours(12),
    ),
  })
}).pipe(
  Effect.andThen(
    Activity.make({
      name: 'ProcessReceivedReviewRequest',
      error: ProcessReceivedReviewRequestError,
      execute: processReceivedReviewRequest(reviewRequestId),
    }),
  ),
  Activity.retry({ times: 10 }),
)

(https://github.com/PREreview/prereview.org/blob/00e33e18e21dc6b79c985ebc220566cd6484329e/src/Domain/ReviewRequests/Workflows/index.ts#L97-L109)

This repeats the first-attempt check and the clock naming in every workflow, and the backoff policy is written as arithmetic instead of a Schedule.

What is the feature you are proposing to solve the problem?

Let Activity.retry accept a schedule and wait with DurableClock.sleep instead of Clock:

Activity.make({
  name: 'ProcessReceivedReviewRequest',
  error: ProcessReceivedReviewRequestError,
  execute: processReceivedReviewRequest(reviewRequestId),
}).pipe(
  Activity.retry({
    times: 10,
    schedule: Schedule.exponential('10 minutes').pipe(
      Schedule.modifyDelay(({ duration }) => Effect.succeed(Duration.min(duration, Duration.hours(12)))),
    ),
  }),
)

After a failed attempt, Activity.retry would ask the schedule for the next delay. It would then call DurableClock.sleep with a name made from the attempt number (and perhaps a name option). Short delays would still run in memory through the existing inMemoryThreshold, and long ones would suspend the workflow.

The open question is replay. Once a named clock has fired, it returns straight away on replay whatever duration is passed, so jitter in the delay is harmlto retry again is different: if it depends on time or randomness (Schedule.during, for example), replay could follow a different path. Two options:

  • Only support schedules whose decisions depend on the attempt number and input.
  • Record each schedule decision as part of replay reads it back.

What alternatives have you considered?

Activity

  1. added
    enhancement
    on Nov 22, 2026
  2. thewilkybarkid commented on Nov 24, 2026

    @thewilkybarkid
    ContributorAuthor

    We've now needed this in a second place, and doing it by hand there turned up a pitfall that a built-in schedule would avoid.

    Our comment workflows each run two activities in a row and retry each one with backoff. We pulled the pattern into a helper:

    const retryWithBackoff =
      (executionId: string, name: string) =>
      <A, E, R>(activity: Effect.Effect<A, E, R>) =>
        pipe(
          Effect.gen(function* () {
            const currentAttempt = yield* Activity.CurrentAttempt
    
            if (currentAttempt === 1) {
              return
            }
    
            yield* DurableClock.sleep({
              name: `sleep-${executionId}-${name}-${currentAttempt}`,
              duration: Duration.min(
                Duration.times(Duration.minutes(10), Math.pow(2, currentAttempt - 2)),
                Duration.hours(12),
              ),
            })
          }),
          Effect.andThen(activity),
          Activity.retry({ times: 10 }),
        )

    (https://github.com/PREreview/prereview.org/blob/2a0a1e16665ff1fcdc22d9632f1bcc44fb824cf6/src/Domain/Comments/Workflows/index.ts#L35-L56)

    The sleep's name has to include the activity's name as well as the execution ID and the attempt. With only sleep-${executionId}-${currentAttempt}, as in the original example, the second activity's waits would share names with the first activity's, so a retry of the second activity could reuse a sleep that had already finished and not wait at all. Nothing warns about this, so it's easy to get wrong when copying the pattern.

    A durable schedule on Activity.retry could name its sleeps from the activity, in the same way activity attempts already get their own keys, so callers wouldn't need to think about it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions