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
@@ -1,127 +1,170 @@
1
1
  import {cloneContent, shiftParent} from '../messageHelper';
2
+ import {ActivityError} from '../error/Errors';
3
+
4
+ const executeMessageSymbol = Symbol.for('executeMessage');
2
5
 
3
6
  export default function ConditionalEventDefinition(activity, eventDefinition) {
4
7
  const {id, broker, environment, attachedTo} = activity;
5
- const {type = 'ConditionalEventDefinition', behaviour = {}} = eventDefinition;
6
- const {debug} = environment.Logger(type.toLowerCase());
7
- const condition = behaviour.expression;
8
- const isWaiting = !attachedTo;
9
8
 
10
- const source = {
11
- type,
12
- condition,
13
- execute,
14
- };
9
+ const {type = 'ConditionalEventDefinition', behaviour = {}} = eventDefinition;
15
10
 
16
- return source;
11
+ this.id = id;
12
+ this.type = type;
13
+ this.isWaiting = !attachedTo;
14
+ this.condition = behaviour.expression;
15
+ this.activity = activity;
16
+ this.environment = environment;
17
+ this.broker = broker;
18
+ this.logger = environment.Logger(type.toLowerCase());
19
+ }
17
20
 
18
- function execute(executeMessage) {
19
- return isWaiting ? executeWait(executeMessage) : executeCatch(executeMessage);
21
+ const proto = ConditionalEventDefinition.prototype;
22
+
23
+ Object.defineProperty(proto, 'executionId', {
24
+ get() {
25
+ const message = this[executeMessageSymbol];
26
+ return message && message.content.executionId;
27
+ },
28
+ });
29
+
30
+ proto.execute = function execute(executeMessage) {
31
+ this[executeMessageSymbol] = executeMessage;
32
+ return this.isWaiting ? this.executeWait(executeMessage) : this.executeCatch(executeMessage);
33
+ };
34
+
35
+ proto.executeWait = function executeWait(executeMessage) {
36
+ const executeContent = executeMessage.content;
37
+ const {executionId, parent} = executeContent;
38
+ const parentExecutionId = parent.executionId;
39
+
40
+ if (this._evaluateWait(executeMessage)) return;
41
+
42
+ const broker = this.broker;
43
+ const onApiMessage = this._onWaitApiMessage.bind(this);
44
+ broker.subscribeTmp('api', `activity.#.${executionId}`, onApiMessage, {
45
+ noAck: true,
46
+ consumerTag: `_api-${executionId}`,
47
+ });
48
+ broker.subscribeTmp('api', `activity.signal.${parentExecutionId}`, onApiMessage, {
49
+ noAck: true,
50
+ consumerTag: `_parent-signal-${executionId}`,
51
+ });
52
+
53
+ const waitContent = cloneContent(executeContent, {
54
+ executionId: parentExecutionId,
55
+ condition: this.condition,
56
+ });
57
+ waitContent.parent = shiftParent(parent);
58
+
59
+ broker.publish('event', 'activity.wait', waitContent);
60
+ };
61
+
62
+ proto.executeCatch = function executeCatch(executeMessage) {
63
+ const executeContent = executeMessage.content;
64
+ const {executionId, index} = executeContent;
65
+
66
+ this.broker.subscribeTmp('api', `activity.#.${executionId}`, this._onCatchApiMessage.bind(this), {
67
+ noAck: true,
68
+ consumerTag: `_api-${executionId}_${index}`,
69
+ });
70
+
71
+ const {id: attachedToId, broker: attachedToBroker} = this.activity.attachedTo;
72
+
73
+ this._debug(`listen for execute completed from <${attachedToId}>`);
74
+
75
+ attachedToBroker.subscribeOnce('execution', 'execute.completed', this._onAttachedCompleted.bind(this), {
76
+ priority: 300,
77
+ consumerTag: `_onend-${executionId}_${index}`,
78
+ });
79
+ };
80
+
81
+ proto._onWaitApiMessage = function onWaitApiMessage(routingKey, message) {
82
+ const messageType = message.properties.type;
83
+
84
+ switch (messageType) {
85
+ case 'signal': {
86
+ return this._evaluateWait(message);
87
+ }
88
+ case 'discard': {
89
+ this._stopWait();
90
+ return this.broker.publish('execution', 'execute.discard', cloneContent(this[executeMessageSymbol].content, {state: 'discard'}));
91
+ }
92
+ case 'stop': {
93
+ return this._stopWait();
94
+ }
20
95
  }
96
+ };
21
97
 
22
- function executeCatch(executeMessage) {
23
- const attachedToBroker = attachedTo.broker;
24
- const messageContent = cloneContent(executeMessage.content);
25
-
26
- const {executionId, index} = messageContent;
27
- messageContent.condition = condition;
28
-
29
- const apiConsumerTag = `_api-${executionId}_${index}`;
30
- const endConsumerTag = `_onend-${executionId}_${index}`;
98
+ proto._evaluateWait = function evaluate(message) {
99
+ const executeMessage = this[executeMessageSymbol];
100
+ const broker = this.broker, executeContent = executeMessage.content;
31
101
 
32
- broker.subscribeTmp('api', `activity.#.${executionId}`, onApiMessage, {noAck: true, consumerTag: apiConsumerTag});
102
+ try {
103
+ var output = this.environment.resolveExpression(this.condition, message); // eslint-disable-line no-var
104
+ } catch (err) {
105
+ return broker.publish('execution', 'execute.error', cloneContent(executeContent, {error: new ActivityError(err.message, executeMessage, err)}, {mandatory: true}));
106
+ }
33
107
 
34
- debug(`<${executionId} (${id})> listen for execute completed from <${attachedTo.id}>`);
35
- attachedToBroker.subscribeOnce('execution', 'execute.completed', onAttachedCompleted, {priority: 300, consumerTag: endConsumerTag});
108
+ this._debug(`condition evaluated to ${!!output}`);
36
109
 
37
- function onAttachedCompleted(routingKey, endMessage) {
38
- stop();
110
+ broker.publish('event', 'activity.condition', cloneContent(executeContent, {
111
+ conditionResult: output,
112
+ }));
39
113
 
40
- const output = environment.resolveExpression(condition, endMessage);
41
- debug(`<${executionId} (${id})> condition from <${endMessage.content.executionId}> evaluated to`, !!output);
114
+ if (!output) return;
115
+ this._stopWait();
116
+ return broker.publish('execution', 'execute.completed', cloneContent(executeContent, {output}));
117
+ };
42
118
 
43
- broker.publish('event', 'activity.condition', {
44
- ...cloneContent(messageContent),
45
- conditionResult: output,
46
- });
119
+ proto._stopWait = function stopWait() {
120
+ const broker = this.broker, executionId = this.executionId;
121
+ broker.cancel(`_api-${executionId}`);
122
+ broker.cancel(`_parent-signal-${executionId}`);
123
+ };
47
124
 
48
- if (output) {
49
- broker.publish('execution', 'execute.completed', {
50
- ...messageContent,
51
- output,
52
- });
53
- }
54
- }
125
+ proto._onAttachedCompleted = function onAttachedCompleted(routingKey, message) {
126
+ this._stopCatch();
55
127
 
56
- function onApiMessage(routingKey, message) {
57
- const messageType = message.properties.type;
58
- switch (messageType) {
59
- case 'discard': {
60
- stop();
61
- debug(`<${executionId} (${id})> discarded`);
62
- return broker.publish('execution', 'execute.discard', {...messageContent, state: 'discard'});
63
- }
64
- case 'stop': {
65
- stop();
66
- return debug(`<${executionId} (${id})> stopped`);
67
- }
68
- }
69
- }
70
-
71
- function stop() {
72
- attachedToBroker.cancel(endConsumerTag);
73
- broker.cancel(apiConsumerTag);
74
- }
128
+ const executeMessage = this[executeMessageSymbol];
129
+ const broker = this.broker, executeContent = executeMessage.content;
130
+ try {
131
+ var output = this.environment.resolveExpression(this.condition, message); // eslint-disable-line no-var
132
+ } catch (err) {
133
+ return broker.publish('execution', 'execute.error', cloneContent(executeContent, {error: new ActivityError(err.message, executeMessage, err)}, {mandatory: true}));
75
134
  }
76
135
 
77
- function executeWait(executeMessage) {
78
- const messageContent = cloneContent(executeMessage.content);
79
- messageContent.condition = condition;
80
- const {executionId, parent} = messageContent;
81
- const parentExecutionId = parent && parent.executionId;
82
-
83
- if (evaluate(executeMessage)) return;
84
-
85
- broker.subscribeTmp('api', `activity.#.${executionId}`, onApiMessage, {noAck: true, consumerTag: `_api-${executionId}`});
86
- broker.subscribeTmp('api', `activity.signal.${parentExecutionId}`, onApiMessage, {noAck: true, consumerTag: `_parent-signal-${executionId}`});
87
-
88
- broker.publish('event', 'activity.wait', {...cloneContent(messageContent), executionId: parentExecutionId, parent: shiftParent(parent)});
89
-
90
- function onApiMessage(routingKey, message) {
91
- const messageType = message.properties.type;
92
-
93
- switch (messageType) {
94
- case 'signal': {
95
- return evaluate(message);
96
- }
97
- case 'discard': {
98
- stop();
99
- return broker.publish('execution', 'execute.discard', {...messageContent, state: 'discard'});
100
- }
101
- case 'stop': {
102
- stop();
103
- break;
104
- }
105
- }
106
- }
107
-
108
- function evaluate(message) {
109
- const output = environment.resolveExpression(condition, message);
110
- debug(`<${executionId} (${id})> condition evaluated to`, !!output);
136
+ this._debug(`condition from <${message.content.executionId}> evaluated to ${!!output}`);
111
137
 
112
- broker.publish('event', 'activity.condition', {
113
- ...cloneContent(messageContent),
114
- conditionResult: output,
115
- });
138
+ broker.publish('event', 'activity.condition', cloneContent(executeContent, {
139
+ conditionResult: output,
140
+ }));
116
141
 
117
- if (!output) return;
118
- stop();
119
- return broker.publish('execution', 'execute.completed', {...messageContent, output});
142
+ if (output) {
143
+ broker.publish('execution', 'execute.completed', cloneContent(executeContent, {output}));
144
+ }
145
+ };
146
+
147
+ proto._onCatchApiMessage = function onCatchApiMessage(routingKey, message) {
148
+ const messageType = message.properties.type;
149
+ switch (messageType) {
150
+ case 'discard': {
151
+ this._stopCatch();
152
+ this._debug('discarded');
153
+ return this.broker.publish('execution', 'execute.discard', cloneContent(this[executeMessageSymbol].content, {state: 'discard'}));
120
154
  }
121
-
122
- function stop() {
123
- broker.cancel(`_api-${executionId}`);
124
- broker.cancel(`_parent-signal-${executionId}`);
155
+ case 'stop': {
156
+ this._stopCatch();
157
+ return this._debug('stopped');
125
158
  }
126
159
  }
127
- }
160
+ };
161
+
162
+ proto._stopCatch = function stopCatch() {
163
+ const {executionId, index} = this[executeMessageSymbol].content;
164
+ this.activity.attachedTo.broker.cancel(`_onend-${executionId}_${index}`);
165
+ this.broker.cancel(`_api-${executionId}_${index}`);
166
+ };
167
+
168
+ proto._debug = function debug(msg) {
169
+ this.logger.debug(`<${this.executionId} (${this.activity.id})> ${msg}`);
170
+ };
@@ -1,160 +1,219 @@
1
1
  import {brokerSafeId} from '../shared';
2
2
  import {cloneContent, shiftParent} from '../messageHelper';
3
3
 
4
+ const completedSymbol = Symbol.for('completed');
5
+ const messageQSymbol = Symbol.for('messageQ');
6
+ const executeMessageSymbol = Symbol.for('executeMessage');
7
+ const referenceElementSymbol = Symbol.for('referenceElement');
8
+ const referenceInfoSymbol = Symbol.for('referenceInfo');
9
+
4
10
  export default function ErrorEventDefinition(activity, eventDefinition) {
5
- const {id, broker, environment, getActivityById, isThrowing} = activity;
11
+ const {id, broker, environment, isThrowing} = activity;
6
12
  const {type = 'ErrorEventDefinition', behaviour = {}} = eventDefinition;
7
- const {debug} = environment.Logger(type.toLowerCase());
8
- const reference = behaviour.errorRef || {name: 'anonymous'};
9
- const referenceElement = reference.id && getActivityById(reference.id);
10
- const errorId = referenceElement ? referenceElement.id : 'anonymous';
11
- const errorQueueName = `error-${brokerSafeId(id)}-${brokerSafeId(errorId)}-q`;
12
-
13
- if (!isThrowing) setupCatch();
14
-
15
- const source = {
16
- type,
17
- reference: {...reference, referenceType: 'throw'},
18
- execute: isThrowing ? executeThrow : executeCatch,
19
- };
20
-
21
- return source;
22
-
23
- function executeCatch(executeMessage) {
24
- let completed;
25
13
 
26
- const messageContent = cloneContent(executeMessage.content);
27
- const {executionId, parent} = messageContent;
28
- const parentExecutionId = parent && parent.executionId;
14
+ this.id = id;
15
+ this.type = type;
29
16
 
30
- const {message: referenceMessage, description} = resolveMessage(executeMessage);
31
- broker.consume(errorQueueName, onThrowApiMessage, {noAck: true, consumerTag: `_onthrow-${executionId}`});
17
+ const reference = this.reference = {
18
+ name: 'anonymous',
19
+ ...behaviour.errorRef,
20
+ referenceType: 'throw',
21
+ };
32
22
 
33
- if (completed) return;
23
+ this.isThrowing = isThrowing;
24
+ this.activity = activity;
25
+ this.environment = environment;
26
+ this.broker = broker;
27
+ this.logger = environment.Logger(type.toLowerCase());
28
+
29
+ const referenceElement = this[referenceElementSymbol] = reference.id && activity.getActivityById(reference.id);
30
+ if (!isThrowing) {
31
+ this[completedSymbol] = false;
32
+ const referenceId = referenceElement ? referenceElement.id : 'anonymous';
33
+ const messageQueueName = `${reference.referenceType}-${brokerSafeId(id)}-${brokerSafeId(referenceId)}-q`;
34
+ this[messageQSymbol] = broker.assertQueue(messageQueueName, {autoDelete: false, durable: true});
35
+ broker.bindQueue(messageQueueName, 'api', `*.${reference.referenceType}.#`, {durable: true, priority: 300});
36
+ }
37
+ }
34
38
 
35
- broker.subscribeTmp('api', `activity.#.${executionId}`, onApiMessage, {noAck: true, consumerTag: `_api-${executionId}`});
39
+ const proto = ErrorEventDefinition.prototype;
36
40
 
37
- if (!environment.settings.strict) {
38
- const expectRoutingKey = `execute.throw.${executionId}`;
39
- broker.publish('execution', 'execute.expect', {...cloneContent(messageContent), expectRoutingKey, expect: {...referenceMessage}});
40
- broker.subscribeOnce('execution', expectRoutingKey, onErrorMessage, {consumerTag: `_onerror-${executionId}`});
41
- }
41
+ Object.defineProperty(proto, 'executionId', {
42
+ get() {
43
+ const message = this[executeMessageSymbol];
44
+ return message && message.content.executionId;
45
+ },
46
+ });
42
47
 
43
- if (completed) return stop();
48
+ proto.execute = function execute(executeMessage) {
49
+ return this.isThrowing ? this.executeThrow(executeMessage) : this.executeCatch(executeMessage);
50
+ };
44
51
 
45
- debug(`<${executionId} (${id})> expect ${description}`);
52
+ proto.executeCatch = function executeCatch(executeMessage) {
53
+ this[executeMessageSymbol] = executeMessage;
54
+ this[completedSymbol] = false;
46
55
 
47
- broker.publish('event', 'activity.wait', {
48
- ...messageContent,
49
- executionId: parentExecutionId,
50
- parent: shiftParent(parent),
51
- expect: {...referenceMessage},
52
- });
56
+ const executeContent = executeMessage.content;
57
+ const {executionId, parent} = executeContent;
58
+ const parentExecutionId = parent && parent.executionId;
53
59
 
54
- function onErrorMessage(routingKey, message) {
55
- const error = message.content.error;
56
- if (!referenceElement) return catchError(routingKey, message, error);
60
+ const info = this[referenceInfoSymbol] = this._getReferenceInfo(executeMessage);
57
61
 
58
- if (!error) return;
62
+ this[messageQSymbol].consume(this._onThrowApiMessage.bind(this), {
63
+ noAck: true,
64
+ consumerTag: `_onthrow-${executionId}`,
65
+ });
59
66
 
60
- if (('' + error.code) !== ('' + referenceMessage.code)) return;
67
+ if (this[completedSymbol]) return;
61
68
 
62
- return catchError(routingKey, message, error);
63
- }
69
+ this._debug(`expect ${info.description}`);
64
70
 
65
- function onThrowApiMessage(routingKey, message) {
66
- const error = message.content.message;
67
- if (!referenceElement) return catchError(routingKey, message, error);
71
+ const broker = this.broker;
72
+ broker.subscribeTmp('api', `activity.#.${executionId}`, this._onApiMessage.bind(this), {
73
+ noAck: true,
74
+ consumerTag: `_api-${executionId}`,
75
+ });
68
76
 
69
- if (referenceMessage.id !== (error && error.id)) return;
70
- return catchError(routingKey, message, error);
71
- }
77
+ if (!this.environment.settings.strict) {
78
+ const expectRoutingKey = `execute.throw.${executionId}`;
79
+ broker.subscribeTmp('execution', expectRoutingKey, this._onErrorMessage.bind(this), {
80
+ noAck: true,
81
+ consumerTag: `_onerror-${executionId}`,
82
+ });
83
+ broker.publish('execution', 'execute.expect', cloneContent(executeContent, {
84
+ expectRoutingKey,
85
+ expect: {...info.message},
86
+ }));
72
87
 
73
- function catchError(routingKey, message, error) {
74
- completed = true;
75
-
76
- stop();
77
-
78
- debug(`<${executionId} (${id})> caught ${description}`);
79
- broker.publish('event', 'activity.catch', {
80
- ...messageContent,
81
- source: {
82
- id: message.content.id,
83
- type: message.content.type,
84
- executionId: message.content.executionId,
85
- },
86
- error,
87
- executionId: parentExecutionId,
88
- parent: shiftParent(executeMessage.content.parent),
89
- }, {type: 'catch'});
90
-
91
- return broker.publish('execution', 'execute.completed', {...messageContent, output: error, cancelActivity: true, state: 'catch'});
92
- }
88
+ if (this[completedSymbol]) return this._stop();
89
+ }
93
90
 
94
- function onApiMessage(routingKey, message) {
95
- const messageType = message.properties.type;
96
-
97
- switch (messageType) {
98
- case 'discard': {
99
- completed = true;
100
- stop();
101
- return broker.publish('execution', 'execute.discard', cloneContent(messageContent));
102
- }
103
- case 'stop': {
104
- stop();
105
- break;
106
- }
107
- }
91
+ const waitContent = cloneContent(executeContent, {
92
+ executionId: parentExecutionId,
93
+ expect: {...info.message},
94
+ });
95
+ waitContent.parent = shiftParent(parent);
96
+
97
+ broker.publish('event', 'activity.wait', waitContent);
98
+ };
99
+
100
+ proto.executeThrow = function executeThrow(executeMessage) {
101
+ const executeContent = executeMessage.content;
102
+ const {executionId, parent} = executeContent;
103
+
104
+ const info = this._getReferenceInfo(executeMessage);
105
+
106
+ this.logger.debug(`<${executionId} (${this.activity.id})> throw ${info.description}`);
107
+
108
+ const broker = this.broker;
109
+ const throwContent = cloneContent(executeContent, {
110
+ executionId: parent.executionId,
111
+ message: {...info.message},
112
+ state: 'throw',
113
+ });
114
+ throwContent.parent = shiftParent(parent);
115
+
116
+ this.broker.publish('event', 'activity.throw', throwContent, {type: 'throw', delegate: true});
117
+
118
+ return broker.publish('execution', 'execute.completed', cloneContent(executeContent, {
119
+ message: {...info.message},
120
+ }));
121
+ };
122
+
123
+ proto._onErrorMessage = function onErrorMessage(routingKey, message) {
124
+ const error = message.content.error;
125
+ if (!this[referenceElementSymbol]) return this._catchError(routingKey, message, error);
126
+
127
+ if (!error) return;
128
+
129
+ const info = this[referenceInfoSymbol];
130
+ if (('' + error.code) !== ('' + info.message.code)) return;
131
+
132
+ return this._catchError(routingKey, message, error);
133
+ };
134
+
135
+ proto._onThrowApiMessage = function onThrowApiMessage(routingKey, message) {
136
+ const error = message.content.message;
137
+ if (!this[referenceElementSymbol]) return this._catchError(routingKey, message, error);
138
+
139
+ const info = this[referenceInfoSymbol];
140
+ if (info.message.id !== (error && error.id)) return;
141
+ return this._catchError(routingKey, message, error);
142
+ };
143
+
144
+ proto._catchError = function catchError(routingKey, message, error) {
145
+ this[completedSymbol] = true;
146
+
147
+ this._stop();
148
+
149
+ this._debug(`caught ${this[referenceInfoSymbol].description}`);
150
+
151
+ const executeContent = this[executeMessageSymbol].content;
152
+ const parent = executeContent.parent;
153
+ const catchContent = cloneContent(executeContent, {
154
+ source: {
155
+ id: message.content.id,
156
+ type: message.content.type,
157
+ executionId: message.content.executionId,
158
+ },
159
+ error,
160
+ executionId: parent.executionId,
161
+ });
162
+ catchContent.parent = shiftParent(parent);
163
+
164
+ const broker = this.broker;
165
+ broker.publish('event', 'activity.catch', catchContent, {type: 'catch'});
166
+
167
+ return broker.publish('execution', 'execute.completed', cloneContent(executeContent, {
168
+ output: error,
169
+ cancelActivity: true,
170
+ state: 'catch',
171
+ }));
172
+ };
173
+
174
+ proto._onApiMessage = function onApiMessage(routingKey, message) {
175
+ const messageType = message.properties.type;
176
+
177
+ switch (messageType) {
178
+ case 'discard': {
179
+ this[completedSymbol] = true;
180
+ this._stop();
181
+ return this.broker.publish('execution', 'execute.discard', cloneContent(this[executeMessageSymbol].content));
108
182
  }
109
-
110
- function stop() {
111
- broker.cancel(`_onthrow-${executionId}`);
112
- broker.cancel(`_onerror-${executionId}`);
113
- broker.cancel(`_api-${executionId}`);
114
- broker.purgeQueue(errorQueueName);
183
+ case 'stop': {
184
+ this._stop();
185
+ break;
115
186
  }
116
187
  }
117
-
118
- function executeThrow(executeMessage) {
119
- const messageContent = cloneContent(executeMessage.content);
120
- const {executionId, parent} = messageContent;
121
- const parentExecutionId = parent && parent.executionId;
122
-
123
- const {message: referenceMessage, description} = resolveMessage(executeMessage);
124
-
125
- debug(`<${executionId} (${id})> throw ${description}`);
126
-
127
- broker.publish('event', 'activity.throw', {
128
- ...cloneContent(messageContent),
129
- executionId: parentExecutionId,
130
- parent: shiftParent(parent),
131
- message: {...referenceMessage},
132
- state: 'throw',
133
- }, {type: 'throw', delegate: true});
134
-
135
- return broker.publish('execution', 'execute.completed', {...messageContent, message: referenceMessage});
188
+ };
189
+
190
+ proto._stop = function stop() {
191
+ const broker = this.broker, executionId = this.executionId;
192
+ broker.cancel(`_onthrow-${executionId}`);
193
+ broker.cancel(`_onerror-${executionId}`);
194
+ broker.cancel(`_api-${executionId}`);
195
+ this[messageQSymbol].purge();
196
+ };
197
+
198
+ proto._getReferenceInfo = function getReferenceInfo(message) {
199
+ const referenceElement = this[referenceElementSymbol];
200
+ if (!referenceElement) {
201
+ return {
202
+ message: {...this.reference},
203
+ description: 'anonymous error',
204
+ };
136
205
  }
137
206
 
138
- function resolveMessage(message) {
139
- if (!referenceElement) {
140
- return {
141
- message: {...reference},
142
- description: 'anonymous error',
143
- };
144
- }
207
+ const result = {
208
+ message: referenceElement.resolve(message),
209
+ };
145
210
 
146
- const result = {
147
- message: referenceElement.resolve(message),
148
- };
211
+ result.description = `${result.message.name} <${result.message.id}>`;
212
+ if (result.message.code) result.description += ` code ${result.message.code}`;
149
213
 
150
- result.description = `${result.message.name} <${result.message.id}>`;
151
- if (result.message.code) result.description += ` code ${result.message.code}`;
214
+ return result;
215
+ };
152
216
 
153
- return result;
154
- }
155
-
156
- function setupCatch() {
157
- broker.assertQueue(errorQueueName, {autoDelete: false, durable: true});
158
- broker.bindQueue(errorQueueName, 'api', '*.throw.#', {durable: true, priority: 300});
159
- }
160
- }
217
+ proto._debug = function debug(msg) {
218
+ this.logger.debug(`<${this.executionId} (${this.activity.id})> ${msg}`);
219
+ };