-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathindex.ts
More file actions
60 lines (55 loc) · 1.72 KB
/
index.ts
File metadata and controls
60 lines (55 loc) · 1.72 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
import * as Sentry from '@sentry/cloudflare';
import { WorkflowEntrypoint } from 'cloudflare:workers';
import type { WorkflowEvent, WorkflowStep } from 'cloudflare:workers';
interface Env {
SENTRY_DSN: string;
MY_WORKFLOW: Workflow;
}
class MyWorkflowBase extends WorkflowEntrypoint<Env> {
async run(_event: WorkflowEvent<unknown>, step: WorkflowStep): Promise<void> {
await step.do('step-one', async () => {
return 'Step one completed';
});
await step.do('step-two', async () => {
return 'Step two completed';
});
}
}
export const MyWorkflow = Sentry.instrumentWorkflowWithSentry(
(env: Env) => ({
dsn: env.SENTRY_DSN,
tracesSampleRate: 1.0,
}),
MyWorkflowBase,
);
export default Sentry.withSentry(
(env: Env) => ({
dsn: env.SENTRY_DSN,
tracesSampleRate: 1.0,
}),
{
async fetch(request, env) {
const url = new URL(request.url);
if (url.pathname === '/workflow/trigger') {
const instance = await env.MY_WORKFLOW.create();
for (let i = 0; i < 15; i++) {
try {
const s = await instance.status();
if (s.status === 'complete' || s.status === 'errored') {
return new Response(JSON.stringify({ id: instance.id, ...s }), {
headers: { 'content-type': 'application/json' },
});
}
} catch {
// status() may not be available in local dev
}
await new Promise(r => setTimeout(r, 500));
}
return new Response(JSON.stringify({ id: instance.id, status: 'timeout' }), {
headers: { 'content-type': 'application/json' },
});
}
return new Response('OK');
},
} satisfies ExportedHandler<Env>,
);