bpmn-elements 6.0.1 → 7.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 +322 -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 +169 -175
  6. package/dist/src/Environment.js +90 -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 +722 -409
  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 +433 -359
  49. package/dist/src/process/ProcessExecution.js +744 -645
  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 +195 -175
  60. package/dist/src/tasks/Task.js +17 -19
  61. package/index.js +2 -0
  62. package/package.json +13 -13
  63. package/src/Api.js +65 -59
  64. package/src/Context.js +138 -141
  65. package/src/Environment.js +88 -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 +436 -401
  73. package/src/definition/DefinitionExecution.js +603 -343
  74. package/src/error/Errors.js +11 -6
  75. package/src/eventDefinitions/CancelEventDefinition.js +164 -121
  76. package/src/eventDefinitions/CompensateEventDefinition.js +158 -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 +16 -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 +374 -333
  106. package/src/process/ProcessExecution.js +606 -554
  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
@@ -20,6 +20,15 @@ var _Errors = require("../error/Errors");
20
20
 
21
21
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
22
22
 
23
+ const consumingSymbol = Symbol.for('consuming');
24
+ const countersSymbol = Symbol.for('counters');
25
+ const execSymbol = Symbol.for('exec');
26
+ const executeMessageSymbol = Symbol.for('executeMessage');
27
+ const extensionsSymbol = Symbol.for('extensions');
28
+ const messageHandlersSymbol = Symbol.for('messageHandlers');
29
+ const stateMessageSymbol = Symbol.for('stateMessage');
30
+ const statusSymbol = Symbol.for('status');
31
+ const stoppedSymbol = Symbol.for('stopped');
23
32
  var _default = Process;
24
33
  exports.default = _default;
25
34
 
@@ -31,433 +40,498 @@ function Process(processDef, context) {
31
40
  parent,
32
41
  behaviour = {}
33
42
  } = processDef;
34
- const environment = context.environment;
43
+ this.id = id;
44
+ this.type = type;
45
+ this.name = name;
46
+ this.parent = parent ? (0, _messageHelper.cloneParent)(parent) : {};
47
+ this.behaviour = behaviour;
35
48
  const {
36
49
  isExecutable
37
50
  } = behaviour;
38
- const logger = environment.Logger(type.toLowerCase());
39
- let execution, initExecutionId, executionId, status, stopped, postponedMessage, stateMessage, consumingRunQ;
40
- let counters = {
51
+ this.isExecutable = isExecutable;
52
+ const environment = this.environment = context.environment;
53
+ this.context = context;
54
+ this[countersSymbol] = {
41
55
  completed: 0,
42
- discarded: 0,
43
- terminated: 0
44
- };
45
- const processApi = {
46
- id,
47
- type,
48
- name,
49
- isExecutable,
50
- behaviour,
51
-
52
- get counters() {
53
- return { ...counters
54
- };
55
- },
56
-
57
- get executionId() {
58
- return executionId;
59
- },
60
-
61
- get status() {
62
- return status;
63
- },
64
-
65
- get stopped() {
66
- return stopped;
67
- },
68
-
69
- get execution() {
70
- return execution;
71
- },
72
-
73
- get isRunning() {
74
- if (!consumingRunQ) return false;
75
- return !!status;
76
- },
77
-
78
- context,
79
- environment,
80
- parent: { ...parent
81
- },
82
- logger,
83
- getApi,
84
- getActivities,
85
- getActivityById,
86
- getSequenceFlows,
87
- getPostponed,
88
- getStartActivities,
89
- getState,
90
- init,
91
- recover,
92
- resume,
93
- run,
94
- sendMessage,
95
- shake,
96
- signal,
97
- cancelActivity,
98
- stop
56
+ discarded: 0
99
57
  };
58
+ this[stoppedSymbol] = false;
59
+ this[execSymbol] = {};
100
60
  const {
101
61
  broker,
102
62
  on,
103
63
  once,
104
64
  waitFor
105
- } = (0, _EventBroker.ProcessBroker)(processApi);
106
- processApi.on = on;
107
- processApi.once = once;
108
- processApi.waitFor = waitFor;
109
- const runQ = broker.getQueue('run-q');
110
- const executionQ = broker.getQueue('execution-q');
111
- Object.defineProperty(processApi, 'broker', {
112
- enumerable: true,
113
- get: () => broker
114
- });
115
- const extensions = context.loadExtensions(processApi);
116
- Object.defineProperty(processApi, 'extensions', {
117
- enumerable: true,
118
- get: () => extensions
119
- });
120
- return processApi;
121
-
122
- function init() {
123
- initExecutionId = (0, _shared.getUniqueId)(id);
124
- logger.debug(`<${id}> initialized with executionId <${initExecutionId}>`);
125
- publishEvent('init', createMessage({
126
- executionId: initExecutionId
127
- }));
128
- }
65
+ } = (0, _EventBroker.ProcessBroker)(this);
66
+ this.broker = broker;
67
+ this.on = on;
68
+ this.once = once;
69
+ this.waitFor = waitFor;
70
+ this[messageHandlersSymbol] = {
71
+ onApiMessage: this._onApiMessage.bind(this),
72
+ onRunMessage: this._onRunMessage.bind(this),
73
+ onExecutionMessage: this._onExecutionMessage.bind(this)
74
+ };
75
+ this.logger = environment.Logger(type.toLowerCase());
76
+ this[extensionsSymbol] = context.loadExtensions(this);
77
+ }
129
78
 
130
- function run(runContent) {
131
- if (processApi.isRunning) throw new Error(`process <${id}> is already running`);
132
- executionId = initExecutionId || (0, _shared.getUniqueId)(id);
133
- initExecutionId = undefined;
134
- const content = createMessage({ ...runContent,
135
- executionId
136
- });
137
- broker.publish('run', 'run.enter', content);
138
- broker.publish('run', 'run.start', (0, _messageHelper.cloneContent)(content));
139
- broker.publish('run', 'run.execute', (0, _messageHelper.cloneContent)(content));
140
- activateRunConsumers();
141
- }
79
+ const proto = Process.prototype;
80
+ Object.defineProperty(proto, 'counters', {
81
+ enumerable: true,
142
82
 
143
- function resume() {
144
- if (processApi.isRunning) throw new Error(`cannot resume running process <${id}>`);
145
- if (!status) return processApi;
146
- stopped = false;
147
- const content = createMessage({
148
- executionId
149
- });
150
- broker.publish('run', 'run.resume', content, {
151
- persistent: false
152
- });
153
- activateRunConsumers();
154
- return processApi;
155
- }
156
-
157
- function recover(state) {
158
- if (processApi.isRunning) throw new Error(`cannot recover running process <${id}>`);
159
- if (!state) return processApi;
160
- stopped = state.stopped;
161
- status = state.status;
162
- executionId = state.executionId;
163
- counters = { ...counters,
164
- ...state.counters
83
+ get() {
84
+ return { ...this[countersSymbol]
165
85
  };
86
+ }
166
87
 
167
- if (state.execution) {
168
- execution = (0, _ProcessExecution.default)(processApi, context).recover(state.execution);
169
- }
88
+ });
89
+ Object.defineProperty(proto, 'extensions', {
90
+ enumerable: true,
170
91
 
171
- broker.recover(state.broker);
172
- return processApi;
92
+ get() {
93
+ return this[extensionsSymbol];
173
94
  }
174
95
 
175
- function shake(startId) {
176
- if (processApi.isRunning) return execution.shake(startId);
177
- return (0, _ProcessExecution.default)(processApi, context).shake(startId);
178
- }
96
+ });
97
+ Object.defineProperty(proto, 'stopped', {
98
+ enumerable: true,
179
99
 
180
- function activateRunConsumers() {
181
- consumingRunQ = true;
182
- broker.subscribeTmp('api', `process.*.${executionId}`, onApiMessage, {
183
- noAck: true,
184
- consumerTag: '_process-api',
185
- priority: 100
186
- });
187
- runQ.assertConsumer(onRunMessage, {
188
- exclusive: true,
189
- consumerTag: '_process-run'
190
- });
100
+ get() {
101
+ return this[stoppedSymbol];
191
102
  }
192
103
 
193
- function deactivateRunConsumers() {
194
- broker.cancel('_process-api');
195
- broker.cancel('_process-run');
196
- broker.cancel('_process-execution');
197
- consumingRunQ = false;
198
- }
104
+ });
105
+ Object.defineProperty(proto, 'isRunning', {
106
+ enumerable: true,
199
107
 
200
- function stop() {
201
- if (!processApi.isRunning) return;
202
- getApi().stop();
108
+ get() {
109
+ if (!this[consumingSymbol]) return false;
110
+ return !!this.status;
203
111
  }
204
112
 
205
- function getApi(message) {
206
- if (execution) return execution.getApi(message);
207
- return (0, _Api.ProcessApi)(broker, message || stateMessage);
113
+ });
114
+ Object.defineProperty(proto, 'executionId', {
115
+ enumerable: true,
116
+
117
+ get() {
118
+ const {
119
+ executionId,
120
+ initExecutionId
121
+ } = this[execSymbol];
122
+ return executionId || initExecutionId;
208
123
  }
209
124
 
210
- function signal(message) {
211
- return getApi().signal(message, {
212
- delegate: true
213
- });
125
+ });
126
+ Object.defineProperty(proto, 'execution', {
127
+ enumerable: true,
128
+
129
+ get() {
130
+ return this[execSymbol].execution;
214
131
  }
215
132
 
216
- function cancelActivity(message) {
217
- return getApi().cancel(message, {
218
- delegate: true
219
- });
133
+ });
134
+ Object.defineProperty(proto, 'status', {
135
+ enumerable: true,
136
+
137
+ get() {
138
+ return this[statusSymbol];
220
139
  }
221
140
 
222
- function onRunMessage(routingKey, message) {
223
- const {
224
- content,
225
- ack,
226
- fields
227
- } = message;
141
+ });
228
142
 
229
- if (routingKey === 'run.resume') {
230
- return onResumeMessage();
231
- }
143
+ proto.init = function init(useAsExecutionId) {
144
+ const exec = this[execSymbol];
145
+ const initExecutionId = exec.initExecutionId = useAsExecutionId || (0, _shared.getUniqueId)(this.id);
232
146
 
233
- stateMessage = message;
234
-
235
- switch (routingKey) {
236
- case 'run.enter':
237
- {
238
- logger.debug(`<${id}> enter`);
239
- status = 'entered';
240
- if (fields.redelivered) break;
241
- execution = undefined;
242
- publishEvent('enter', content);
243
- break;
244
- }
147
+ this._debug(`initialized with executionId <${initExecutionId}>`);
245
148
 
246
- case 'run.start':
247
- {
248
- logger.debug(`<${id}> start`);
249
- status = 'start';
250
- publishEvent('start', content);
251
- break;
252
- }
149
+ this._publishEvent('init', this._createMessage({
150
+ executionId: initExecutionId
151
+ }));
152
+ };
253
153
 
254
- case 'run.execute':
255
- {
256
- status = 'executing';
257
- const executeMessage = (0, _messageHelper.cloneMessage)(message);
258
-
259
- if (fields.redelivered && !execution) {
260
- executeMessage.fields.redelivered = undefined;
261
- }
262
-
263
- postponedMessage = message;
264
- executionQ.assertConsumer(onExecutionMessage, {
265
- exclusive: true,
266
- consumerTag: '_process-execution'
267
- });
268
- execution = execution || (0, _ProcessExecution.default)(processApi, context);
269
- return execution.execute(executeMessage);
270
- }
154
+ proto.run = function run(runContent) {
155
+ if (this.isRunning) throw new Error(`process <${this.id}> is already running`);
156
+ const exec = this[execSymbol];
157
+ const executionId = exec.executionId = exec.initExecutionId || (0, _shared.getUniqueId)(this.id);
158
+ exec.initExecutionId = undefined;
271
159
 
272
- case 'run.error':
273
- {
274
- publishEvent('error', (0, _messageHelper.cloneContent)(content, {
275
- error: fields.redelivered ? (0, _Errors.makeErrorFromMessage)(message) : content.error
276
- }));
277
- break;
278
- }
160
+ const content = this._createMessage({ ...runContent,
161
+ executionId
162
+ });
279
163
 
280
- case 'run.end':
281
- {
282
- status = 'end';
283
- if (fields.redelivered) break;
284
- logger.debug(`<${id}> completed`);
285
- counters.completed++;
286
- broker.publish('run', 'run.leave', content);
287
- publishEvent('end', content);
288
- break;
289
- }
164
+ const broker = this.broker;
165
+ broker.publish('run', 'run.enter', content);
166
+ broker.publish('run', 'run.start', (0, _messageHelper.cloneContent)(content));
167
+ broker.publish('run', 'run.execute', (0, _messageHelper.cloneContent)(content));
290
168
 
291
- case 'run.discarded':
292
- {
293
- status = 'discarded';
294
- if (fields.redelivered) break;
295
- counters.discarded++;
296
- broker.publish('run', 'run.leave', content);
297
- break;
298
- }
169
+ this._activateRunConsumers();
170
+ };
299
171
 
300
- case 'run.leave':
301
- {
302
- status = undefined;
303
- broker.cancel('_process-api');
304
- publishEvent('leave');
305
- break;
306
- }
307
- }
172
+ proto.resume = function resume() {
173
+ if (this.isRunning) throw new Error(`cannot resume running process <${this.id}>`);
174
+ if (!this.status) return this;
175
+ this[stoppedSymbol] = false;
176
+
177
+ const content = this._createMessage();
308
178
 
309
- ack();
179
+ this.broker.publish('run', 'run.resume', content, {
180
+ persistent: false
181
+ });
310
182
 
311
- function onResumeMessage() {
312
- message.ack();
183
+ this._activateRunConsumers();
313
184
 
314
- switch (stateMessage.fields.routingKey) {
315
- case 'run.enter':
316
- case 'run.start':
317
- case 'run.discarded':
318
- case 'run.end':
319
- case 'run.leave':
320
- break;
185
+ return this;
186
+ };
321
187
 
322
- default:
323
- return;
324
- }
188
+ proto.recover = function recover(state) {
189
+ if (this.isRunning) throw new Error(`cannot recover running process <${this.id}>`);
190
+ if (!state) return this;
191
+ this[stoppedSymbol] = !!state.stopped;
192
+ this[statusSymbol] = state.status;
193
+ const exec = this[execSymbol];
194
+ exec.executionId = state.executionId;
195
+ this[countersSymbol] = { ...this[countersSymbol],
196
+ ...state.counters
197
+ };
198
+ this.environment.recover(state.environment);
199
+
200
+ if (state.execution) {
201
+ exec.execution = new _ProcessExecution.default(this, this.context).recover(state.execution);
202
+ }
325
203
 
326
- if (!stateMessage.fields.redelivered) return;
327
- logger.debug(`<${id}> resume from ${status}`);
328
- return broker.publish('run', stateMessage.fields.routingKey, (0, _messageHelper.cloneContent)(stateMessage.content), stateMessage.properties);
204
+ this.broker.recover(state.broker);
205
+ return this;
206
+ };
207
+
208
+ proto.shake = function shake(startId) {
209
+ if (this.isRunning) return this.execution.shake(startId);
210
+ return new _ProcessExecution.default(this, this.context).shake(startId);
211
+ };
212
+
213
+ proto.stop = function stop() {
214
+ if (!this.isRunning) return;
215
+ this.getApi().stop();
216
+ };
217
+
218
+ proto.getApi = function getApi(message) {
219
+ const execution = this.execution;
220
+ if (execution) return execution.getApi(message);
221
+ return (0, _Api.ProcessApi)(this.broker, message || this[stateMessageSymbol]);
222
+ };
223
+
224
+ proto.signal = function signal(message) {
225
+ return this.getApi().signal(message, {
226
+ delegate: true
227
+ });
228
+ };
229
+
230
+ proto.getState = function getState() {
231
+ return this._createMessage({
232
+ environment: this.environment.getState(),
233
+ status: this.status,
234
+ stopped: this.stopped,
235
+ counters: this.counters,
236
+ broker: this.broker.getState(true),
237
+ execution: this.execution && this.execution.getState(),
238
+ output: { ...this.environment.output
329
239
  }
240
+ });
241
+ };
242
+
243
+ proto.cancelActivity = function cancelActivity(message) {
244
+ return this.getApi().cancel(message, {
245
+ delegate: true
246
+ });
247
+ };
248
+
249
+ proto._activateRunConsumers = function activateRunConsumers() {
250
+ this[consumingSymbol] = true;
251
+ const broker = this.broker;
252
+ const {
253
+ onApiMessage,
254
+ onRunMessage
255
+ } = this[messageHandlersSymbol];
256
+ broker.subscribeTmp('api', `process.*.${this.executionId}`, onApiMessage, {
257
+ noAck: true,
258
+ consumerTag: '_process-api',
259
+ priority: 100
260
+ });
261
+ broker.getQueue('run-q').assertConsumer(onRunMessage, {
262
+ exclusive: true,
263
+ consumerTag: '_process-run'
264
+ });
265
+ };
266
+
267
+ proto._deactivateRunConsumers = function deactivateRunConsumers() {
268
+ const broker = this.broker;
269
+ broker.cancel('_process-api');
270
+ broker.cancel('_process-run');
271
+ broker.cancel('_process-execution');
272
+ this[consumingSymbol] = false;
273
+ };
274
+
275
+ proto._onRunMessage = function onRunMessage(routingKey, message) {
276
+ const {
277
+ content,
278
+ fields
279
+ } = message;
280
+
281
+ if (routingKey === 'run.resume') {
282
+ return this._onResumeMessage(message);
330
283
  }
331
284
 
332
- function onExecutionMessage(routingKey, message) {
333
- const content = message.content;
334
- const messageType = message.properties.type;
335
- message.ack();
285
+ const exec = this[execSymbol];
286
+ this[stateMessageSymbol] = message;
336
287
 
337
- switch (messageType) {
338
- case 'stopped':
339
- {
340
- return onStop();
341
- }
288
+ switch (routingKey) {
289
+ case 'run.enter':
290
+ {
291
+ this._debug('enter');
342
292
 
343
- case 'error':
344
- {
345
- broker.publish('run', 'run.error', content);
346
- broker.publish('run', 'run.discarded', content);
347
- break;
348
- }
293
+ this[statusSymbol] = 'entered';
294
+ if (fields.redelivered) break;
295
+ exec.execution = undefined;
296
+
297
+ this._publishEvent('enter', content);
349
298
 
350
- case 'discard':
351
- broker.publish('run', 'run.discarded', content);
352
299
  break;
300
+ }
301
+
302
+ case 'run.start':
303
+ {
304
+ this._debug('start');
305
+
306
+ this[statusSymbol] = 'start';
353
307
 
354
- default:
355
- {
356
- broker.publish('run', 'run.end', content);
308
+ this._publishEvent('start', content);
309
+
310
+ break;
311
+ }
312
+
313
+ case 'run.execute':
314
+ {
315
+ this[statusSymbol] = 'executing';
316
+ const executeMessage = (0, _messageHelper.cloneMessage)(message);
317
+
318
+ if (fields.redelivered && !exec.execution) {
319
+ executeMessage.fields.redelivered = undefined;
357
320
  }
358
- }
359
321
 
360
- if (postponedMessage) {
361
- const ackMessage = postponedMessage;
362
- postponedMessage = null;
363
- ackMessage.ack();
364
- }
365
- }
322
+ this[executeMessageSymbol] = message;
323
+ this.broker.getQueue('execution-q').assertConsumer(this[messageHandlersSymbol].onExecutionMessage, {
324
+ exclusive: true,
325
+ consumerTag: '_process-execution'
326
+ });
327
+ const execution = exec.execution = exec.execution || new _ProcessExecution.default(this, this.context);
328
+ return execution.execute(executeMessage);
329
+ }
366
330
 
367
- function publishEvent(state, content) {
368
- if (!content) content = createMessage();
369
- broker.publish('event', `process.${state}`, { ...content,
370
- state
371
- }, {
372
- type: state,
373
- mandatory: state === 'error'
374
- });
375
- }
331
+ case 'run.error':
332
+ {
333
+ this._publishEvent('error', (0, _messageHelper.cloneContent)(content, {
334
+ error: fields.redelivered ? (0, _Errors.makeErrorFromMessage)(message) : content.error
335
+ }));
376
336
 
377
- function sendMessage(message) {
378
- const messageContent = message.content;
379
- if (!messageContent) return;
380
- let targetsFound = false;
381
-
382
- if (messageContent.target && messageContent.target.id && getActivityById(messageContent.target.id)) {
383
- targetsFound = true;
384
- } else if (messageContent.message && getStartActivities({
385
- referenceId: messageContent.message.id,
386
- referenceType: messageContent.message.messageType
387
- }).length) {
388
- targetsFound = true;
389
- }
337
+ break;
338
+ }
390
339
 
391
- if (!targetsFound) return;
392
- if (!status) run();
393
- getApi().sendApiMessage(message.properties.type || 'message', (0, _messageHelper.cloneContent)(messageContent), {
394
- delegate: true
395
- });
396
- }
340
+ case 'run.end':
341
+ {
342
+ this[statusSymbol] = 'end';
343
+ if (fields.redelivered) break;
397
344
 
398
- function getActivityById(childId) {
399
- if (execution) return execution.getActivityById(childId);
400
- return context.getActivityById(childId);
401
- }
345
+ this._debug('completed');
402
346
 
403
- function getActivities() {
404
- if (execution) return execution.getActivities();
405
- return context.getActivities(id);
406
- }
347
+ this[countersSymbol].completed++;
348
+ this.broker.publish('run', 'run.leave', content);
407
349
 
408
- function getStartActivities(filterOptions) {
409
- return context.getStartActivities(filterOptions, id);
410
- }
350
+ this._publishEvent('end', content);
351
+
352
+ break;
353
+ }
354
+
355
+ case 'run.discarded':
356
+ {
357
+ this[statusSymbol] = 'discarded';
358
+ if (fields.redelivered) break;
359
+ this[countersSymbol].discarded++;
360
+ this.broker.publish('run', 'run.leave', content);
361
+
362
+ this._publishEvent('discarded', content);
363
+
364
+ break;
365
+ }
366
+
367
+ case 'run.leave':
368
+ {
369
+ this[statusSymbol] = undefined;
370
+ this.broker.cancel('_process-api');
371
+ const {
372
+ output,
373
+ ...rest
374
+ } = content; // eslint-disable-line no-unused-vars
375
+
376
+ this._publishEvent('leave', rest);
411
377
 
412
- function getSequenceFlows() {
413
- if (execution) return execution.getSequenceFlows();
414
- return context.getSequenceFlows();
378
+ break;
379
+ }
415
380
  }
416
381
 
417
- function getPostponed(...args) {
418
- if (execution) return execution.getPostponed(...args);
419
- return [];
382
+ message.ack();
383
+ };
384
+
385
+ proto._onResumeMessage = function onResumeMessage(message) {
386
+ message.ack();
387
+ const stateMessage = this[stateMessageSymbol];
388
+
389
+ switch (stateMessage.fields.routingKey) {
390
+ case 'run.enter':
391
+ case 'run.start':
392
+ case 'run.discarded':
393
+ case 'run.end':
394
+ case 'run.leave':
395
+ break;
396
+
397
+ default:
398
+ return;
420
399
  }
421
400
 
422
- function onApiMessage(routingKey, message) {
423
- const messageType = message.properties.type;
401
+ if (!stateMessage.fields.redelivered) return;
424
402
 
425
- switch (messageType) {
426
- case 'stop':
427
- {
428
- if (execution && !execution.completed) return;
429
- onStop();
430
- break;
431
- }
432
- }
403
+ this._debug(`resume from ${this.status}`);
404
+
405
+ return this.broker.publish('run', stateMessage.fields.routingKey, (0, _messageHelper.cloneContent)(stateMessage.content), stateMessage.properties);
406
+ };
407
+
408
+ proto._onExecutionMessage = function onExecutionMessage(routingKey, message) {
409
+ const content = message.content;
410
+ const messageType = message.properties.type;
411
+ message.ack();
412
+
413
+ switch (messageType) {
414
+ case 'stopped':
415
+ {
416
+ return this._onStop();
417
+ }
418
+
419
+ case 'error':
420
+ {
421
+ this.broker.publish('run', 'run.error', content);
422
+ this.broker.publish('run', 'run.discarded', content);
423
+ break;
424
+ }
425
+
426
+ case 'discard':
427
+ this.broker.publish('run', 'run.discarded', content);
428
+ break;
429
+
430
+ default:
431
+ {
432
+ this.broker.publish('run', 'run.end', content);
433
+ }
433
434
  }
434
435
 
435
- function onStop() {
436
- stopped = true;
437
- deactivateRunConsumers();
438
- return publishEvent('stop');
436
+ const executeMessage = this[executeMessageSymbol];
437
+
438
+ if (executeMessage) {
439
+ this[executeMessageSymbol] = null;
440
+ executeMessage.ack();
439
441
  }
442
+ };
440
443
 
441
- function createMessage(override = {}) {
442
- return {
443
- id,
444
- type,
445
- name,
446
- parent: { ...parent
447
- },
448
- ...override
449
- };
444
+ proto._publishEvent = function publishEvent(state, content) {
445
+ const eventContent = this._createMessage({ ...content,
446
+ state
447
+ });
448
+
449
+ this.broker.publish('event', `process.${state}`, eventContent, {
450
+ type: state,
451
+ mandatory: state === 'error'
452
+ });
453
+ };
454
+
455
+ proto.sendMessage = function sendMessage(message) {
456
+ const messageContent = message.content;
457
+ if (!messageContent) return;
458
+ let targetsFound = false;
459
+
460
+ if (messageContent.target && messageContent.target.id && this.getActivityById(messageContent.target.id)) {
461
+ targetsFound = true;
462
+ } else if (messageContent.message && this.getStartActivities({
463
+ referenceId: messageContent.message.id,
464
+ referenceType: messageContent.message.messageType
465
+ }).length) {
466
+ targetsFound = true;
450
467
  }
451
468
 
452
- function getState() {
453
- return createMessage({
454
- executionId,
455
- status,
456
- stopped,
457
- counters: { ...counters
458
- },
459
- broker: broker.getState(true),
460
- execution: execution && execution.getState()
461
- });
469
+ if (!targetsFound) return;
470
+ if (!this.status) this.run();
471
+ this.getApi().sendApiMessage(message.properties.type || 'message', (0, _messageHelper.cloneContent)(messageContent), {
472
+ delegate: true
473
+ });
474
+ };
475
+
476
+ proto.getActivityById = function getActivityById(childId) {
477
+ if (this.execution) return this.execution.getActivityById(childId);
478
+ return this.context.getActivityById(childId);
479
+ };
480
+
481
+ proto.getActivities = function getActivities() {
482
+ if (this.execution) return this.execution.getActivities();
483
+ return this.context.getActivities(this.id);
484
+ };
485
+
486
+ proto.getStartActivities = function getStartActivities(filterOptions) {
487
+ return this.context.getStartActivities(filterOptions, this.id);
488
+ };
489
+
490
+ proto.getSequenceFlows = function getSequenceFlows() {
491
+ if (this.execution) return this.execution.getSequenceFlows();
492
+ return this.context.getSequenceFlows();
493
+ };
494
+
495
+ proto.getPostponed = function getPostponed(...args) {
496
+ if (!this.execution) return [];
497
+ return this.execution.getPostponed(...args);
498
+ };
499
+
500
+ proto._onApiMessage = function onApiMessage(routingKey, message) {
501
+ const messageType = message.properties.type;
502
+
503
+ switch (messageType) {
504
+ case 'stop':
505
+ {
506
+ if (this.execution && !this.execution.completed) return;
507
+
508
+ this._onStop();
509
+
510
+ break;
511
+ }
462
512
  }
463
- }
513
+ };
514
+
515
+ proto._onStop = function onStop() {
516
+ this[stoppedSymbol] = true;
517
+
518
+ this._deactivateRunConsumers();
519
+
520
+ return this._publishEvent('stop');
521
+ };
522
+
523
+ proto._createMessage = function createMessage(override) {
524
+ return {
525
+ id: this.id,
526
+ type: this.type,
527
+ name: this.name,
528
+ executionId: this.executionId,
529
+ parent: { ...this.parent
530
+ },
531
+ ...override
532
+ };
533
+ };
534
+
535
+ proto._debug = function debug(msg) {
536
+ this.logger.debug(`<${this.id}> ${msg}`);
537
+ };