-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsubscribe.ts
60 lines (53 loc) · 1.73 KB
/
subscribe.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
59
60
import { Event, State } from '..';
import { Admin, EventTypes } from '../admin';
const kafkaServers = process.env['MELONADE_KAFKA_SERVERS'];
const namespace = process.env['MELONADE_NAMESPACE'];
const adminClient = new Admin({
kafkaServers,
namespace,
adminId: 'DEMO_SUBSCRIBE_01',
subscribeToAllTransaction: true,
});
// use subscribeToAllTransaction or subscribe each transactionID
// adminClient.subscribe('someTransactionID1')
// adminClient.subscribe('someTransactionID2')
// adminClient.subscribe('someTransactionID3')
adminClient.on(
EventTypes.TransactionEvent,
(event: Event.ITransactionEvent) => {
switch (event.details.status) {
case State.TransactionStates.Running:
console.log(`Transacrion ${event.transactionId} started`);
break;
case State.TransactionStates.Completed:
console.log(`Transacrion ${event.transactionId} Completed`);
break;
}
},
);
adminClient.on(
EventTypes.TransactionFalseEvent,
(event: Event.ITransactionErrorEvent) => {
console.log(`Transacrion ${event.transactionId} went wrong`, event.error);
},
);
adminClient.on(EventTypes.TaskEvent, (event: Event.ITaskEvent) => {
// Ignore system task and compensate task
// Or you may filter by taskName as well
if (event.details.type === 'TASK') {
switch (event.details.status) {
case State.TaskStates.Scheduled:
console.log(`Task ${event.details.taskId} started`);
break;
case State.TaskStates.Inprogress:
console.log(`Task ${event.details.taskId} acked`);
break;
case State.TaskStates.Completed:
console.log(`Task ${event.details.taskId} Completed`);
break;
}
}
});
adminClient.once('ready', () => {
console.log('Ready');
});