bpmn-elements 6.0.0 → 8.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +335 -0
- package/README.md +3 -0
- package/dist/index.js +52 -44
- package/dist/src/Api.js +77 -76
- package/dist/src/Context.js +176 -172
- package/dist/src/Environment.js +110 -102
- package/dist/src/EventBroker.js +89 -88
- package/dist/src/ExtensionsMapper.js +2 -2
- package/dist/src/MessageFormatter.js +164 -95
- package/dist/src/Scripts.js +6 -2
- package/dist/src/activity/Activity.js +1106 -916
- package/dist/src/activity/ActivityExecution.js +342 -297
- package/dist/src/activity/Dummy.js +3 -3
- package/dist/src/definition/Definition.js +498 -444
- package/dist/src/definition/DefinitionExecution.js +710 -408
- package/dist/src/error/Errors.js +17 -7
- package/dist/src/eventDefinitions/CancelEventDefinition.js +190 -150
- package/dist/src/eventDefinitions/CompensateEventDefinition.js +194 -161
- package/dist/src/eventDefinitions/ConditionalEventDefinition.js +197 -135
- package/dist/src/eventDefinitions/ErrorEventDefinition.js +207 -165
- package/dist/src/eventDefinitions/EscalationEventDefinition.js +175 -141
- package/dist/src/eventDefinitions/EventDefinitionExecution.js +157 -129
- package/dist/src/eventDefinitions/LinkEventDefinition.js +174 -149
- package/dist/src/eventDefinitions/MessageEventDefinition.js +213 -176
- package/dist/src/eventDefinitions/SignalEventDefinition.js +203 -161
- package/dist/src/eventDefinitions/TerminateEventDefinition.js +21 -23
- package/dist/src/eventDefinitions/TimerEventDefinition.js +243 -228
- package/dist/src/events/BoundaryEvent.js +180 -144
- package/dist/src/events/EndEvent.js +18 -23
- package/dist/src/events/IntermediateCatchEvent.js +44 -58
- package/dist/src/events/IntermediateThrowEvent.js +18 -23
- package/dist/src/events/StartEvent.js +109 -94
- package/dist/src/flows/Association.js +94 -100
- package/dist/src/flows/MessageFlow.js +86 -103
- package/dist/src/flows/SequenceFlow.js +173 -182
- package/dist/src/gateways/EventBasedGateway.js +88 -84
- package/dist/src/gateways/ExclusiveGateway.js +13 -16
- package/dist/src/gateways/InclusiveGateway.js +11 -14
- package/dist/src/gateways/ParallelGateway.js +11 -14
- package/dist/src/getPropertyValue.js +34 -34
- package/dist/src/io/BpmnIO.js +17 -14
- package/dist/src/io/EnvironmentDataObject.js +33 -29
- package/dist/src/io/EnvironmentDataStore.js +33 -29
- package/dist/src/io/EnvironmentDataStoreReference.js +35 -31
- package/dist/src/io/InputOutputSpecification.js +177 -168
- package/dist/src/io/Properties.js +117 -124
- package/dist/src/messageHelper.js +1 -1
- package/dist/src/process/Process.js +439 -362
- package/dist/src/process/ProcessExecution.js +748 -646
- package/dist/src/shared.js +2 -2
- package/dist/src/tasks/CallActivity.js +160 -0
- package/dist/src/tasks/LoopCharacteristics.js +309 -330
- package/dist/src/tasks/ReceiveTask.js +233 -182
- package/dist/src/tasks/ScriptTask.js +35 -41
- package/dist/src/tasks/ServiceImplementation.js +13 -20
- package/dist/src/tasks/ServiceTask.js +82 -75
- package/dist/src/tasks/SignalTask.js +97 -93
- package/dist/src/tasks/StandardLoopCharacteristics.js +1 -1
- package/dist/src/tasks/SubProcess.js +193 -175
- package/dist/src/tasks/Task.js +17 -19
- package/index.js +2 -0
- package/package.json +15 -15
- package/src/Api.js +65 -59
- package/src/Context.js +145 -140
- package/src/Environment.js +116 -100
- package/src/EventBroker.js +67 -68
- package/src/ExtensionsMapper.js +2 -2
- package/src/MessageFormatter.js +132 -74
- package/src/activity/Activity.js +915 -775
- package/src/activity/ActivityExecution.js +293 -247
- package/src/activity/Dummy.js +2 -2
- package/src/definition/Definition.js +437 -401
- package/src/definition/DefinitionExecution.js +598 -340
- package/src/error/Errors.js +11 -6
- package/src/eventDefinitions/CancelEventDefinition.js +164 -121
- package/src/eventDefinitions/CompensateEventDefinition.js +159 -124
- package/src/eventDefinitions/ConditionalEventDefinition.js +147 -104
- package/src/eventDefinitions/ErrorEventDefinition.js +190 -131
- package/src/eventDefinitions/EscalationEventDefinition.js +139 -101
- package/src/eventDefinitions/EventDefinitionExecution.js +127 -95
- package/src/eventDefinitions/LinkEventDefinition.js +160 -129
- package/src/eventDefinitions/MessageEventDefinition.js +178 -121
- package/src/eventDefinitions/SignalEventDefinition.js +162 -106
- package/src/eventDefinitions/TerminateEventDefinition.js +19 -19
- package/src/eventDefinitions/TimerEventDefinition.js +202 -167
- package/src/events/BoundaryEvent.js +156 -115
- package/src/events/EndEvent.js +15 -18
- package/src/events/IntermediateCatchEvent.js +40 -44
- package/src/events/IntermediateThrowEvent.js +15 -18
- package/src/events/StartEvent.js +84 -50
- package/src/flows/Association.js +98 -112
- package/src/flows/MessageFlow.js +81 -97
- package/src/flows/SequenceFlow.js +146 -160
- package/src/gateways/EventBasedGateway.js +75 -68
- package/src/gateways/ExclusiveGateway.js +8 -13
- package/src/gateways/InclusiveGateway.js +8 -13
- package/src/gateways/ParallelGateway.js +8 -13
- package/src/getPropertyValue.js +34 -33
- package/src/io/BpmnIO.js +20 -15
- package/src/io/EnvironmentDataObject.js +29 -18
- package/src/io/EnvironmentDataStore.js +29 -18
- package/src/io/EnvironmentDataStoreReference.js +31 -20
- package/src/io/InputOutputSpecification.js +154 -157
- package/src/io/Properties.js +95 -97
- package/src/process/Process.js +378 -333
- package/src/process/ProcessExecution.js +603 -553
- package/src/tasks/CallActivity.js +130 -0
- package/src/tasks/LoopCharacteristics.js +290 -289
- package/src/tasks/ReceiveTask.js +174 -107
- package/src/tasks/ScriptTask.js +27 -30
- package/src/tasks/ServiceImplementation.js +13 -18
- package/src/tasks/ServiceTask.js +67 -60
- package/src/tasks/SignalTask.js +77 -52
- package/src/tasks/StandardLoopCharacteristics.js +1 -1
- package/src/tasks/SubProcess.js +184 -157
- package/src/tasks/Task.js +15 -19
package/src/error/Errors.js
CHANGED
|
@@ -15,6 +15,8 @@ class ActivityError extends Error {
|
|
|
15
15
|
}
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
+
class RunError extends ActivityError {}
|
|
19
|
+
|
|
18
20
|
class BpmnError extends Error {
|
|
19
21
|
constructor(description, behaviour = {}, sourceMessage, inner) {
|
|
20
22
|
const {errorCode} = behaviour;
|
|
@@ -33,6 +35,7 @@ class BpmnError extends Error {
|
|
|
33
35
|
export {
|
|
34
36
|
ActivityError,
|
|
35
37
|
BpmnError,
|
|
38
|
+
RunError,
|
|
36
39
|
makeErrorFromMessage
|
|
37
40
|
};
|
|
38
41
|
|
|
@@ -41,21 +44,23 @@ function makeErrorFromMessage(errorMessage) {
|
|
|
41
44
|
if (isKnownError(content)) return content;
|
|
42
45
|
|
|
43
46
|
const {error} = content;
|
|
44
|
-
if (!error) return;
|
|
47
|
+
if (!error) return new Error(`Malformatted error message with routing key ${errorMessage.fields && errorMessage.fields.routingKey}`);
|
|
45
48
|
|
|
46
49
|
if (isKnownError(error)) return error;
|
|
47
50
|
switch (error.type) {
|
|
48
51
|
case 'ActivityError':
|
|
49
52
|
return new ActivityError(error.message || error.description, error.source, error.inner ? error.inner : {code: error.code, name: error.name});
|
|
53
|
+
case 'RunError':
|
|
54
|
+
return new RunError(error.message || error.description, error.source, error.inner ? error.inner : {code: error.code, name: error.name});
|
|
50
55
|
case 'BpmnError':
|
|
51
56
|
return new BpmnError(error.message || error.description, error, error.source);
|
|
52
57
|
}
|
|
53
58
|
|
|
54
59
|
return error;
|
|
60
|
+
}
|
|
55
61
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
}
|
|
62
|
+
function isKnownError(test) {
|
|
63
|
+
if (test instanceof ActivityError) return test;
|
|
64
|
+
if (test instanceof BpmnError) return test;
|
|
65
|
+
if (test instanceof Error) return test;
|
|
61
66
|
}
|
|
@@ -1,130 +1,173 @@
|
|
|
1
1
|
import {brokerSafeId} from '../shared';
|
|
2
2
|
import {cloneContent, shiftParent} from '../messageHelper';
|
|
3
3
|
|
|
4
|
+
const kMessageQ = Symbol.for('cancelQ');
|
|
5
|
+
const kCompleted = Symbol.for('completed');
|
|
6
|
+
const kExecuteMessage = Symbol.for('executeMessage');
|
|
7
|
+
|
|
4
8
|
export default function CancelEventDefinition(activity, eventDefinition) {
|
|
5
9
|
const {id, broker, environment, isThrowing} = activity;
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
const messageContent = cloneContent(executeMessage.content);
|
|
25
|
-
const {executionId, parent, attachedTo} = messageContent;
|
|
26
|
-
const parentExecutionId = parent && parent.executionId;
|
|
27
|
-
|
|
28
|
-
broker.consume(cancelQueueName, onCatchMessage, {noAck: true, consumerTag: `_oncancel-${executionId}`});
|
|
29
|
-
|
|
30
|
-
if (completed) return;
|
|
31
|
-
|
|
32
|
-
broker.subscribeTmp('api', `activity.#.${parentExecutionId}`, onApiMessage, {noAck: true, consumerTag: `_api-parent-${parentExecutionId}`});
|
|
33
|
-
broker.subscribeTmp('api', `activity.#.${executionId}`, onApiMessage, {noAck: true, consumerTag: `_api-${executionId}`});
|
|
34
|
-
|
|
35
|
-
debug(`<${executionId} (${id})> expect cancel`);
|
|
36
|
-
|
|
37
|
-
const exchangeKey = `execute.canceled.${executionId}`;
|
|
38
|
-
broker.subscribeOnce('execution', exchangeKey, onCatchMessage, {consumerTag: `_onattached-cancel-${executionId}`});
|
|
39
|
-
broker.publish('execution', 'execute.expect', cloneContent(messageContent, {
|
|
40
|
-
pattern: '#.cancel',
|
|
41
|
-
exchange: 'execution',
|
|
42
|
-
exchangeKey,
|
|
43
|
-
}));
|
|
44
|
-
|
|
45
|
-
function onCatchMessage(_, message) {
|
|
46
|
-
if (message.content && message.content.isTransaction) return onCancelTransaction(_, message);
|
|
47
|
-
|
|
48
|
-
debug(`<${executionId} (${id})> cancel caught from <${message.content.id}>`);
|
|
49
|
-
return complete(message.content.message);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function onCancelTransaction(_, message) {
|
|
53
|
-
broker.cancel(`_oncancel-${executionId}`);
|
|
54
|
-
|
|
55
|
-
debug(`<${executionId} (${id})> cancel transaction thrown by <${message.content.id}>`);
|
|
56
|
-
|
|
57
|
-
broker.assertExchange('cancel', 'topic');
|
|
58
|
-
broker.publish('execution', 'execute.detach', cloneContent(messageContent, {
|
|
59
|
-
pattern: '#',
|
|
60
|
-
bindExchange: 'cancel',
|
|
61
|
-
sourceExchange: 'event',
|
|
62
|
-
sourcePattern: '#',
|
|
63
|
-
}));
|
|
64
|
-
|
|
65
|
-
broker.publish('event', 'activity.compensate', cloneContent(message.content, {
|
|
66
|
-
state: 'throw',
|
|
67
|
-
}), {type: 'compensate', delegate: true});
|
|
68
|
-
|
|
69
|
-
broker.subscribeTmp('cancel', 'activity.leave', (__, {content: msg}) => {
|
|
70
|
-
if (msg.id !== attachedTo) return;
|
|
71
|
-
return complete(message.content.message);
|
|
72
|
-
}, {noAck: true, consumerTag: `_oncancelend-${executionId}`});
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
function complete(output) {
|
|
76
|
-
completed = true;
|
|
77
|
-
stop();
|
|
78
|
-
debug(`<${executionId} (${id})> completed`);
|
|
79
|
-
return broker.publish('execution', 'execute.completed', {...messageContent, output, state: 'cancel'});
|
|
80
|
-
}
|
|
10
|
+
const type = eventDefinition.type;
|
|
11
|
+
|
|
12
|
+
this.id = id;
|
|
13
|
+
this.type = type;
|
|
14
|
+
const reference = this.reference = {referenceType: 'cancel'};
|
|
15
|
+
this.isThrowing = isThrowing;
|
|
16
|
+
this.activity = activity;
|
|
17
|
+
this.environment = environment;
|
|
18
|
+
this.broker = broker;
|
|
19
|
+
this.logger = environment.Logger(type.toLowerCase());
|
|
20
|
+
|
|
21
|
+
if (!isThrowing) {
|
|
22
|
+
this[kCompleted] = false;
|
|
23
|
+
const messageQueueName = `${reference.referenceType}-${brokerSafeId(id)}-q`;
|
|
24
|
+
this[kMessageQ] = broker.assertQueue(messageQueueName, {autoDelete: false, durable: true});
|
|
25
|
+
broker.bindQueue(messageQueueName, 'api', `*.${reference.referenceType}.#`, {durable: true, priority: 400});
|
|
26
|
+
}
|
|
27
|
+
}
|
|
81
28
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
29
|
+
const proto = CancelEventDefinition.prototype;
|
|
30
|
+
|
|
31
|
+
Object.defineProperty(proto, 'executionId', {
|
|
32
|
+
get() {
|
|
33
|
+
const message = this[kExecuteMessage];
|
|
34
|
+
return message && message.content.executionId;
|
|
35
|
+
},
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
proto.execute = function execute(executeMessage) {
|
|
39
|
+
return this.isThrowing ? this.executeThrow(executeMessage) : this.executeCatch(executeMessage);
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
proto.executeCatch = function executeCatch(executeMessage) {
|
|
43
|
+
this[kExecuteMessage] = executeMessage;
|
|
44
|
+
this[kCompleted] = false;
|
|
45
|
+
|
|
46
|
+
const executeContent = executeMessage.content;
|
|
47
|
+
const {executionId, parent} = executeContent;
|
|
48
|
+
const parentExecutionId = parent.executionId;
|
|
49
|
+
|
|
50
|
+
const broker = this.broker;
|
|
51
|
+
const onCatchMessage = this._onCatchMessage.bind(this);
|
|
52
|
+
this[kMessageQ].consume(onCatchMessage, {
|
|
53
|
+
noAck: true,
|
|
54
|
+
consumerTag: `_oncancel-${executionId}`,
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
if (this[kCompleted]) return;
|
|
58
|
+
|
|
59
|
+
const onApiMessage = this._onApiMessage.bind(this);
|
|
60
|
+
broker.subscribeTmp('api', `activity.#.${parentExecutionId}`, onApiMessage, {
|
|
61
|
+
noAck: true,
|
|
62
|
+
consumerTag: `_api-parent-${executionId}`,
|
|
63
|
+
});
|
|
64
|
+
broker.subscribeTmp('api', `activity.#.${executionId}`, onApiMessage, {
|
|
65
|
+
noAck: true,
|
|
66
|
+
consumerTag: `_api-${executionId}`,
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
this._debug('expect cancel');
|
|
70
|
+
|
|
71
|
+
const exchangeKey = `execute.canceled.${executionId}`;
|
|
72
|
+
broker.subscribeOnce('execution', exchangeKey, onCatchMessage, {
|
|
73
|
+
consumerTag: `_onattached-cancel-${executionId}`,
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
broker.publish('execution', 'execute.expect', cloneContent(executeContent, {
|
|
77
|
+
pattern: '#.cancel',
|
|
78
|
+
exchange: 'execution',
|
|
79
|
+
exchangeKey,
|
|
80
|
+
}));
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
proto.executeThrow = function executeThrow(executeMessage) {
|
|
84
|
+
const {isTransaction} = this.environment.variables.content || {};
|
|
85
|
+
const executeContent = executeMessage.content;
|
|
86
|
+
const {executionId, parent} = executeContent;
|
|
87
|
+
|
|
88
|
+
this.logger.debug(`<${executionId} (${this.activity.id})> throw cancel${isTransaction ? ' transaction' : ''}`);
|
|
89
|
+
|
|
90
|
+
const broker = this.broker;
|
|
91
|
+
const cancelContent = cloneContent(executeContent, {
|
|
92
|
+
isTransaction,
|
|
93
|
+
executionId: parent.executionId,
|
|
94
|
+
state: 'throw',
|
|
95
|
+
});
|
|
96
|
+
cancelContent.parent = shiftParent(parent);
|
|
97
|
+
|
|
98
|
+
broker.publish('event', 'activity.cancel', cancelContent, {type: 'cancel', delegate: isTransaction});
|
|
99
|
+
|
|
100
|
+
return broker.publish('execution', 'execute.completed', cloneContent(executeContent));
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
proto._onCatchMessage = function onCatchMessage(_, message) {
|
|
104
|
+
if (message.content && message.content.isTransaction) return this._onCancelTransaction(_, message);
|
|
105
|
+
|
|
106
|
+
this._debug(`cancel caught from <${message.content.id}>`);
|
|
107
|
+
return this._complete(message.content.message);
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
proto._onCancelTransaction = function onCancelTransaction(_, message) {
|
|
111
|
+
const broker = this.broker, executionId = this.executionId;
|
|
112
|
+
const executeContent = this[kExecuteMessage].content;
|
|
113
|
+
broker.cancel(`_oncancel-${executionId}`);
|
|
114
|
+
|
|
115
|
+
this._debug(`cancel transaction thrown by <${message.content.id}>`);
|
|
116
|
+
|
|
117
|
+
broker.assertExchange('cancel', 'topic');
|
|
118
|
+
broker.publish('execution', 'execute.detach', cloneContent(executeContent, {
|
|
119
|
+
pattern: '#',
|
|
120
|
+
bindExchange: 'cancel',
|
|
121
|
+
sourceExchange: 'event',
|
|
122
|
+
sourcePattern: '#',
|
|
123
|
+
}));
|
|
124
|
+
|
|
125
|
+
broker.publish('event', 'activity.compensate', cloneContent(message.content, {
|
|
126
|
+
state: 'throw',
|
|
127
|
+
}), {type: 'compensate', delegate: true});
|
|
128
|
+
|
|
129
|
+
broker.subscribeTmp('cancel', 'activity.leave', (__, {content: msg}) => {
|
|
130
|
+
if (msg.id !== executeContent.attachedTo) return;
|
|
131
|
+
return this._complete(message.content.message);
|
|
132
|
+
}, {noAck: true, consumerTag: `_oncancelend-${executionId}`});
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
proto._complete = function complete(output) {
|
|
136
|
+
this[kCompleted] = true;
|
|
137
|
+
this._stop();
|
|
138
|
+
this._debug('completed');
|
|
139
|
+
const content = cloneContent(this[kExecuteMessage].content, {
|
|
140
|
+
output,
|
|
141
|
+
state: 'cancel',
|
|
142
|
+
});
|
|
143
|
+
return this.broker.publish('execution', 'execute.completed', content);
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
proto._onApiMessage = function onApiMessage(routingKey, message) {
|
|
147
|
+
switch (message.properties.type) {
|
|
148
|
+
case 'discard': {
|
|
149
|
+
this[kCompleted] = true;
|
|
150
|
+
this._stop();
|
|
151
|
+
const content = cloneContent(this[kExecuteMessage].content);
|
|
152
|
+
return this.broker.publish('execution', 'execute.discard', content);
|
|
95
153
|
}
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
broker.cancel(`_api-${executionId}`);
|
|
100
|
-
broker.cancel(`_oncancel-${executionId}`);
|
|
101
|
-
broker.cancel(`_oncancelend-${executionId}`);
|
|
102
|
-
broker.cancel(`_onattached-cancel-${executionId}`);
|
|
103
|
-
broker.purgeQueue(cancelQueueName);
|
|
154
|
+
case 'stop': {
|
|
155
|
+
this._stop();
|
|
156
|
+
break;
|
|
104
157
|
}
|
|
105
158
|
}
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
}, {type: 'cancel', delegate: isTransaction});
|
|
122
|
-
|
|
123
|
-
return broker.publish('execution', 'execute.completed', {...messageContent});
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
function setupCatch() {
|
|
127
|
-
broker.assertQueue(cancelQueueName, {autoDelete: false, durable: true});
|
|
128
|
-
broker.bindQueue(cancelQueueName, 'api', '*.cancel.#', {durable: true, priority: 400});
|
|
129
|
-
}
|
|
130
|
-
}
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
proto._stop = function stop() {
|
|
162
|
+
const broker = this.broker, executionId = this.executionId;
|
|
163
|
+
broker.cancel(`_api-parent-${executionId}`);
|
|
164
|
+
broker.cancel(`_api-${executionId}`);
|
|
165
|
+
broker.cancel(`_oncancel-${executionId}`);
|
|
166
|
+
broker.cancel(`_oncancelend-${executionId}`);
|
|
167
|
+
broker.cancel(`_onattached-cancel-${executionId}`);
|
|
168
|
+
this[kMessageQ].purge();
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
proto._debug = function debug(msg) {
|
|
172
|
+
this.logger.debug(`<${this.executionId} (${this.activity.id})> ${msg}`);
|
|
173
|
+
};
|
|
@@ -1,144 +1,179 @@
|
|
|
1
1
|
import {brokerSafeId} from '../shared';
|
|
2
2
|
import {cloneContent, cloneMessage, shiftParent} from '../messageHelper';
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
const associations = context.getOutboundAssociations(id) || [];
|
|
10
|
-
|
|
11
|
-
if (!isThrowing) setupCatch();
|
|
12
|
-
|
|
13
|
-
const source = {
|
|
14
|
-
id,
|
|
15
|
-
type,
|
|
16
|
-
reference: {referenceType: 'compensate'},
|
|
17
|
-
execute: isThrowing ? executeThrow : executeCatch,
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
return source;
|
|
21
|
-
|
|
22
|
-
function executeCatch(executeMessage) {
|
|
23
|
-
let completed;
|
|
24
|
-
|
|
25
|
-
const messageContent = cloneContent(executeMessage.content);
|
|
26
|
-
const {executionId, parent} = messageContent;
|
|
27
|
-
const parentExecutionId = parent && parent.executionId;
|
|
28
|
-
|
|
29
|
-
broker.consume(compensationQueueName, onCompensateApiMessage, {noAck: true, consumerTag: `_oncompensate-${executionId}`});
|
|
4
|
+
const kCompleted = Symbol.for('completed');
|
|
5
|
+
const kExecuteMessage = Symbol.for('executeMessage');
|
|
6
|
+
const kMessageQ = Symbol.for('messageQ');
|
|
7
|
+
const kCompensateQ = Symbol.for('compensateQ');
|
|
8
|
+
const kAssociations = Symbol.for('associations');
|
|
30
9
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
broker.subscribeTmp('api', `activity.#.${executionId}`, onApiMessage, {noAck: true, consumerTag: `_api-${executionId}`});
|
|
10
|
+
export default function CompensateEventDefinition(activity, eventDefinition, context) {
|
|
11
|
+
const {id, broker, environment, isThrowing} = activity;
|
|
34
12
|
|
|
35
|
-
|
|
13
|
+
this.id = id;
|
|
14
|
+
const type = this.type = eventDefinition.type;
|
|
15
|
+
const reference = this.reference = {referenceType: 'compensate'};
|
|
16
|
+
this.isThrowing = isThrowing;
|
|
17
|
+
this.activity = activity;
|
|
18
|
+
this.broker = broker;
|
|
19
|
+
this.logger = environment.Logger(type.toLowerCase());
|
|
20
|
+
|
|
21
|
+
if (!isThrowing) {
|
|
22
|
+
this[kCompleted] = false;
|
|
23
|
+
this[kAssociations] = context.getOutboundAssociations(id) || [];
|
|
24
|
+
const messageQueueName = `${reference.referenceType}-${brokerSafeId(id)}-q`;
|
|
25
|
+
this[kMessageQ] = broker.assertQueue(messageQueueName, {autoDelete: false, durable: true});
|
|
26
|
+
broker.bindQueue(messageQueueName, 'api', `*.${reference.referenceType}.#`, {durable: true, priority: 400});
|
|
27
|
+
}
|
|
28
|
+
}
|
|
36
29
|
|
|
37
|
-
|
|
30
|
+
const proto = CompensateEventDefinition.prototype;
|
|
31
|
+
|
|
32
|
+
Object.defineProperty(proto, 'executionId', {
|
|
33
|
+
get() {
|
|
34
|
+
const message = this[kExecuteMessage];
|
|
35
|
+
return message && message.content.executionId;
|
|
36
|
+
},
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
proto.execute = function execute(executeMessage) {
|
|
40
|
+
return this.isThrowing ? this.executeThrow(executeMessage) : this.executeCatch(executeMessage);
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
proto.executeCatch = function executeCatch(executeMessage) {
|
|
44
|
+
this[kExecuteMessage] = executeMessage;
|
|
45
|
+
this[kCompleted] = false;
|
|
46
|
+
|
|
47
|
+
const executeContent = executeMessage.content;
|
|
48
|
+
const {executionId, parent} = executeContent;
|
|
49
|
+
|
|
50
|
+
this._debug('expect compensate');
|
|
51
|
+
|
|
52
|
+
const broker = this.broker;
|
|
53
|
+
broker.assertExchange('compensate', 'topic');
|
|
54
|
+
this[kCompensateQ] = broker.assertQueue('compensate-q', {durable: true, autoDelete: false});
|
|
55
|
+
broker.subscribeTmp('compensate', 'execute.#', this._onCollect.bind(this), {
|
|
56
|
+
noAck: true,
|
|
57
|
+
consumerTag: '_oncollect-messages',
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
broker.publish('execution', 'execute.detach', cloneContent(executeContent, {
|
|
61
|
+
sourceExchange: 'execution',
|
|
62
|
+
bindExchange: 'compensate',
|
|
63
|
+
}));
|
|
64
|
+
|
|
65
|
+
this[kMessageQ].consume(this._onCompensateApiMessage.bind(this), {
|
|
66
|
+
noAck: true,
|
|
67
|
+
consumerTag: `_oncompensate-${executionId}`,
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
if (this[kCompleted]) return;
|
|
71
|
+
|
|
72
|
+
const onApiMessage = this._onApiMessage.bind(this);
|
|
73
|
+
broker.subscribeTmp('api', `activity.#.${executionId}`, onApiMessage, {
|
|
74
|
+
noAck: true,
|
|
75
|
+
consumerTag: `_api-${executionId}`,
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
const detachContent = cloneContent(executeContent, {
|
|
79
|
+
executionId: parent.executionId,
|
|
80
|
+
bindExchange: 'compensate',
|
|
81
|
+
});
|
|
82
|
+
detachContent.parent = shiftParent(parent);
|
|
83
|
+
|
|
84
|
+
broker.publish('event', 'activity.detach', detachContent);
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
proto.executeThrow = function executeThrow(executeMessage) {
|
|
88
|
+
const executeContent = executeMessage.content;
|
|
89
|
+
const {executionId, parent} = executeContent;
|
|
90
|
+
const parentExecutionId = parent && parent.executionId;
|
|
91
|
+
|
|
92
|
+
this.logger.debug(`<${executionId} (${this.activity.id})> throw compensate`);
|
|
93
|
+
|
|
94
|
+
const broker = this.broker;
|
|
95
|
+
const throwContent = cloneContent(executeContent, {
|
|
96
|
+
executionId: parentExecutionId,
|
|
97
|
+
state: 'throw',
|
|
98
|
+
});
|
|
99
|
+
throwContent.parent = shiftParent(parent);
|
|
100
|
+
broker.publish('event', 'activity.compensate', throwContent, {type: 'compensate', delegate: true});
|
|
101
|
+
|
|
102
|
+
return broker.publish('execution', 'execute.completed', cloneContent(executeContent));
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
proto._onCollect = function onCollect(routingKey, message) {
|
|
106
|
+
|
|
107
|
+
switch (routingKey) {
|
|
108
|
+
case 'execute.error':
|
|
109
|
+
case 'execute.completed': {
|
|
110
|
+
return this[kCompensateQ].queueMessage(message.fields, cloneContent(message.content), message.properties);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
};
|
|
38
114
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
broker.publish('execution', 'execute.detach', cloneContent(messageContent, {bindExchange: 'compensate'}));
|
|
115
|
+
proto._onCompensateApiMessage = function onCompensateApiMessage(routingKey, message) {
|
|
116
|
+
const output = message.content.message;
|
|
117
|
+
this[kCompleted] = true;
|
|
43
118
|
|
|
44
|
-
|
|
45
|
-
...messageContent,
|
|
46
|
-
executionId: parentExecutionId,
|
|
47
|
-
parent: shiftParent(parent),
|
|
48
|
-
bindExchange: 'compensate',
|
|
49
|
-
});
|
|
119
|
+
this._stop();
|
|
50
120
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
121
|
+
this._debug('caught compensate event');
|
|
122
|
+
const broker = this.broker;
|
|
123
|
+
const executeContent = this[kExecuteMessage].content;
|
|
124
|
+
const catchContent = cloneContent(executeContent, {
|
|
125
|
+
message: {...output},
|
|
126
|
+
executionId: executeContent.parent.executionId,
|
|
127
|
+
});
|
|
128
|
+
catchContent.parent = shiftParent(catchContent.parent);
|
|
59
129
|
|
|
60
|
-
|
|
61
|
-
const output = message.content.message;
|
|
62
|
-
completed = true;
|
|
130
|
+
broker.publish('event', 'activity.catch', catchContent, {type: 'catch'});
|
|
63
131
|
|
|
64
|
-
|
|
132
|
+
const compensateQ = this[kCompensateQ];
|
|
133
|
+
compensateQ.on('depleted', onDepleted);
|
|
134
|
+
compensateQ.consume(this._onCollected.bind(this), {noAck: true, consumerTag: '_convey-messages'});
|
|
65
135
|
|
|
66
|
-
|
|
67
|
-
broker.publish('event', 'activity.catch', {
|
|
68
|
-
...messageContent,
|
|
69
|
-
message: {...output},
|
|
70
|
-
executionId: parentExecutionId,
|
|
71
|
-
parent: shiftParent(executeMessage.content.parent),
|
|
72
|
-
}, {type: 'catch'});
|
|
136
|
+
for (const association of this[kAssociations]) association.complete(cloneMessage(message));
|
|
73
137
|
|
|
74
|
-
|
|
75
|
-
|
|
138
|
+
function onDepleted() {
|
|
139
|
+
compensateQ.off('depleted', onDepleted);
|
|
140
|
+
return broker.publish('execution', 'execute.completed', cloneContent(executeContent, {output, state: 'catch'}));
|
|
141
|
+
}
|
|
142
|
+
};
|
|
76
143
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
144
|
+
proto._onCollected = function onCollected(routingKey, message) {
|
|
145
|
+
for (const association of this[kAssociations]) association.take(cloneMessage(message));
|
|
146
|
+
};
|
|
80
147
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
return broker.publish('execution', 'execute.completed', {...messageContent, output, state: 'catch'});
|
|
84
|
-
}
|
|
85
|
-
}
|
|
148
|
+
proto._onApiMessage = function onApiMessage(routingKey, message) {
|
|
149
|
+
const messageType = message.properties.type;
|
|
86
150
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
});
|
|
151
|
+
switch (messageType) {
|
|
152
|
+
case 'compensate': {
|
|
153
|
+
return this._onCompensateApiMessage(routingKey, message);
|
|
91
154
|
}
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
case 'compensate': {
|
|
98
|
-
return onCompensateApiMessage(routingKey, message);
|
|
99
|
-
}
|
|
100
|
-
case 'discard': {
|
|
101
|
-
completed = true;
|
|
102
|
-
stop();
|
|
103
|
-
associations.forEach((association) => {
|
|
104
|
-
association.discard(cloneMessage(message));
|
|
105
|
-
});
|
|
106
|
-
return broker.publish('execution', 'execute.discard', {...messageContent});
|
|
107
|
-
}
|
|
108
|
-
case 'stop': {
|
|
109
|
-
stop();
|
|
110
|
-
break;
|
|
111
|
-
}
|
|
112
|
-
}
|
|
155
|
+
case 'discard': {
|
|
156
|
+
this[kCompleted] = true;
|
|
157
|
+
this._stop();
|
|
158
|
+
for (const association of this[kAssociations]) association.discard(cloneMessage(message));
|
|
159
|
+
return this.broker.publish('execution', 'execute.discard', cloneContent(this[kExecuteMessage].content));
|
|
113
160
|
}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
broker.cancel(`_oncompensate-${executionId}`);
|
|
118
|
-
broker.cancel('_oncollect-messages');
|
|
119
|
-
broker.cancel('_convey-messages');
|
|
161
|
+
case 'stop': {
|
|
162
|
+
this._stop();
|
|
163
|
+
break;
|
|
120
164
|
}
|
|
121
165
|
}
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
return broker.publish('execution', 'execute.completed', {...messageContent});
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
function setupCatch() {
|
|
141
|
-
broker.assertQueue(compensationQueueName, {autoDelete: false, durable: true});
|
|
142
|
-
broker.bindQueue(compensationQueueName, 'api', '*.compensate.#', {durable: true, priority: 400});
|
|
143
|
-
}
|
|
144
|
-
}
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
proto._stop = function stop() {
|
|
169
|
+
const broker = this.broker, executionId = this.executionId;
|
|
170
|
+
broker.cancel(`_api-${executionId}`);
|
|
171
|
+
broker.cancel(`_oncompensate-${executionId}`);
|
|
172
|
+
broker.cancel('_oncollect-messages');
|
|
173
|
+
broker.cancel('_convey-messages');
|
|
174
|
+
this[kMessageQ].purge();
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
proto._debug = function debug(msg) {
|
|
178
|
+
this.logger.debug(`<${this.executionId} (${this.activity.id})> ${msg}`);
|
|
179
|
+
};
|