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.
Files changed (116) hide show
  1. package/CHANGELOG.md +335 -0
  2. package/README.md +3 -0
  3. package/dist/index.js +52 -44
  4. package/dist/src/Api.js +77 -76
  5. package/dist/src/Context.js +176 -172
  6. package/dist/src/Environment.js +110 -102
  7. package/dist/src/EventBroker.js +89 -88
  8. package/dist/src/ExtensionsMapper.js +2 -2
  9. package/dist/src/MessageFormatter.js +164 -95
  10. package/dist/src/Scripts.js +6 -2
  11. package/dist/src/activity/Activity.js +1106 -916
  12. package/dist/src/activity/ActivityExecution.js +342 -297
  13. package/dist/src/activity/Dummy.js +3 -3
  14. package/dist/src/definition/Definition.js +498 -444
  15. package/dist/src/definition/DefinitionExecution.js +710 -408
  16. package/dist/src/error/Errors.js +17 -7
  17. package/dist/src/eventDefinitions/CancelEventDefinition.js +190 -150
  18. package/dist/src/eventDefinitions/CompensateEventDefinition.js +194 -161
  19. package/dist/src/eventDefinitions/ConditionalEventDefinition.js +197 -135
  20. package/dist/src/eventDefinitions/ErrorEventDefinition.js +207 -165
  21. package/dist/src/eventDefinitions/EscalationEventDefinition.js +175 -141
  22. package/dist/src/eventDefinitions/EventDefinitionExecution.js +157 -129
  23. package/dist/src/eventDefinitions/LinkEventDefinition.js +174 -149
  24. package/dist/src/eventDefinitions/MessageEventDefinition.js +213 -176
  25. package/dist/src/eventDefinitions/SignalEventDefinition.js +203 -161
  26. package/dist/src/eventDefinitions/TerminateEventDefinition.js +21 -23
  27. package/dist/src/eventDefinitions/TimerEventDefinition.js +243 -228
  28. package/dist/src/events/BoundaryEvent.js +180 -144
  29. package/dist/src/events/EndEvent.js +18 -23
  30. package/dist/src/events/IntermediateCatchEvent.js +44 -58
  31. package/dist/src/events/IntermediateThrowEvent.js +18 -23
  32. package/dist/src/events/StartEvent.js +109 -94
  33. package/dist/src/flows/Association.js +94 -100
  34. package/dist/src/flows/MessageFlow.js +86 -103
  35. package/dist/src/flows/SequenceFlow.js +173 -182
  36. package/dist/src/gateways/EventBasedGateway.js +88 -84
  37. package/dist/src/gateways/ExclusiveGateway.js +13 -16
  38. package/dist/src/gateways/InclusiveGateway.js +11 -14
  39. package/dist/src/gateways/ParallelGateway.js +11 -14
  40. package/dist/src/getPropertyValue.js +34 -34
  41. package/dist/src/io/BpmnIO.js +17 -14
  42. package/dist/src/io/EnvironmentDataObject.js +33 -29
  43. package/dist/src/io/EnvironmentDataStore.js +33 -29
  44. package/dist/src/io/EnvironmentDataStoreReference.js +35 -31
  45. package/dist/src/io/InputOutputSpecification.js +177 -168
  46. package/dist/src/io/Properties.js +117 -124
  47. package/dist/src/messageHelper.js +1 -1
  48. package/dist/src/process/Process.js +439 -362
  49. package/dist/src/process/ProcessExecution.js +748 -646
  50. package/dist/src/shared.js +2 -2
  51. package/dist/src/tasks/CallActivity.js +160 -0
  52. package/dist/src/tasks/LoopCharacteristics.js +309 -330
  53. package/dist/src/tasks/ReceiveTask.js +233 -182
  54. package/dist/src/tasks/ScriptTask.js +35 -41
  55. package/dist/src/tasks/ServiceImplementation.js +13 -20
  56. package/dist/src/tasks/ServiceTask.js +82 -75
  57. package/dist/src/tasks/SignalTask.js +97 -93
  58. package/dist/src/tasks/StandardLoopCharacteristics.js +1 -1
  59. package/dist/src/tasks/SubProcess.js +193 -175
  60. package/dist/src/tasks/Task.js +17 -19
  61. package/index.js +2 -0
  62. package/package.json +15 -15
  63. package/src/Api.js +65 -59
  64. package/src/Context.js +145 -140
  65. package/src/Environment.js +116 -100
  66. package/src/EventBroker.js +67 -68
  67. package/src/ExtensionsMapper.js +2 -2
  68. package/src/MessageFormatter.js +132 -74
  69. package/src/activity/Activity.js +915 -775
  70. package/src/activity/ActivityExecution.js +293 -247
  71. package/src/activity/Dummy.js +2 -2
  72. package/src/definition/Definition.js +437 -401
  73. package/src/definition/DefinitionExecution.js +598 -340
  74. package/src/error/Errors.js +11 -6
  75. package/src/eventDefinitions/CancelEventDefinition.js +164 -121
  76. package/src/eventDefinitions/CompensateEventDefinition.js +159 -124
  77. package/src/eventDefinitions/ConditionalEventDefinition.js +147 -104
  78. package/src/eventDefinitions/ErrorEventDefinition.js +190 -131
  79. package/src/eventDefinitions/EscalationEventDefinition.js +139 -101
  80. package/src/eventDefinitions/EventDefinitionExecution.js +127 -95
  81. package/src/eventDefinitions/LinkEventDefinition.js +160 -129
  82. package/src/eventDefinitions/MessageEventDefinition.js +178 -121
  83. package/src/eventDefinitions/SignalEventDefinition.js +162 -106
  84. package/src/eventDefinitions/TerminateEventDefinition.js +19 -19
  85. package/src/eventDefinitions/TimerEventDefinition.js +202 -167
  86. package/src/events/BoundaryEvent.js +156 -115
  87. package/src/events/EndEvent.js +15 -18
  88. package/src/events/IntermediateCatchEvent.js +40 -44
  89. package/src/events/IntermediateThrowEvent.js +15 -18
  90. package/src/events/StartEvent.js +84 -50
  91. package/src/flows/Association.js +98 -112
  92. package/src/flows/MessageFlow.js +81 -97
  93. package/src/flows/SequenceFlow.js +146 -160
  94. package/src/gateways/EventBasedGateway.js +75 -68
  95. package/src/gateways/ExclusiveGateway.js +8 -13
  96. package/src/gateways/InclusiveGateway.js +8 -13
  97. package/src/gateways/ParallelGateway.js +8 -13
  98. package/src/getPropertyValue.js +34 -33
  99. package/src/io/BpmnIO.js +20 -15
  100. package/src/io/EnvironmentDataObject.js +29 -18
  101. package/src/io/EnvironmentDataStore.js +29 -18
  102. package/src/io/EnvironmentDataStoreReference.js +31 -20
  103. package/src/io/InputOutputSpecification.js +154 -157
  104. package/src/io/Properties.js +95 -97
  105. package/src/process/Process.js +378 -333
  106. package/src/process/ProcessExecution.js +603 -553
  107. package/src/tasks/CallActivity.js +130 -0
  108. package/src/tasks/LoopCharacteristics.js +290 -289
  109. package/src/tasks/ReceiveTask.js +174 -107
  110. package/src/tasks/ScriptTask.js +27 -30
  111. package/src/tasks/ServiceImplementation.js +13 -18
  112. package/src/tasks/ServiceTask.js +67 -60
  113. package/src/tasks/SignalTask.js +77 -52
  114. package/src/tasks/StandardLoopCharacteristics.js +1 -1
  115. package/src/tasks/SubProcess.js +184 -157
  116. package/src/tasks/Task.js +15 -19
