-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdefault-source.ts
More file actions
75 lines (64 loc) · 2.43 KB
/
Copy pathdefault-source.ts
File metadata and controls
75 lines (64 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/**
* Seed code that appears in the editor on first page load.
*
* The editor submits this source verbatim to the Worker Loader — no
* TypeScript, no bundling, no npm installs. Whatever JavaScript the user
* writes here runs as the tenant worker.
*
* The only contract is:
* - It must export a default `{ fetch }` handler that creates the workflow
* via `env.WORKFLOWS.create({ id, params })`. The dispatcher calls this
* handler to kick off a run.
* - It must export a `TenantWorkflow` class that extends `WorkflowEntrypoint`
* — the Workflows engine invokes `TenantWorkflow.run()` via the
* dispatcher.
*/
export const DEFAULT_SOURCE = `import { WorkflowEntrypoint } from 'cloudflare:workers';
// --- Your workflow -------------------------------------------------------
//
// Edit this to build whatever pipeline you like. Each \`step.do()\` appears
// as a checkpoint in the UI; \`console.log()\` output streams live.
export class TenantWorkflow extends WorkflowEntrypoint {
async run(event, step) {
const name = event.payload?.name ?? 'world';
console.log('starting workflow for', name);
const greeting = await step.do('compose greeting', async () => {
const message = \`Hello, \${name}!\`;
console.log('composed:', message);
return message;
});
await step.sleep('pause for effect', '2 seconds');
const shouted = await step.do('shout it', async () => {
const result = greeting.toUpperCase() + '!!!';
console.warn('shouting:', result);
return result;
});
await step.sleep('catch breath', '2 seconds');
const summary = await step.do('summarise', async () => {
return {
original: greeting,
shouted,
length: shouted.length,
};
});
console.log('done, returning', summary);
return summary;
}
}
// --- Plumbing ------------------------------------------------------------
//
// The dispatcher POSTs { id, payload } to this handler with the pre-allocated
// runId. We forward it straight into \`env.WORKFLOWS.create()\` — which is the
// wrapped binding provided by dynamic-workflows, so the tenant id travels
// along automatically.
export default {
async fetch(request, env) {
const { id, payload } = await request.json();
const instance = await env.WORKFLOWS.create({ id, params: payload });
return Response.json({
id: await instance.id,
status: await instance.status(),
});
},
};
`;