|
| 1 | +import { MyWorkflow } from './workflow'; |
| 2 | + |
| 3 | +interface Env { |
| 4 | + SENTRY_DSN: string; |
| 5 | + MY_WORKFLOW: Workflow; |
| 6 | +} |
| 7 | + |
| 8 | +// `MyWorkflow` was already wrapped by hand in `./workflow`. The auto-instrument |
| 9 | +// transform cannot see that from this entry, so it emits its wrapper behind |
| 10 | +// `_INTERNAL_wrapUnlessInstrumented`, which hands the manual wrap back unchanged |
| 11 | +// instead of nesting a second wrapper around it. |
| 12 | +export { MyWorkflow }; |
| 13 | + |
| 14 | +export default { |
| 15 | + async fetch(request: Request, env: Env): Promise<Response> { |
| 16 | + const url = new URL(request.url); |
| 17 | + |
| 18 | + // Issued by the test after `/trigger` returned, its transaction is the |
| 19 | + // sentinel proving every earlier envelope (including a duplicate step |
| 20 | + // transaction from an accidental double wrap) has been delivered. |
| 21 | + if (url.pathname === '/sentinel') { |
| 22 | + return new Response('ok'); |
| 23 | + } |
| 24 | + |
| 25 | + if (url.pathname === '/trigger') { |
| 26 | + const instance = await env.MY_WORKFLOW.create(); |
| 27 | + // Respond only once the workflow finished, so every step envelope (including |
| 28 | + // a duplicate from an accidental double wrap) is sent before this request's |
| 29 | + // own transaction completes the test's expectations. |
| 30 | + for (let i = 0; i < 20; i++) { |
| 31 | + try { |
| 32 | + const s = await instance.status(); |
| 33 | + if (s.status === 'complete' || s.status === 'errored') { |
| 34 | + return Response.json({ id: instance.id, ...s }); |
| 35 | + } |
| 36 | + } catch { |
| 37 | + // status() may not be available in local dev |
| 38 | + } |
| 39 | + await new Promise(resolve => setTimeout(resolve, 250)); |
| 40 | + } |
| 41 | + return Response.json({ id: instance.id, status: 'timeout' }); |
| 42 | + } |
| 43 | + |
| 44 | + return new Response('Not found', { status: 404 }); |
| 45 | + }, |
| 46 | +} satisfies ExportedHandler<Env>; |
0 commit comments