1import * as Neon from "alchemy/Neon";
2import * as SQL from "alchemy/SQL/Postgres";
3import * as Effect from "effect/Effect";
4import * as Layer from "effect/Layer";
5import { resources } from "./resources.ts";
6
7export default class Events extends Neon.Function<Events>()(
8 "Events",
9 Effect.gen(function* () {
10 const { branch } = yield* resources;
11 return { branch, main: import.meta.url };
12 }),
13 Effect.gen(function* () {
14 const { branch } = yield* resources;
15 const connection = yield* Neon.Connect(branch);
16 const sql = yield* SQL.Postgres({ url: connection.connectionString });
17 yield* Neon.CronEventSource("Nightly", { cron: "0 2 * * *" }, (event) =>
18 Effect.gen(function* () {
19 // Redelivery of the same invocation cannot create a second journal entry.
20 yield* sql`INSERT INTO scheduled_runs (invocation_id)
21 VALUES (${event.invocationId}) ON CONFLICT DO NOTHING`;
22 }),
23 );
24 }).pipe(
25 Effect.provide(Layer.mergeAll(Neon.ConnectHttp, Neon.CronEventSourceHttp)),
26 ),
27) {}