-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathworker.ts
More file actions
43 lines (35 loc) · 1.29 KB
/
worker.ts
File metadata and controls
43 lines (35 loc) · 1.29 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
import { ITask, ITaskResponse, IWorkerConfig, State, Worker } from '..';
const config: IWorkerConfig = {
kafkaServers: 'localhost:29092', // kafka's brokers server
namespace: 'docker-compose', // melonade's namespace
};
// process callback task
const processTask = async (task: ITask): Promise<ITaskResponse> => {
console.log(`Processing ${task.taskName}: ${task.taskId}`);
await sleep(5 * 1000);
console.log(`Processed ${task.taskName}: ${task.taskId}`);
return {
status: State.TaskStates.Completed,
output: 'hello',
};
};
// compensate callback task
// will run if workflow failed, and task is success
// reverse what has been done
// task.input = { input: <processTask.input>, output: <processTask.output> }
const compensateTask = async (task: ITask): Promise<ITaskResponse> => {
console.log(`Compensating ${task.taskName}: ${task.taskId}`);
await sleep(5 * 1000);
console.log(`Compensated ${task.taskName}: ${task.taskId}`);
return {
status: State.TaskStates.Completed,
};
};
for (const workerId of ['t1', 't2', 't3']) {
const worker = new Worker(workerId, processTask, compensateTask, config);
worker.once('ready', () => {
console.log(`Worker ${workerId} is ready!`);
});
}
// util funcs
const sleep = (ms: number) => new Promise((res) => setTimeout(res, ms));