1import * as Effect from "effect/Effect";
2import * as Schedule from "effect/Schedule";
3
4/**
5 * Ride out the window in which a Worker created earlier in the same apply is
6 * not yet visible to Email Routing's action validation.
7 *
8 * Cloudflare validates a `{ type: "worker", value: [scriptName] }` action
9 * while handling the create/update request and rejects it with
10 * `WorkerScriptNotFound` ("Workers Script Info not found") until the script
11 * propagates — typically within a few hundred milliseconds. The rejected call
12 * creates nothing, so a retry cannot produce a duplicate rule.
13 *
14 * Mirrors `Cloudflare/Fetcher`'s treatment of a freshly-deployed script:
15 * bounded exponential backoff (~25s total), then re-raise the original error
16 * unchanged so a genuinely missing Worker still fails and says so.
17 *
18 * @see https://github.com/alchemy-run/alchemy/issues/1348
19 */
20export const retryWorkerScriptNotFound = <A, E extends { _tag: string }, R>(
21 effect: Effect.Effect<A, E, R>,
22): Effect.Effect<A, E, R> =>
23 effect.pipe(
24 Effect.retry({
25 while: (error) => error._tag === "WorkerScriptNotFound",
26 schedule: Schedule.exponential("100 millis"),
27 times: 8,
28 }),
29 );