Skip to content

ClusterCron: on Sharding a failing or dying body is run again and again for the same slot #10964

Done

Description

@RhysSullivan

Note: everything below was written by an AI agent working on the author's behalf and may not have been reviewed by a human.

This is AI-generated; I don't have strong opinions on the API or developer experience and am glad to take whatever shape you prefer, but I do need the functionality.

What happens

With the default calculateNextRunFromPrevious: false, a ClusterCron whose body fails (or dies) on Sharding / SingleRunner runs its body again and again for the same slot. It does not run once, log, and move on. Each failing slot keeps being retried at the entity manager's defect backoff, beside the later slots, for as long as skipIfOlderThan allows (one day by default).

Measured on main (db1629c626), Node 24.22.3, with the script below: a cron every 10 seconds whose body always fails, run for 45 seconds.

+6.1s 11:06:40  +6.6s 11:06:40  +7.3s 11:06:40  +8.4s 11:06:40  +10.1s 11:06:40  +12.7s 11:06:40
+16.1s 11:06:50 +16.5s 11:06:40 +16.6s 11:06:50 +17.4s 11:06:50 ...
+40.7s 11:06:40 +40.8s 11:06:50 +42.1s 11:07:00 +42.6s 11:07:10
runs per slot: 11:06:40 → 10, 11:06:50 → 9, 11:07:00 → 8, 11:07:10 → 6

The 11:06:40 slot is still being run 34 seconds after it was due. A body that dies (Effect.die) behaves the same (9, 8, 7, 5 runs in 35 seconds). On a minutely cron, the same thing gives 16 runs in the first slot and 10 in the second. That was measured on 4.1.4 and 4.1.6, in a Durable Object running SingleRunner with @effect/sql-sqlite-do.

With calculateNextRunFromPrevious: true the slot is retried in place and later slots wait. That is what its documentation says ("A run that fails is retried in place and blocks later runs until it succeeds or is older than skipIfOlderThan"). The default mode's documentation says nothing about retrying.

Cause, as I read it

  • ClusterCron.make wraps the body in Effect.orDie (execute, cluster/ClusterCron.ts).
  • The entity's run handler logs the failure (Effect.logWarning(exit.cause)) and schedules the next slot in onExitPrimitive, then still ends with the defect.
  • Sharding's entity manager answers a defect by restarting the entity and redelivering the persisted run on its defect retry schedule ("Defect in entity, restarting", cluster/internal/entityManager.ts): exponential from 500 ms, at most 10 s.

The two runtimes disagree

@effect/platform-cloudflare's cluster runs the same ClusterCron without this. Its entity runtime retries a defect only when the entity sets defectRetryPolicy (runWithDefectRetry in internal/entityRuntime.ts); otherwise it replies with the defect and rebuilds. So on Cloudflare a failed slot runs once. That is from reading the code and from running it, not from a reproduction in this issue.

Question

Which is intended for the default mode?

  1. At most once per slot: the failure is logged and the next slot scheduled, and that is the end of it. Then run should not end with a defect after handling the failure. calculateNextRunFromPrevious would keep its documented retry in place, and the Cloudflare runtime would need that retry added to match.
  2. Retried: then it should be documented, probably bounded, and the Cloudflare runtime should match it.

I'm happy to send a PR once you say which.

Reproduction

effect and @effect/sql-sqlite-node at main (or 4.1.6), run with node cron-retry.ts:

import { SqliteClient } from "@effect/sql-sqlite-node"
import { Cron, DateTime, Duration, Effect, Layer } from "effect"
import { ClusterCron, SingleRunner } from "effect/cluster"
import * as Digest from "effect/crypto/Digest"

const started = Date.now()
const runs = new Map<string, number>()

const Failing = ClusterCron.make({
  name: "failing",
  cron: Cron.parseUnsafe("*/10 * * * * *"),
  execute: Effect.gen(function*() {
    const slot = DateTime.formatIso(yield* ClusterCron.CurrentScheduledTime)
    runs.set(slot, (runs.get(slot) ?? 0) + 1)
    console.log(`+${((Date.now() - started) / 1000).toFixed(1)}s body run for slot ${slot}`)
    return yield* Effect.fail("boom")
  })
})

await Effect.runPromise(
  Layer.launch(Failing.pipe(
    Layer.provide(SingleRunner.layer({
      runnerStorage: "memory",
      shardingConfig: { entityMessagePollInterval: Duration.millis(200) }
    })),
    Layer.provide(SqliteClient.layer({ filename: ":memory:" })),
    Layer.provide(Digest.layerWebCrypto)
  )).pipe(Effect.timeout(Duration.seconds(45)), Effect.ignore)
)
console.log(Object.fromEntries(runs)) // more than one run for every slot

Activity

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions