The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { |
| 2 | retryContainerBuild, |
| 3 | retryContainerPublication, |
| 4 | } from "@alchemy.run/cloudflare/Containers/ContainerPublication"; |
| 5 | import { |
| 6 | DockerRegistryBlobUnknown, |
| 7 | DockerRegistryUnavailable, |
| 8 | } from "@alchemy.run/docker/RegistryError"; |
| 9 | import { describe, expect, it } from "@alchemy.run/test"; |
| 10 | import * as Effect from "effect/Effect"; |
| 11 | import * as Fiber from "effect/Fiber"; |
| 12 | import { PlatformError, SystemError } from "effect/PlatformError"; |
| 13 | import * as TestClock from "effect/testing/TestClock"; |
| 14 | |
| 15 | const cause = new PlatformError( |
| 16 | new SystemError({ |
| 17 | _tag: "Unknown", |
| 18 | module: "Docker", |
| 19 | method: "buildx.build", |
| 20 | description: "publication failed", |
| 21 | }), |
| 22 | ); |
| 23 | |
| 24 | describe( |
| 25 | "container publication retries", |
| 26 | { |
| 27 | tags: ["unit", "provider:cloudflare", "provider:cloudflare:container", "local"], |
| 28 | }, |
| 29 | () => { |
| 30 | for (const error of [ |
| 31 | new DockerRegistryBlobUnknown({ cause }), |
| 32 | new DockerRegistryUnavailable({ cause }), |
| 33 | ]) { |
| 34 | it.effect( |
| 35 | `recovers from ${error._tag}`, |
| 36 | () => |
| 37 | Effect.gen(function* () { |
| 38 | let attempts = 0; |
| 39 | const fiber = yield* Effect.suspend(() => |
| 40 | ++attempts < 3 ? Effect.fail(error) : Effect.succeed("published"), |
| 41 | ).pipe(retryContainerPublication, Effect.forkChild); |
| 42 | yield* TestClock.adjust("15 seconds"); |
| 43 | expect(yield* Fiber.join(fiber)).toBe("published"); |
| 44 | expect(attempts).toBe(3); |
| 45 | }), |
| 46 | { tags: ["provider:docker", "provider:docker:registry"] }, |
| 47 | ); |
| 48 | |
| 49 | it.effect( |
| 50 | `bounds retries for ${error._tag}`, |
| 51 | () => |
| 52 | Effect.gen(function* () { |
| 53 | let attempts = 0; |
| 54 | const fiber = yield* Effect.suspend(() => { |
| 55 | attempts++; |
| 56 | return Effect.fail(error); |
| 57 | }).pipe(retryContainerPublication, Effect.flip, Effect.forkChild); |
| 58 | yield* TestClock.adjust("1 minute"); |
| 59 | expect(yield* Fiber.join(fiber)).toBe(error); |
| 60 | expect(attempts).toBe(6); |
| 61 | }), |
| 62 | { tags: ["provider:docker", "provider:docker:registry"] }, |
| 63 | ); |
| 64 | } |
| 65 | |
| 66 | it.effect("retries a build attempt that stalls past its deadline", () => |
| 67 | Effect.gen(function* () { |
| 68 | let attempts = 0; |
| 69 | const fiber = yield* Effect.suspend(() => |
| 70 | ++attempts === 1 ? Effect.never : Effect.succeed("published"), |
| 71 | ).pipe(retryContainerBuild, Effect.forkChild); |
| 72 | yield* TestClock.adjust("3 minutes"); |
| 73 | yield* TestClock.adjust("3 seconds"); |
| 74 | expect(yield* Fiber.join(fiber)).toBe("published"); |
| 75 | expect(attempts).toBe(2); |
| 76 | }), |
| 77 | ); |
| 78 | |
| 79 | it.effect("gives up on a build that keeps stalling", () => |
| 80 | Effect.gen(function* () { |
| 81 | let attempts = 0; |
| 82 | const fiber = yield* Effect.suspend(() => { |
| 83 | attempts++; |
| 84 | return Effect.never; |
| 85 | }).pipe(retryContainerBuild, Effect.flip, Effect.forkChild); |
| 86 | yield* TestClock.adjust("20 minutes"); |
| 87 | expect((yield* Fiber.join(fiber))._tag).toBe("TimeoutError"); |
| 88 | expect(attempts).toBe(6); |
| 89 | }), |
| 90 | ); |
| 91 | |
| 92 | it.effect("propagates other Docker errors without retrying", () => |
| 93 | Effect.gen(function* () { |
| 94 | let attempts = 0; |
| 95 | const error = yield* Effect.suspend(() => { |
| 96 | attempts++; |
| 97 | return Effect.fail(cause); |
| 98 | }).pipe(retryContainerPublication, Effect.flip); |
| 99 | expect(error).toBe(cause); |
| 100 | expect(attempts).toBe(1); |
| 101 | }), |
| 102 | ); |
| 103 | }, |
| 104 | ); |