@@ -3,376 +3,421 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.default = ActivityExecution;
6
+ exports.default = void 0;
7
7
 
8
8
  var _Api = require("../Api");
9
9
 
10
10
  var _messageHelper = require("../messageHelper");
11
11
 
12
+ const kCompleted = Symbol.for('completed');
13
+ const kExecuteQ = Symbol.for('executeQ');
14
+ const kExecuteMessage = Symbol.for('executeMessage');
15
+ const kMessageHandlers = Symbol.for('messageHandlers');
16
+ const kPostponed = Symbol.for('postponed');
17
+ var _default = ActivityExecution;
18
+ exports.default = _default;
19
+
12
20
  function ActivityExecution(activity, context) {
13
- const {
14
- id,
15
- broker,
16
- logger,
17
- isSubProcess,
18
- Behaviour,
19
- environment
20
- } = activity;
21
- const {
22
- batchSize = 50
23
- } = environment.settings;
24
- const postponed = [];
25
- let source,
26
- initMessage,
27
- completed = false,
28
- executionId;
29
- const executeQ = broker.assertQueue('execute-q', {
21
+ this.activity = activity;
22
+ this.context = context;
23
+ this.id = activity.id;
24
+ this.broker = activity.broker;
25
+ this[kPostponed] = [];
26
+ this[kCompleted] = false;
27
+ this[kExecuteQ] = this.broker.assertQueue('execute-q', {
30
28
  durable: true,
31
29
  autoDelete: false
32
30
  });
33
- const executionApi = {
34
- get completed() {
35
- return completed;
36
- },
37
-
38
- get source() {
39
- return source;
40
- },
41
-
42
- discard,
43
- execute,
44
- passthrough,
45
- getApi,
46
- getPostponed,
47
- getState,
48
- recover,
49
- stop
31
+ this[kMessageHandlers] = {
32
+ onParentApiMessage: this._onParentApiMessage.bind(this),
33
+ onExecuteMessage: this._onExecuteMessage.bind(this)
50
34
  };
51
- return executionApi;
35
+ }
52
36
 
53
- function getPostponed() {
54
- let apis = postponed.map(msg => getApi(msg));
55
- if (!isSubProcess || !source) return apis;
56
- apis = apis.concat(source.getPostponed());
57
- return apis;
37
+ const proto = ActivityExecution.prototype;
38
+ Object.defineProperty(proto, 'completed', {
39
+ enumerable: true,
40
+
41
+ get() {
42
+ return this[kCompleted];
58
43
  }
59
44
 
60
- function execute(executeMessage) {
61
- if (!executeMessage) throw new Error('Execution requires message');
62
- if (!executeMessage.content || !executeMessage.content.executionId) throw new Error('Execution requires execution id');
63
- const isRedelivered = executeMessage.fields.redelivered;
64
- executionId = executeMessage.content.executionId;
65
- initMessage = (0, _messageHelper.cloneMessage)(executeMessage);
66
- initMessage.content = { ...initMessage.content,
67
- executionId,
68
- state: 'start',
69
- isRootScope: true
70
- };
71
-
72
- if (isRedelivered) {
73
- postponed.splice(0);
74
- logger.debug(`<${executionId} (${id})> resume execution`);
75
- if (!source) source = Behaviour(activity, context);
76
- activate();
77
- return broker.publish('execution', 'execute.resume.execution', (0, _messageHelper.cloneContent)(initMessage.content), {
78
- persistent: false
79
- });
80
- }
45
+ });
81
46
 
82
- logger.debug(`<${executionId} (${id})> execute`);
83
- activate();
84
- source = Behaviour(activity, context);
85
- broker.publish('execution', 'execute.start', (0, _messageHelper.cloneContent)(initMessage.content));
86
- }
47
+ proto.execute = function execute(executeMessage) {
48
+ if (!executeMessage) throw new Error('Execution requires message');
49
+ const executionId = executeMessage.content && executeMessage.content.executionId;
50
+ if (!executionId) throw new Error('Execution requires execution id');
51
+ this.executionId = executionId;
52
+ const initMessage = this[kExecuteMessage] = (0, _messageHelper.cloneMessage)(executeMessage, {
53
+ executionId,
54
+ state: 'start',
55
+ isRootScope: true
56
+ });
87
57
 
88
- function passthrough(executeMessage) {
89
- if (!source) return execute(executeMessage);
90
- return source.execute(executeMessage);
91
- }
58
+ if (executeMessage.fields.redelivered) {
59
+ this[kPostponed].splice(0);
92
60
 
93
- function discard() {
94
- if (completed) return;
95
- if (!initMessage) return logger.warn(`<${id}> is not executing`);
96
- getApi(initMessage).discard();
97
- }
61
+ this._debug('resume execution');
98
62
 
99
- function stop() {
100
- if (!initMessage) return;
101
- getApi(initMessage).stop();
63
+ if (!this.source) this.source = new this.activity.Behaviour(this.activity, this.context);
64
+ this.activate();
65
+ return this.broker.publish('execution', 'execute.resume.execution', (0, _messageHelper.cloneContent)(initMessage.content), {
66
+ persistent: false
67
+ });
102
68
  }
103
69
 
104
- function getState() {
105
- const result = {
106
- completed
107
- };
108
- if (!source || !source.getState) return result;
109
- return { ...result,
110
- ...source.getState()
111
- };
70
+ this._debug('execute');
71
+
72
+ this.activate();
73
+ this.source = new this.activity.Behaviour(this.activity, this.context);
74
+ this.broker.publish('execution', 'execute.start', (0, _messageHelper.cloneContent)(initMessage.content));
75
+ };
76
+
77
+ proto.activate = function activate() {
78
+ if (this[kCompleted]) return;
79
+ const broker = this.broker;
80
+ const batchSize = this.activity.environment.settings.batchSize || 50;
81
+ broker.bindQueue('execute-q', 'execution', 'execute.#', {
82
+ priority: 100
83
+ });
84
+ const {
85
+ onExecuteMessage,
86
+ onParentApiMessage
87
+ } = this[kMessageHandlers];
88
+ this[kExecuteQ].assertConsumer(onExecuteMessage, {
89
+ exclusive: true,
90
+ prefetch: batchSize * 2,
91
+ priority: 100,
92
+ consumerTag: '_activity-execute'
93
+ });
94
+ if (this[kCompleted]) return this.deactivate();
95
+ broker.subscribeTmp('api', `activity.*.${this.executionId}`, onParentApiMessage, {
96
+ noAck: true,
97
+ consumerTag: '_activity-api-execution',
98
+ priority: 200
99
+ });
100
+ };
101
+
102
+ proto.deactivate = function deactivate() {
103
+ const broker = this.broker;
104
+ broker.cancel('_activity-api-execution');
105
+ broker.cancel('_activity-execute');
106
+ broker.unbindQueue('execute-q', 'execution', 'execute.#');
107
+ };
108
+
109
+ proto.discard = function discard() {
110
+ if (this[kCompleted]) return;
111
+ const initMessage = this[kExecuteMessage];
112
+ if (!initMessage) return this.activity.logger.warn(`<${this.id}> is not executing`);
113
+ this.getApi(initMessage).discard();
114
+ };
115
+
116
+ proto.getApi = function getApi(apiMessage) {
117
+ const self = this;
118
+ if (!apiMessage) apiMessage = this[kExecuteMessage];
119
+
120
+ if (self.source.getApi) {
121
+ const sourceApi = self.source.getApi(apiMessage);
122
+ if (sourceApi) return sourceApi;
112
123
  }
113
124
 
114
- function recover(state) {
115
- postponed.splice(0);
116
- if (!state) return executionApi;
117
- if ('completed' in state) completed = state.completed;
118
- source = Behaviour(activity, context);
125
+ const api = (0, _Api.ActivityApi)(self.broker, apiMessage);
119
126
 
120
- if (source.recover) {
121
- source.recover(state);
122
- }
127
+ api.getExecuting = function getExecuting() {
128
+ return self[kPostponed].reduce((result, msg) => {
129
+ if (msg.content.executionId === apiMessage.content.executionId) return result;
130
+ result.push(self.getApi(msg));
131
+ return result;
132
+ }, []);
133
+ };
123
134
 
124
- return executionApi;
125
- }
135
+ return api;
136
+ };
126
137
 
127
- function activate() {
128
- if (completed) return;
129
- broker.bindQueue(executeQ.name, 'execution', 'execute.#', {
130
- priority: 100
131
- });
132
- executeQ.assertConsumer(onExecuteMessage, {
133
- exclusive: true,
134
- prefetch: batchSize * 2,
135
- priority: 100,
136
- consumerTag: '_activity-execute'
137
- });
138
- if (completed) return deactivate();
139
- broker.subscribeTmp('api', `activity.*.${executionId}`, onParentApiMessage, {
140
- noAck: true,
141
- consumerTag: '_activity-api-execution',
142
- priority: 200
143
- });
144
- }
138
+ proto.passthrough = function passthrough(executeMessage) {
139
+ if (!this.source) return this.execute(executeMessage);
140
+ return this._sourceExecute(executeMessage);
141
+ };
145
142
 
146
- function deactivate() {
147
- broker.cancel('_activity-api-execution');
148
- broker.cancel('_activity-execute');
149
- broker.unbindQueue(executeQ.name, 'execution', 'execute.#');
150
- }
143
+ proto.getPostponed = function getPostponed() {
144
+ let apis = this[kPostponed].map(msg => this.getApi(msg));
145
+ if (!this.activity.isSubProcess || !this.source) return apis;
146
+ apis = apis.concat(this.source.getPostponed());
147
+ return apis;
148
+ };
151
149
 
152
- function onParentApiMessage(routingKey, message) {
153
- const messageType = message.properties.type;
150
+ proto.getState = function getState() {
151
+ const result = {
152
+ completed: this[kCompleted]
153
+ };
154
+ const source = this.source;
155
+ if (!source || !source.getState) return result;
156
+ return { ...result,
157
+ ...source.getState()
158
+ };
159
+ };
154
160
 
155
- switch (messageType) {
156
- case 'discard':
157
- executeQ.queueMessage({
158
- routingKey: 'execute.discard'
159
- }, (0, _messageHelper.cloneContent)(initMessage.content));
160
- break;
161
+ proto.recover = function recover(state) {
162
+ this[kPostponed].splice(0);
163
+ if (!state) return this;
164
+ if ('completed' in state) this[kCompleted] = state.completed;
165
+ const source = this.source = new this.activity.Behaviour(this.activity, this.context);
161
166
 
162
- case 'stop':
163
- onStop(message);
164
- break;
165
- }
167
+ if (source.recover) {
168
+ source.recover(state);
166
169
  }
167
170
 
168
- function onStop(message) {
169
- const stoppedId = message && message.content && message.content.executionId;
170
- const running = getPostponed();
171
- running.forEach(api => {
172
- if (stoppedId !== api.content.executionId) {
173
- api.stop();
174
- }
175
- });
176
- broker.cancel('_activity-execute');
177
- broker.cancel('_activity-api-execution');
178
- }
171
+ return this;
172
+ };
173
+
174
+ proto.stop = function stop() {
175
+ const executeMessage = this[kExecuteMessage];
176
+ if (!executeMessage) return;
177
+ this.getApi(executeMessage).stop();
178
+ };
179
179
 
180
- function onExecuteMessage(routingKey, message) {
181
- const {
182
- fields = {},
183
- content = {},
184
- properties = {}
185
- } = message;
186
- const isRedelivered = fields.redelivered;
187
- const {
188
- isRootScope,
189
- ignoreIfExecuting,
190
- keep,
191
- executionId: cexid,
180
+ proto._sourceExecute = function sourceExecute(executeMessage) {
181
+ try {
182
+ return this.source.execute(executeMessage);
183
+ } catch (error) {
184
+ return this.broker.publish('execution', 'execute.error', (0, _messageHelper.cloneContent)(executeMessage.content, {
192
185
  error
193
- } = content;
194
- const {
195
- persistent,
196
- correlationId
197
- } = properties;
198
- if (isRedelivered && persistent === false) return message.ack();
199
-
200
- switch (routingKey) {
201
- case 'execute.resume.execution':
202
- {
203
- if (!postponed.length) return broker.publish('execution', 'execute.start', (0, _messageHelper.cloneContent)(initMessage.content));
204
- break;
205
- }
186
+ }));
187
+ }
188
+ };
206
189
 
207
- case 'execute.error':
208
- case 'execute.discard':
209
- executionDiscard();
190
+ proto._onExecuteMessage = function onExecuteMessage(routingKey, message) {
191
+ const {
192
+ fields,
193
+ content,
194
+ properties
195
+ } = message;
196
+ const isRedelivered = fields.redelivered;
197
+ if (isRedelivered && properties.persistent === false) return message.ack();
198
+
199
+ switch (routingKey) {
200
+ case 'execute.resume.execution':
201
+ {
202
+ if (!this[kPostponed].length) return this.broker.publish('execution', 'execute.start', (0, _messageHelper.cloneContent)(this[kExecuteMessage].content));
210
203
  break;
204
+ }
211
205
 
212
- case 'execute.cancel':
213
- case 'execute.completed':
214
- {
215
- if (isRedelivered) {
216
- message.ack();
217
- return broker.publish('execution', routingKey, getExecuteMessage().content);
218
- }
206
+ case 'execute.error':
207
+ case 'execute.discard':
208
+ return this._onExecutionDiscarded(message);
219
209
 
220
- executionCompleted();
221
- break;
210
+ case 'execute.cancel':
211
+ case 'execute.completed':
212
+ {
213
+ if (isRedelivered) {
214
+ message.ack();
215
+ return this.broker.publish('execution', routingKey, getExecuteMessage(message).content);
222
216
  }
223
217
 
224
- case 'execute.start':
225
- {
226
- if (!stateChangeMessage()) return;
227
- return source.execute(getExecuteMessage());
228
- }
218
+ return this._onExecutionCompleted(message);
219
+ }
229
220
 
230
- case 'execute.outbound.take':
231
- {
232
- if (isRedelivered) {
233
- message.ack();
234
- break;
235
- }
221
+ case 'execute.start':
222
+ {
223
+ if (!this._onStateChangeMessage(message)) return;
224
+ return this._sourceExecute(getExecuteMessage(message));
225
+ }
236
226
 
237
- broker.publish('execution', 'execution.outbound.take', (0, _messageHelper.cloneContent)(content), {
238
- type: 'outbound'
239
- });
227
+ case 'execute.outbound.take':
228
+ {
229
+ if (isRedelivered) {
230
+ message.ack();
240
231
  break;
241
232
  }
242
233
 
243
- default:
244
- {
245
- if (!stateChangeMessage()) return;
234
+ this.broker.publish('execution', 'execution.outbound.take', (0, _messageHelper.cloneContent)(content), {
235
+ type: 'outbound'
236
+ });
237
+ break;
238
+ }
239
+
240
+ default:
241
+ {
242
+ if (!this._onStateChangeMessage(message)) return;
246
243
 
247
- if (isRedelivered) {
248
- return source.execute(getExecuteMessage());
249
- }
244
+ if (isRedelivered) {
245
+ return this._sourceExecute(getExecuteMessage(message));
250
246
  }
247
+ }
248
+ }
249
+ };
250
+
251
+ proto._onStateChangeMessage = function onStateChangeMessage(message) {
252
+ const {
253
+ ignoreIfExecuting,
254
+ executionId
255
+ } = message.content;
256
+ const postponed = this[kPostponed];
257
+ const idx = postponed.findIndex(msg => msg.content.executionId === executionId);
258
+ let previousMsg;
259
+
260
+ if (idx > -1) {
261
+ if (ignoreIfExecuting) {
262
+ message.ack();
263
+ return false;
251
264
  }
252
265
 
253
- function stateChangeMessage() {
254
- const idx = postponed.findIndex(msg => msg.content.executionId === cexid);
255
- let previousMsg;
266
+ previousMsg = postponed.splice(idx, 1, message)[0];
267
+ previousMsg.ack();
268
+ return true;
269
+ }
256
270
 
257
- if (idx > -1) {
258
- if (ignoreIfExecuting) {
259
- message.ack();
260
- return false;
261
- }
271
+ postponed.push(message);
272
+ return true;
273
+ };
262
274
 
263
- previousMsg = postponed.splice(idx, 1, message)[0];
264
- previousMsg.ack();
265
- return true;
266
- }
275
+ proto._onExecutionCompleted = function onExecutionCompleted(message) {
276
+ const postponedMsg = this._ackPostponed(message);
267
277
 
268
- postponed.push(message);
269
- return true;
270
- }
278
+ if (!postponedMsg) return;
279
+ const postponed = this[kPostponed];
280
+ const {
281
+ executionId,
282
+ keep,
283
+ isRootScope
284
+ } = message.content;
271
285
 
272
- function getExecuteMessage() {
273
- const result = (0, _messageHelper.cloneMessage)(message, { ...(isRedelivered ? {
274
- isRecovered: true
275
- } : undefined)
276
- });
277
- result.content.ignoreIfExecuting = undefined;
278
- return result;
286
+ if (!isRootScope) {
287
+ this._debug('completed sub execution');
288
+
289
+ if (!keep) message.ack();
290
+
291
+ if (postponed.length === 1 && postponed[0].content.isRootScope && !postponed[0].content.preventComplete) {
292
+ return this.broker.publish('execution', 'execute.completed', (0, _messageHelper.cloneContent)(postponed[0].content));
279
293
  }
280
294
 
281
- function executionCompleted() {
282
- const postponedMsg = ackPostponed(message);
283
- if (!postponedMsg) return;
295
+ return;
296
+ }
284
297
 
285
- if (!isRootScope) {
286
- logger.debug(`<${cexid} (${id})> completed sub execution`);
287
- if (!keep) message.ack();
298
+ this._debug('completed execution', executionId);
288
299
 
289
- if (postponed.length === 1 && postponed[0].content.isRootScope && !postponed[0].content.preventComplete) {
290
- return broker.publish('execution', 'execute.completed', (0, _messageHelper.cloneContent)(postponed[0].content));
291
- }
300
+ this[kCompleted] = true;
301
+ message.ack(true);
302
+ this.deactivate();
303
+ const subApis = this.getPostponed();
304
+ postponed.splice(0);
292
305
 
293
- return;
294
- }
306
+ for (const api of subApis) api.discard();
295
307
 
296
- logger.debug(`<${cexid} (${id})> completed execution`);
297
- completed = true;
298
- message.ack(true);
299
- deactivate();
300
- const subApis = getPostponed();
301
- postponed.splice(0);
302
- subApis.forEach(api => api.discard());
303
- publishExecutionCompleted('completed', { ...postponedMsg.content,
304
- ...message.content
308
+ this._publishExecutionCompleted('completed', { ...postponedMsg.content,
309
+ ...message.content
310
+ }, message.properties.correlationId);
311
+ };
312
+
313
+ proto._onExecutionDiscarded = function onExecutionDiscarded(message) {
314
+ const postponedMsg = this._ackPostponed(message);
315
+
316
+ const {
317
+ isRootScope,
318
+ error
319
+ } = message.content;
320
+ if (!isRootScope && !postponedMsg) return;
321
+ const postponed = this[kPostponed];
322
+ const correlationId = message.properties.correlationId;
323
+
324
+ if (!error && !isRootScope) {
325
+ message.ack();
326
+
327
+ if (postponed.length === 1 && postponed[0].content.isRootScope) {
328
+ return this.broker.publish('execution', 'execute.discard', postponed[0].content, {
329
+ correlationId
305
330
  });
306
331
  }
307
332
 
308
- function executionDiscard() {
309
- const postponedMsg = ackPostponed(message);
310
- if (!isRootScope && !postponedMsg) return;
333
+ return;
334
+ }
311
335
 
312
- if (!error && !isRootScope) {
313
- message.ack();
336
+ message.ack(true);
337
+ this.deactivate();
338
+ const subApis = this.getPostponed();
339
+ postponed.splice(0);
314
340
 
315
- if (postponed.length === 1 && postponed[0].content.isRootScope) {
316
- return broker.publish('execution', 'execute.discard', { ...postponed[0].content
317
- }, {
318
- correlationId
319
- });
320
- }
341
+ for (const api of subApis) api.discard();
321
342
 
322
- return;
323
- }
343
+ if (error) {
344
+ return this._publishExecutionCompleted('error', (0, _messageHelper.cloneContent)(message.content, {
345
+ error
346
+ }), correlationId);
347
+ }
324
348
 
325
- message.ack(true);
326
- deactivate();
327
- const subApis = getPostponed();
328
- postponed.splice(0);
329
- subApis.forEach(api => api.discard());
330
- publishExecutionCompleted(error ? 'error' : 'discard', { ...content
331
- });
332
- }
349
+ this._publishExecutionCompleted('discard', message.content, correlationId);
350
+ };
333
351
 
334
- function publishExecutionCompleted(completionType, completeContent) {
335
- completed = true;
336
- broker.publish('execution', `execution.${completionType}`, { ...completeContent,
337
- state: completionType
352
+ proto._publishExecutionCompleted = function publishExecutionCompleted(completionType, completeContent, correlationId) {
353
+ this[kCompleted] = true;
354
+ this.broker.publish('execution', `execution.${completionType}`, { ...completeContent,
355
+ state: completionType
356
+ }, {
357
+ type: completionType,
358
+ correlationId
359
+ });
360
+ };
361
+
362
+ proto._ackPostponed = function ackPostponed(completeMessage) {
363
+ const {
364
+ executionId: eid
365
+ } = completeMessage.content;
366
+ const postponed = this[kPostponed];
367
+ const idx = postponed.findIndex(({
368
+ content: c
369
+ }) => c.executionId === eid);
370
+ if (idx === -1) return;
371
+ const [msg] = postponed.splice(idx, 1);
372
+ msg.ack();
373
+ return msg;
374
+ };
375
+
376
+ proto._onParentApiMessage = function onParentApiMessage(routingKey, message) {
377
+ switch (message.properties.type) {
378
+ case 'error':
379
+ return this[kExecuteQ].queueMessage({
380
+ routingKey: 'execute.error'
338
381
  }, {
339
- type: completionType,
340
- correlationId
382
+ error: message.content.error
341
383
  });
342
- }
343
- }
344
384
 
345
- function ackPostponed(completeMessage) {
346
- const {
347
- executionId: eid
348
- } = completeMessage.content;
349
- const idx = postponed.findIndex(({
350
- content
351
- }) => content.executionId === eid);
352
- if (idx === -1) return;
353
- const [msg] = postponed.splice(idx, 1);
354
- msg.ack();
355
- return msg;
385
+ case 'discard':
386
+ return this[kExecuteQ].queueMessage({
387
+ routingKey: 'execute.discard'
388
+ }, (0, _messageHelper.cloneContent)(this[kExecuteMessage].content));
389
+
390
+ case 'stop':
391
+ {
392
+ return this._onStop(message);
393
+ }
356
394
  }
395
+ };
357
396
 
358
- function getApi(apiMessage) {
359
- if (!apiMessage) apiMessage = initMessage;
397
+ proto._onStop = function onStop(message) {
398
+ const stoppedId = message && message.content && message.content.executionId;
399
+ const running = this.getPostponed();
360
400
 
361
- if (source.getApi) {
362
- const sourceApi = source.getApi(apiMessage);
363
- if (sourceApi) return sourceApi;
401
+ for (const api of running) {
402
+ if (stoppedId !== api.content.executionId) {
403
+ api.stop();
364
404
  }
405
+ }
365
406
 
366
- const api = (0, _Api.ActivityApi)(broker, apiMessage);
407
+ this.broker.cancel('_activity-execute');
408
+ this.broker.cancel('_activity-api-execution');
409
+ };
367
410
 
368
- api.getExecuting = function getExecuting() {
369
- return postponed.reduce((result, msg) => {
370
- if (msg.content.executionId === apiMessage.content.executionId) return result;
371
- result.push(getApi(msg));
372
- return result;
373
- }, []);
374
- };
411
+ proto._debug = function debug(logMessage, executionId) {
412
+ executionId = executionId || this.executionId;
413
+ this.activity.logger.debug(`<${executionId} (${this.id})> ${logMessage}`);
414
+ };
375
415
 
376
- return api;
377
- }
416
+ function getExecuteMessage(message) {
417
+ const result = (0, _messageHelper.cloneMessage)(message, { ...(message.fields.redelivered ? {
418
+ isRecovered: true
419
+ } : undefined),
420
+ ignoreIfExecuting: undefined
421
+ });
422
+ return result;
378
423
  }