| 1 | import * as Cloudflare from "@/Cloudflare"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 4 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 5 | |
| 6 | export const EXPLICIT_WORKFLOW_NAME = "alchemy-pr1460-effect-workflow"; |
| 7 | |
| 8 | export class ExplicitNameWorkflow extends Cloudflare.Workflow<ExplicitNameWorkflow>()( |
| 9 | "ExplicitNameWorkflow", |
| 10 | { workflowName: EXPLICIT_WORKFLOW_NAME, schedules: ["0 0 1 1 *"] }, |
| 11 | Effect.succeed( |
| 12 | Effect.fn(function* () { |
| 13 | const event = yield* Cloudflare.Workflows.WorkflowEvent; |
| 14 | return yield* Cloudflare.Workflows.task( |
| 15 | "greet", |
| 16 | Effect.succeed({ |
| 17 | greeting: "Hello, world!", |
| 18 | workflowName: event.workflowName, |
| 19 | instanceId: event.instanceId, |
| 20 | }), |
| 21 | ); |
| 22 | }), |
| 23 | ), |
| 24 | ) {} |
| 25 | |
| 26 | export default class ExplicitNameWorkflowWorker extends Cloudflare.Worker<ExplicitNameWorkflowWorker>()( |
| 27 | "ExplicitNameWorkflowWorker", |
| 28 | { main: import.meta.url }, |
| 29 | Effect.gen(function* () { |
| 30 | const workflow = yield* ExplicitNameWorkflow; |
| 31 | return { |
| 32 | fetch: Effect.gen(function* () { |
| 33 | const request = yield* HttpServerRequest; |
| 34 | if (request.url.startsWith("/workflow/start/")) { |
| 35 | const instance = yield* workflow.create(); |
| 36 | return yield* HttpServerResponse.json({ instanceId: instance.id }); |
| 37 | } |
| 38 | if (request.url.startsWith("/workflow/status/")) { |
| 39 | const instance = yield* workflow.get(request.url.split("/").pop()!); |
| 40 | return yield* HttpServerResponse.json(yield* instance.status()); |
| 41 | } |
| 42 | return HttpServerResponse.text("ok"); |
| 43 | }), |
| 44 | }; |
| 45 | }), |
| 46 | ) {} |