| 1 | import * as AWS from "@/AWS/index.ts"; |
| 2 | import * as Duration from "effect/Duration"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 5 | |
| 6 | /** |
| 7 | * Probe for Lambda's Shutdown phase. |
| 8 | * |
| 9 | * The init closure registers an instance-scope finalizer that writes a |
| 10 | * marker to stdout (→ CloudWatch): the generated entry registers an |
| 11 | * internal extension, so Lambda grants the runtime a 500 ms SIGTERM window |
| 12 | * at sandbox spin-down in which the entry closes the instance scope — the |
| 13 | * marker appears exactly once per sandbox, never per invocation. |
| 14 | * |
| 15 | * The fetch handler registers a request-scope finalizer whose marker must |
| 16 | * appear once per invocation (the dispatch settles the request scope |
| 17 | * inline before returning). |
| 18 | */ |
| 19 | export default class ShutdownProbe extends AWS.Lambda.Function<ShutdownProbe>()( |
| 20 | "ShutdownProbe", |
| 21 | { |
| 22 | main: import.meta.url, |
| 23 | functionUrl: true, |
| 24 | timeout: Duration.seconds(10), |
| 25 | }, |
| 26 | Effect.gen(function* () { |
| 27 | yield* Effect.addFinalizer(() => |
| 28 | Effect.sync(() => console.log("ALCHEMY_INSTANCE_FINALIZED")), |
| 29 | ); |
| 30 | return { |
| 31 | fetch: Effect.gen(function* () { |
| 32 | yield* Effect.addFinalizer(() => |
| 33 | Effect.sync(() => console.log("ALCHEMY_REQUEST_FINALIZED")), |
| 34 | ); |
| 35 | return HttpServerResponse.text("ok"); |
| 36 | }), |
| 37 | }; |
| 38 | }), |
| 39 | ) {} |