generated from hatchet-dev/hatchet-python-quickstart
-
Notifications
You must be signed in to change notification settings - Fork 3
/
retries-worker.ts
58 lines (51 loc) · 1.2 KB
/
retries-worker.ts
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
import Hatchet, { Workflow } from '@hatchet-dev/typescript-sdk';
const hatchet = Hatchet.init();
const sleep = (ms: number) =>
new Promise((resolve) => {
setTimeout(resolve, ms);
});
let numRetries = 0;
const workflow: Workflow = {
id: 'retries-workflow',
description: 'test',
on: {
event: 'user:create',
},
steps: [
{
name: 'step1',
run: async (ctx) => {
if (numRetries < 3) {
numRetries += 1;
throw new Error('step1 failed');
}
console.log(
'starting step1 with the following input',
ctx.workflowInput(),
);
console.log('waiting 5 seconds...');
await sleep(5000);
console.log('executed step1!');
return { step1: 'step1 results!' };
},
retries: 3,
},
{
name: 'step2',
parents: ['step1'],
run: (ctx) => {
console.log(
'executed step2 after step1 returned ',
ctx.stepOutput('step1'),
);
return { step2: 'step2 results!' };
},
},
],
};
async function main() {
const worker = await hatchet.worker('example-worker');
await worker.registerWorkflow(workflow);
worker.start();
}
main();