forked from danielgerlag/workflow-es
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12-saga.js
79 lines (66 loc) · 1.87 KB
/
12-saga.js
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
76
77
78
79
const workflow_es = require("workflow-es");
const workflow_mongo = require("workflow-es-mongodb");
class SayHello extends workflow_es.StepBody {
run(context) {
console.log("Hello");
return workflow_es.ExecutionResult.next();
}
}
class PrintMessage extends workflow_es.StepBody {
run(context) {
console.log(this.message);
return workflow_es.ExecutionResult.next();
}
}
class DoSomething extends workflow_es.StepBody {
run(context) {
console.log("Doing something...");
return workflow_es.ExecutionResult.next();
}
}
class UndoSomething extends workflow_es.StepBody {
run(context) {
console.log("Undoing something...");
return workflow_es.ExecutionResult.next();
}
}
class DoSomethingBad extends workflow_es.StepBody {
run(context) {
console.log("Doing something bad...");
throw "explode";
}
}
class SayGoodbye extends workflow_es.StepBody {
run(context) {
console.log("Bye");
return workflow_es.ExecutionResult.next();
}
}
class Saga_Workflow {
constructor() {
this.id = "saga-sample";
this.version = 1;
}
build(builder) {
builder
.startWith(SayHello)
.saga(saga => saga
.startWith(DoSomething)
.then(DoSomethingBad)
)
.compensateWith(UndoSomething)
.then(SayGoodbye);
}
}
async function main() {
var config = workflow_es.configureWorkflow();
//let mongoPersistence = new workflow_mongo.MongoDBPersistence("mongodb://127.0.0.1:27017/workflow-node");
//await mongoPersistence.connect;
//config.usePersistence(mongoPersistence);
var host = config.getHost();
host.registerWorkflow(Saga_Workflow);
await host.start();
let id = await host.startWorkflow("saga-sample", 1);
console.log("Started workflow: " + id);
}
main();