bpmn-elements 6.0.1 → 8.0.1

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 +341 -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 +1105 -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 +16 -16
  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 +914 -776
  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
@@ -2,389 +2,434 @@ import ProcessExecution from './ProcessExecution';
2
2
  import {getUniqueId} from '../shared';
3
3
  import {ProcessApi} from '../Api';
4
4
  import {ProcessBroker} from '../EventBroker';
5
- import {cloneMessage, cloneContent} from '../messageHelper';
5
+ import {cloneMessage, cloneContent, cloneParent} from '../messageHelper';
6
6
  import {makeErrorFromMessage} from '../error/Errors';
7
7
 
8
+ const kConsuming = Symbol.for('consuming');
9
+ const kCounters = Symbol.for('counters');
10
+ const kExec = Symbol.for('execution');
11
+ const kExecuteMessage = Symbol.for('executeMessage');
12
+ const kExtensions = Symbol.for('extensions');
13
+ const kMessageHandlers = Symbol.for('messageHandlers');
14
+ const kStateMessage = Symbol.for('stateMessage');
15
+ const kStatus = Symbol.for('status');
16
+ const kStopped = Symbol.for('stopped');
17
+
8
18
  export default Process;
9
19
 
10
20
  export function Process(processDef, context) {
11
21
  const {id, type = 'process', name, parent, behaviour = {}} = processDef;
12
- const environment = context.environment;
13
- const {isExecutable} = behaviour;
14
-
15
- const logger = environment.Logger(type.toLowerCase());
22
+ this.id = id;
23
+ this.type = type;
24
+ this.name = name;
25
+ this.parent = parent ? cloneParent(parent) : {};
26
+ this.behaviour = behaviour;
16
27
 
17
- let execution, initExecutionId, executionId, status, stopped, postponedMessage, stateMessage, consumingRunQ;
28
+ const {isExecutable} = behaviour;
29
+ this.isExecutable = isExecutable;
18
30
 
19
- let counters = {
31
+ const environment = this.environment = context.environment;
32
+ this.context = context;
33
+ this[kCounters] = {
20
34
  completed: 0,
21
35
  discarded: 0,
22
- terminated: 0,
23
36
  };
24
-
25
- const processApi = {
26
- id,
27
- type,
28
- name,
29
- isExecutable,
30
- behaviour,
31
- get counters() {
32
- return {...counters};
33
- },
34
- get executionId() {
35
- return executionId;
36
- },
37
- get status() {
38
- return status;
39
- },
40
- get stopped() {
41
- return stopped;
42
- },
43
- get execution() {
44
- return execution;
45
- },
46
- get isRunning() {
47
- if (!consumingRunQ) return false;
48
- return !!status;
49
- },
50
- context,
51
- environment,
52
- parent: {...parent},
53
- logger,
54
- getApi,
55
- getActivities,
56
- getActivityById,
57
- getSequenceFlows,
58
- getPostponed,
59
- getStartActivities,
60
- getState,
61
- init,
62
- recover,
63
- resume,
64
- run,
65
- sendMessage,
66
- shake,
67
- signal,
68
- cancelActivity,
69
- stop,
37
+ this[kConsuming] = false;
38
+ this[kExec] = {};
39
+ this[kStatus] = undefined;
40
+ this[kStopped] = false;
41
+
42
+ const {broker, on, once, waitFor} = ProcessBroker(this);
43
+ this.broker = broker;
44
+ this.on = on;
45
+ this.once = once;
46
+ this.waitFor = waitFor;
47
+
48
+ this[kMessageHandlers] = {
49
+ onApiMessage: this._onApiMessage.bind(this),
50
+ onRunMessage: this._onRunMessage.bind(this),
51
+ onExecutionMessage: this._onExecutionMessage.bind(this),
70
52
  };
71
53
 
72
- const {broker, on, once, waitFor} = ProcessBroker(processApi);
73
-
74
- processApi.on = on;
75
- processApi.once = once;
76
- processApi.waitFor = waitFor;
77
-
78
- const runQ = broker.getQueue('run-q');
79
- const executionQ = broker.getQueue('execution-q');
80
-
81
- Object.defineProperty(processApi, 'broker', {
82
- enumerable: true,
83
- get: () => broker,
84
- });
85
-
86
- const extensions = context.loadExtensions(processApi);
87
- Object.defineProperty(processApi, 'extensions', {
88
- enumerable: true,
89
- get: () => extensions,
90
- });
54
+ this.logger = environment.Logger(type.toLowerCase());
91
55
 
92
- return processApi;
56
+ this[kExtensions] = context.loadExtensions(this);
57
+ }
93
58
 
94
- function init() {
95
- initExecutionId = getUniqueId(id);
96
- logger.debug(`<${id}> initialized with executionId <${initExecutionId}>`);
97
- publishEvent('init', createMessage({executionId: initExecutionId}));
59
+ const proto = Process.prototype;
60
+
61
+ Object.defineProperty(proto, 'counters', {
62
+ enumerable: true,
63
+ get() {
64
+ return {...this[kCounters]};
65
+ },
66
+ });
67
+
68
+ Object.defineProperty(proto, 'extensions', {
69
+ enumerable: true,
70
+ get() {
71
+ return this[kExtensions];
72
+ },
73
+ });
74
+
75
+ Object.defineProperty(proto, 'stopped', {
76
+ enumerable: true,
77
+ get() {
78
+ return this[kStopped];
79
+ },
80
+ });
81
+
82
+ Object.defineProperty(proto, 'isRunning', {
83
+ enumerable: true,
84
+ get() {
85
+ if (!this[kConsuming]) return false;
86
+ return !!this.status;
87
+ },
88
+ });
89
+
90
+ Object.defineProperty(proto, 'executionId', {
91
+ enumerable: true,
92
+ get() {
93
+ const {executionId, initExecutionId} = this[kExec];
94
+ return executionId || initExecutionId;
95
+ },
96
+ });
97
+
98
+ Object.defineProperty(proto, 'execution', {
99
+ enumerable: true,
100
+ get() {
101
+ return this[kExec].execution;
102
+ },
103
+ });
104
+
105
+ Object.defineProperty(proto, 'status', {
106
+ enumerable: true,
107
+ get() {
108
+ return this[kStatus];
109
+ },
110
+ });
111
+
112
+ proto.init = function init(useAsExecutionId) {
113
+ const exec = this[kExec];
114
+ const initExecutionId = exec.initExecutionId = useAsExecutionId || getUniqueId(this.id);
115
+ this._debug(`initialized with executionId <${initExecutionId}>`);
116
+ this._publishEvent('init', this._createMessage({executionId: initExecutionId}));
117
+ };
118
+
119
+ proto.run = function run(runContent) {
120
+ if (this.isRunning) throw new Error(`process <${this.id}> is already running`);
121
+
122
+ const exec = this[kExec];
123
+ const executionId = exec.executionId = exec.initExecutionId || getUniqueId(this.id);
124
+ exec.initExecutionId = undefined;
125
+
126
+ const content = this._createMessage({...runContent, executionId});
127
+
128
+ const broker = this.broker;
129
+ broker.publish('run', 'run.enter', content);
130
+ broker.publish('run', 'run.start', cloneContent(content));
131
+ broker.publish('run', 'run.execute', cloneContent(content));
132
+
133
+ this._activateRunConsumers();
134
+ };
135
+
136
+ proto.resume = function resume() {
137
+ if (this.isRunning) throw new Error(`cannot resume running process <${this.id}>`);
138
+ if (!this.status) return this;
139
+
140
+ this[kStopped] = false;
141
+
142
+ const content = this._createMessage();
143
+ this.broker.publish('run', 'run.resume', content, {persistent: false});
144
+ this._activateRunConsumers();
145
+ return this;
146
+ };
147
+
148
+ proto.recover = function recover(state) {
149
+ if (this.isRunning) throw new Error(`cannot recover running process <${this.id}>`);
150
+ if (!state) return this;
151
+
152
+ this[kStopped] = !!state.stopped;
153
+ this[kStatus] = state.status;
154
+ const exec = this[kExec];
155
+ exec.executionId = state.executionId;
156
+ this[kCounters] = {...this[kCounters], ...state.counters};
157
+ this.environment.recover(state.environment);
158
+
159
+ if (state.execution) {
160
+ exec.execution = new ProcessExecution(this, this.context).recover(state.execution);
98
161
  }
99
162
 
100
- function run(runContent) {
101
- if (processApi.isRunning) throw new Error(`process <${id}> is already running`);
102
-
103
- executionId = initExecutionId || getUniqueId(id);
104
- initExecutionId = undefined;
105
-
106
- const content = createMessage({...runContent, executionId});
107
-
108
- broker.publish('run', 'run.enter', content);
109
- broker.publish('run', 'run.start', cloneContent(content));
110
- broker.publish('run', 'run.execute', cloneContent(content));
111
-
112
- activateRunConsumers();
163
+ this.broker.recover(state.broker);
164
+
165
+ return this;
166
+ };
167
+
168
+ proto.shake = function shake(startId) {
169
+ if (this.isRunning) return this.execution.shake(startId);
170
+ return new ProcessExecution(this, this.context).shake(startId);
171
+ };
172
+
173
+ proto.stop = function stop() {
174
+ if (!this.isRunning) return;
175
+ this.getApi().stop();
176
+ };
177
+
178
+ proto.getApi = function getApi(message) {
179
+ const execution = this.execution;
180
+ if (execution) return execution.getApi(message);
181
+ return ProcessApi(this.broker, message || this[kStateMessage]);
182
+ };
183
+
184
+ proto.signal = function signal(message) {
185
+ return this.getApi().signal(message, {delegate: true});
186
+ };
187
+
188
+ proto.getState = function getState() {
189
+ return this._createMessage({
190
+ environment: this.environment.getState(),
191
+ status: this.status,
192
+ stopped: this.stopped,
193
+ counters: this.counters,
194
+ broker: this.broker.getState(true),
195
+ execution: this.execution && this.execution.getState(),
196
+ });
197
+ };
198
+
199
+ proto.cancelActivity = function cancelActivity(message) {
200
+ return this.getApi().cancel(message, {delegate: true});
201
+ };
202
+
203
+ proto._activateRunConsumers = function activateRunConsumers() {
204
+ this[kConsuming] = true;
205
+ const broker = this.broker;
206
+ const {onApiMessage, onRunMessage} = this[kMessageHandlers];
207
+ broker.subscribeTmp('api', `process.*.${this.executionId}`, onApiMessage, {noAck: true, consumerTag: '_process-api', priority: 100});
208
+ broker.getQueue('run-q').assertConsumer(onRunMessage, {exclusive: true, consumerTag: '_process-run'});
209
+ };
210
+
211
+ proto._deactivateRunConsumers = function deactivateRunConsumers() {
212
+ const broker = this.broker;
213
+ broker.cancel('_process-api');
214
+ broker.cancel('_process-run');
215
+ broker.cancel('_process-execution');
216
+ this[kConsuming] = false;
217
+ };
218
+
219
+ proto._onRunMessage = function onRunMessage(routingKey, message) {
220
+ const {content, fields} = message;
221
+
222
+ if (routingKey === 'run.resume') {
223
+ return this._onResumeMessage(message);
113
224
  }
114
225
 
115
- function resume() {
116
- if (processApi.isRunning) throw new Error(`cannot resume running process <${id}>`);
117
- if (!status) return processApi;
226
+ const exec = this[kExec];
227
+ this[kStateMessage] = message;
118
228
 
119
- stopped = false;
229
+ switch (routingKey) {
230
+ case 'run.enter': {
231
+ this._debug('enter');
120
232
 
121
- const content = createMessage({executionId});
122
- broker.publish('run', 'run.resume', content, {persistent: false});
123
- activateRunConsumers();
124
- return processApi;
125
- }
233
+ this[kStatus] = 'entered';
234
+ if (fields.redelivered) break;
126
235
 
127
- function recover(state) {
128
- if (processApi.isRunning) throw new Error(`cannot recover running process <${id}>`);
129
- if (!state) return processApi;
236
+ exec.execution = undefined;
237
+ this._publishEvent('enter', content);
130
238
 
131
- stopped = state.stopped;
132
- status = state.status;
133
- executionId = state.executionId;
134
- counters = {...counters, ...state.counters};
135
-
136
- if (state.execution) {
137
- execution = ProcessExecution(processApi, context).recover(state.execution);
239
+ break;
138
240
  }
139
-
140
- broker.recover(state.broker);
141
-
142
- return processApi;
143
- }
144
-
145
- function shake(startId) {
146
- if (processApi.isRunning) return execution.shake(startId);
147
- return ProcessExecution(processApi, context).shake(startId);
148
- }
149
-
150
- function activateRunConsumers() {
151
- consumingRunQ = true;
152
- broker.subscribeTmp('api', `process.*.${executionId}`, onApiMessage, {noAck: true, consumerTag: '_process-api', priority: 100});
153
- runQ.assertConsumer(onRunMessage, {exclusive: true, consumerTag: '_process-run'});
154
- }
155
-
156
- function deactivateRunConsumers() {
157
- broker.cancel('_process-api');
158
- broker.cancel('_process-run');
159
- broker.cancel('_process-execution');
160
- consumingRunQ = false;
161
- }
162
-
163
- function stop() {
164
- if (!processApi.isRunning) return;
165
- getApi().stop();
166
- }
167
-
168
- function getApi(message) {
169
- if (execution) return execution.getApi(message);
170
- return ProcessApi(broker, message || stateMessage);
171
- }
172
-
173
- function signal(message) {
174
- return getApi().signal(message, {delegate: true});
175
- }
176
-
177
- function cancelActivity(message) {
178
- return getApi().cancel(message, {delegate: true});
179
- }
180
-
181
- function onRunMessage(routingKey, message) {
182
- const {content, ack, fields} = message;
183
-
184
- if (routingKey === 'run.resume') {
185
- return onResumeMessage();
241
+ case 'run.start': {
242
+ this._debug('start');
243
+ this[kStatus] = 'start';
244
+ this._publishEvent('start', content);
245
+ break;
186
246
  }
187
-
188
- stateMessage = message;
189
-
190
- switch (routingKey) {
191
- case 'run.enter': {
192
- logger.debug(`<${id}> enter`);
193
-
194
- status = 'entered';
195
- if (fields.redelivered) break;
196
-
197
- execution = undefined;
198
- publishEvent('enter', content);
199
-
200
- break;
201
- }
202
- case 'run.start': {
203
- logger.debug(`<${id}> start`);
204
- status = 'start';
205
- publishEvent('start', content);
206
- break;
207
- }
208
- case 'run.execute': {
209
- status = 'executing';
210
- const executeMessage = cloneMessage(message);
211
- if (fields.redelivered && !execution) {
212
- executeMessage.fields.redelivered = undefined;
213
- }
214
- postponedMessage = message;
215
- executionQ.assertConsumer(onExecutionMessage, {exclusive: true, consumerTag: '_process-execution'});
216
- execution = execution || ProcessExecution(processApi, context);
217
- return execution.execute(executeMessage);
218
- }
219
- case 'run.error': {
220
- publishEvent('error', cloneContent(content, {
221
- error: fields.redelivered ? makeErrorFromMessage(message) : content.error,
222
- }));
223
- break;
224
- }
225
- case 'run.end': {
226
- status = 'end';
227
-
228
- if (fields.redelivered) break;
229
- logger.debug(`<${id}> completed`);
230
-
231
- counters.completed++;
232
-
233
- broker.publish('run', 'run.leave', content);
234
- publishEvent('end', content);
235
- break;
247
+ case 'run.execute': {
248
+ this[kStatus] = 'executing';
249
+ const executeMessage = cloneMessage(message);
250
+ if (fields.redelivered && !exec.execution) {
251
+ executeMessage.fields.redelivered = undefined;
236
252
  }
237
- case 'run.discarded': {
238
- status = 'discarded';
239
- if (fields.redelivered) break;
253
+ this[kExecuteMessage] = message;
240
254
 
241
- counters.discarded++;
255
+ this.broker.getQueue('execution-q').assertConsumer(this[kMessageHandlers].onExecutionMessage, {
256
+ exclusive: true,
257
+ consumerTag: '_process-execution',
258
+ });
242
259
 
243
- broker.publish('run', 'run.leave', content);
244
- break;
245
- }
246
- case 'run.leave': {
247
- status = undefined;
248
- broker.cancel('_process-api');
249
- publishEvent('leave');
250
- break;
251
- }
260
+ const execution = exec.execution = exec.execution || new ProcessExecution(this, this.context);
261
+ return execution.execute(executeMessage);
252
262
  }
263
+ case 'run.error': {
264
+ this[kStatus] = 'errored';
265
+ this._publishEvent('error', cloneContent(content, {
266
+ error: fields.redelivered ? makeErrorFromMessage(message) : content.error,
267
+ }));
268
+ break;
269
+ }
270
+ case 'run.end': {
271
+ this[kStatus] = 'end';
253
272
 
254
- ack();
255
-
256
- function onResumeMessage() {
257
- message.ack();
258
-
259
- switch (stateMessage.fields.routingKey) {
260
- case 'run.enter':
261
- case 'run.start':
262
- case 'run.discarded':
263
- case 'run.end':
264
- case 'run.leave':
265
- break;
266
- default:
267
- return;
268
- }
273
+ if (fields.redelivered) break;
274
+ this._debug('completed');
269
275
 
270
- if (!stateMessage.fields.redelivered) return;
276
+ this[kCounters].completed++;
271
277
 
272
- logger.debug(`<${id}> resume from ${status}`);
278
+ this.broker.publish('run', 'run.leave', content);
273
279
 
274
- return broker.publish('run', stateMessage.fields.routingKey, cloneContent(stateMessage.content), stateMessage.properties);
280
+ this._publishEvent('end', content);
281
+ break;
275
282
  }
276
- }
283
+ case 'run.discarded': {
284
+ this[kStatus] = 'discarded';
285
+ if (fields.redelivered) break;
277
286
 
278
- function onExecutionMessage(routingKey, message) {
279
- const content = message.content;
280
- const messageType = message.properties.type;
281
- message.ack();
287
+ this[kCounters].discarded++;
282
288
 
283
- switch (messageType) {
284
- case 'stopped': {
285
- return onStop();
286
- }
287
- case 'error': {
288
- broker.publish('run', 'run.error', content);
289
- broker.publish('run', 'run.discarded', content);
290
- break;
291
- }
292
- case 'discard':
293
- broker.publish('run', 'run.discarded', content);
294
- break;
295
- default: {
296
- broker.publish('run', 'run.end', content);
297
- }
298
- }
289
+ this.broker.publish('run', 'run.leave', content);
299
290
 
300
- if (postponedMessage) {
301
- const ackMessage = postponedMessage;
302
- postponedMessage = null;
303
- ackMessage.ack();
291
+ this._publishEvent('discarded', content);
292
+ break;
304
293
  }
305
- }
306
-
307
- function publishEvent(state, content) {
308
- if (!content) content = createMessage();
309
- broker.publish('event', `process.${state}`, {...content, state}, {type: state, mandatory: state === 'error'});
310
- }
311
-
312
- function sendMessage(message) {
313
- const messageContent = message.content;
314
- if (!messageContent) return;
315
-
316
- let targetsFound = false;
317
- if (messageContent.target && messageContent.target.id && getActivityById(messageContent.target.id)) {
318
- targetsFound = true;
319
- } else if (messageContent.message && getStartActivities({referenceId: messageContent.message.id, referenceType: messageContent.message.messageType}).length) {
320
- targetsFound = true;
294
+ case 'run.leave': {
295
+ this[kStatus] = undefined;
296
+ this.broker.cancel('_process-api');
297
+ const {output, ...rest} = content; // eslint-disable-line no-unused-vars
298
+ this._publishEvent('leave', rest);
299
+ break;
321
300
  }
322
- if (!targetsFound) return;
323
-
324
- if (!status) run();
325
- getApi().sendApiMessage(message.properties.type || 'message', cloneContent(messageContent), {delegate: true});
326
301
  }
327
302
 
328
- function getActivityById(childId) {
329
- if (execution) return execution.getActivityById(childId);
330
- return context.getActivityById(childId);
303
+ message.ack();
304
+ };
305
+
306
+ proto._onResumeMessage = function onResumeMessage(message) {
307
+ message.ack();
308
+
309
+ const stateMessage = this[kStateMessage];
310
+ switch (stateMessage.fields.routingKey) {
311
+ case 'run.enter':
312
+ case 'run.start':
313
+ case 'run.discarded':
314
+ case 'run.end':
315
+ case 'run.leave':
316
+ break;
317
+ default:
318
+ return;
331
319
  }
332
320
 
333
- function getActivities() {
334
- if (execution) return execution.getActivities();
335
- return context.getActivities(id);
336
- }
337
-
338
- function getStartActivities(filterOptions) {
339
- return context.getStartActivities(filterOptions, id);
340
- }
321
+ if (!stateMessage.fields.redelivered) return;
341
322
 
342
- function getSequenceFlows() {
343
- if (execution) return execution.getSequenceFlows();
344
- return context.getSequenceFlows();
345
- }
323
+ this._debug(`resume from ${this.status}`);
346
324
 
347
- function getPostponed(...args) {
348
- if (execution) return execution.getPostponed(...args);
349
- return [];
350
- }
325
+ return this.broker.publish('run', stateMessage.fields.routingKey, cloneContent(stateMessage.content), stateMessage.properties);
326
+ };
351
327
 
352
- function onApiMessage(routingKey, message) {
353
- const messageType = message.properties.type;
328
+ proto._onExecutionMessage = function onExecutionMessage(routingKey, message) {
329
+ const content = message.content;
330
+ const messageType = message.properties.type;
331
+ message.ack();
354
332
 
355
- switch (messageType) {
356
- case 'stop': {
357
- if (execution && !execution.completed) return;
358
- onStop();
359
- break;
360
- }
333
+ switch (messageType) {
334
+ case 'stopped': {
335
+ return this._onStop();
336
+ }
337
+ case 'error': {
338
+ this.broker.publish('run', 'run.error', content);
339
+ this.broker.publish('run', 'run.discarded', content);
340
+ break;
341
+ }
342
+ case 'discard':
343
+ this.broker.publish('run', 'run.discarded', content);
344
+ break;
345
+ default: {
346
+ this.broker.publish('run', 'run.end', content);
361
347
  }
362
348
  }
363
349
 
364
- function onStop() {
365
- stopped = true;
366
- deactivateRunConsumers();
367
- return publishEvent('stop');
350
+ const executeMessage = this[kExecuteMessage];
351
+ this[kExecuteMessage] = null;
352
+ executeMessage.ack();
353
+ };
354
+
355
+ proto._publishEvent = function publishEvent(state, content) {
356
+ const eventContent = this._createMessage({...content, state});
357
+ this.broker.publish('event', `process.${state}`, eventContent, {type: state, mandatory: state === 'error'});
358
+ };
359
+
360
+ proto.sendMessage = function sendMessage(message) {
361
+ const messageContent = message && message.content;
362
+ if (!messageContent) return;
363
+
364
+ let targetsFound = false;
365
+ if (messageContent.target && messageContent.target.id && this.getActivityById(messageContent.target.id)) {
366
+ targetsFound = true;
367
+ } else if (messageContent.message && this.getStartActivities({referenceId: messageContent.message.id, referenceType: messageContent.message.messageType}).length) {
368
+ targetsFound = true;
368
369
  }
369
-
370
- function createMessage(override = {}) {
371
- return {
372
- id,
373
- type,
374
- name,
375
- parent: {...parent},
376
- ...override,
377
- };
370
+ if (!targetsFound) return;
371
+
372
+ if (!this.status) this.run();
373
+ this.getApi().sendApiMessage(message.properties.type || 'message', cloneContent(messageContent), {delegate: true});
374
+ };
375
+
376
+ proto.getActivityById = function getActivityById(childId) {
377
+ const execution = this.execution;
378
+ if (execution) return execution.getActivityById(childId);
379
+ return this.context.getActivityById(childId);
380
+ };
381
+
382
+ proto.getActivities = function getActivities() {
383
+ const execution = this.execution;
384
+ if (execution) return execution.getActivities();
385
+ return this.context.getActivities(this.id);
386
+ };
387
+
388
+ proto.getStartActivities = function getStartActivities(filterOptions) {
389
+ return this.context.getStartActivities(filterOptions, this.id);
390
+ };
391
+
392
+ proto.getSequenceFlows = function getSequenceFlows() {
393
+ const execution = this.execution;
394
+ if (execution) return execution.getSequenceFlows();
395
+ return this.context.getSequenceFlows();
396
+ };
397
+
398
+ proto.getPostponed = function getPostponed(...args) {
399
+ const execution = this.execution;
400
+ if (!execution) return [];
401
+ return execution.getPostponed(...args);
402
+ };
403
+
404
+ proto._onApiMessage = function onApiMessage(routingKey, message) {
405
+ const messageType = message.properties.type;
406
+
407
+ switch (messageType) {
408
+ case 'stop': {
409
+ if (this.execution && !this.execution.completed) return;
410
+ this._onStop();
411
+ break;
412
+ }
378
413
  }
414
+ };
415
+
416
+ proto._onStop = function onStop() {
417
+ this[kStopped] = true;
418
+ this._deactivateRunConsumers();
419
+ return this._publishEvent('stop');
420
+ };
421
+
422
+ proto._createMessage = function createMessage(override) {
423
+ return {
424
+ id: this.id,
425
+ type: this.type,
426
+ name: this.name,
427
+ executionId: this.executionId,
428
+ parent: {...this.parent},
429
+ ...override,
430
+ };
431
+ };
379
432
 
380
- function getState() {
381
- return createMessage({
382
- executionId,
383
- status,
384
- stopped,
385
- counters: {...counters},
386
- broker: broker.getState(true),
387
- execution: execution && execution.getState(),
388
- });
389
- }
390
- }
433
+ proto._debug = function debug(msg) {
434
+ this.logger.debug(`<${this.id}> ${msg}`);
435
+ };