bpmn-elements 5.1.3 → 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 (119) hide show
  1. package/CHANGELOG.md +322 -0
  2. package/README.md +9 -3
  3. package/dist/index.js +71 -39
  4. package/dist/src/Api.js +77 -76
  5. package/dist/src/Context.js +169 -164
  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/Timers.js +4 -6
  12. package/dist/src/activity/Activity.js +1108 -901
  13. package/dist/src/activity/ActivityExecution.js +342 -297
  14. package/dist/src/activity/Dummy.js +3 -3
  15. package/dist/src/definition/Definition.js +498 -444
  16. package/dist/src/definition/DefinitionExecution.js +722 -409
  17. package/dist/src/error/Errors.js +17 -7
  18. package/dist/src/eventDefinitions/CancelEventDefinition.js +190 -150
  19. package/dist/src/eventDefinitions/CompensateEventDefinition.js +194 -161
  20. package/dist/src/eventDefinitions/ConditionalEventDefinition.js +197 -135
  21. package/dist/src/eventDefinitions/ErrorEventDefinition.js +207 -165
  22. package/dist/src/eventDefinitions/EscalationEventDefinition.js +175 -141
  23. package/dist/src/eventDefinitions/EventDefinitionExecution.js +157 -129
  24. package/dist/src/eventDefinitions/LinkEventDefinition.js +174 -149
  25. package/dist/src/eventDefinitions/MessageEventDefinition.js +213 -176
  26. package/dist/src/eventDefinitions/SignalEventDefinition.js +203 -161
  27. package/dist/src/eventDefinitions/TerminateEventDefinition.js +21 -23
  28. package/dist/src/eventDefinitions/TimerEventDefinition.js +243 -228
  29. package/dist/src/events/BoundaryEvent.js +180 -144
  30. package/dist/src/events/EndEvent.js +18 -23
  31. package/dist/src/events/IntermediateCatchEvent.js +44 -58
  32. package/dist/src/events/IntermediateThrowEvent.js +18 -23
  33. package/dist/src/events/StartEvent.js +109 -94
  34. package/dist/src/flows/Association.js +94 -101
  35. package/dist/src/flows/MessageFlow.js +86 -103
  36. package/dist/src/flows/SequenceFlow.js +172 -184
  37. package/dist/src/gateways/EventBasedGateway.js +88 -84
  38. package/dist/src/gateways/ExclusiveGateway.js +13 -16
  39. package/dist/src/gateways/InclusiveGateway.js +11 -14
  40. package/dist/src/gateways/ParallelGateway.js +11 -14
  41. package/dist/src/getPropertyValue.js +34 -34
  42. package/dist/src/io/BpmnIO.js +31 -0
  43. package/dist/src/io/EnvironmentDataObject.js +33 -29
  44. package/dist/src/io/EnvironmentDataStore.js +52 -0
  45. package/dist/src/io/EnvironmentDataStoreReference.js +52 -0
  46. package/dist/src/io/InputOutputSpecification.js +177 -168
  47. package/dist/src/io/Properties.js +252 -0
  48. package/dist/src/messageHelper.js +1 -1
  49. package/dist/src/process/Process.js +433 -359
  50. package/dist/src/process/ProcessExecution.js +744 -645
  51. package/dist/src/shared.js +3 -6
  52. package/dist/src/tasks/CallActivity.js +160 -0
  53. package/dist/src/tasks/LoopCharacteristics.js +309 -330
  54. package/dist/src/tasks/ReceiveTask.js +233 -182
  55. package/dist/src/tasks/ScriptTask.js +35 -41
  56. package/dist/src/tasks/ServiceImplementation.js +13 -20
  57. package/dist/src/tasks/ServiceTask.js +82 -75
  58. package/dist/src/tasks/SignalTask.js +97 -93
  59. package/dist/src/tasks/StandardLoopCharacteristics.js +1 -1
  60. package/dist/src/tasks/SubProcess.js +195 -175
  61. package/dist/src/tasks/Task.js +17 -19
  62. package/index.js +8 -0
  63. package/package.json +16 -15
  64. package/src/Api.js +65 -59
  65. package/src/Context.js +142 -132
  66. package/src/Environment.js +88 -100
  67. package/src/EventBroker.js +67 -68
  68. package/src/ExtensionsMapper.js +2 -2
  69. package/src/MessageFormatter.js +132 -74
  70. package/src/Timers.js +4 -4
  71. package/src/activity/Activity.js +916 -757
  72. package/src/activity/ActivityExecution.js +293 -247
  73. package/src/activity/Dummy.js +2 -2
  74. package/src/definition/Definition.js +436 -401
  75. package/src/definition/DefinitionExecution.js +603 -343
  76. package/src/error/Errors.js +11 -6
  77. package/src/eventDefinitions/CancelEventDefinition.js +164 -121
  78. package/src/eventDefinitions/CompensateEventDefinition.js +158 -124
  79. package/src/eventDefinitions/ConditionalEventDefinition.js +147 -104
  80. package/src/eventDefinitions/ErrorEventDefinition.js +190 -131
  81. package/src/eventDefinitions/EscalationEventDefinition.js +139 -101
  82. package/src/eventDefinitions/EventDefinitionExecution.js +127 -95
  83. package/src/eventDefinitions/LinkEventDefinition.js +160 -129
  84. package/src/eventDefinitions/MessageEventDefinition.js +178 -121
  85. package/src/eventDefinitions/SignalEventDefinition.js +162 -106
  86. package/src/eventDefinitions/TerminateEventDefinition.js +19 -19
  87. package/src/eventDefinitions/TimerEventDefinition.js +202 -167
  88. package/src/events/BoundaryEvent.js +156 -115
  89. package/src/events/EndEvent.js +15 -18
  90. package/src/events/IntermediateCatchEvent.js +40 -44
  91. package/src/events/IntermediateThrowEvent.js +15 -18
  92. package/src/events/StartEvent.js +84 -50
  93. package/src/flows/Association.js +98 -113
  94. package/src/flows/MessageFlow.js +81 -97
  95. package/src/flows/SequenceFlow.js +145 -163
  96. package/src/gateways/EventBasedGateway.js +75 -68
  97. package/src/gateways/ExclusiveGateway.js +8 -13
  98. package/src/gateways/InclusiveGateway.js +8 -13
  99. package/src/gateways/ParallelGateway.js +8 -13
  100. package/src/getPropertyValue.js +34 -33
  101. package/src/io/BpmnIO.js +20 -0
  102. package/src/io/EnvironmentDataObject.js +29 -18
  103. package/src/io/EnvironmentDataStore.js +33 -0
  104. package/src/io/EnvironmentDataStoreReference.js +33 -0
  105. package/src/io/InputOutputSpecification.js +154 -157
  106. package/src/io/Properties.js +199 -0
  107. package/src/process/Process.js +374 -333
  108. package/src/process/ProcessExecution.js +606 -554
  109. package/src/shared.js +1 -5
  110. package/src/tasks/CallActivity.js +130 -0
  111. package/src/tasks/LoopCharacteristics.js +290 -289
  112. package/src/tasks/ReceiveTask.js +174 -107
  113. package/src/tasks/ScriptTask.js +27 -30
  114. package/src/tasks/ServiceImplementation.js +13 -18
  115. package/src/tasks/ServiceTask.js +67 -60
  116. package/src/tasks/SignalTask.js +77 -52
  117. package/src/tasks/StandardLoopCharacteristics.js +1 -1
  118. package/src/tasks/SubProcess.js +184 -157
  119. package/src/tasks/Task.js +15 -19
@@ -1,8 +1,13 @@
1
1
  import Activity from '../activity/Activity';
2
2
  import {cloneContent} from '../messageHelper';
3
3
 
4
+ const completedSymbol = Symbol.for('completed');
5
+ const executeMessageSymbol = Symbol.for('executeMessage');
6
+ const referenceElementSymbol = Symbol.for('referenceElement');
7
+ const referenceInfoSymbol = Symbol.for('referenceInfo');
8
+
4
9
  export default function ReceiveTask(activityDef, context) {
5
- const task = Activity(ReceiveTaskBehaviour, activityDef, context);
10
+ const task = new Activity(ReceiveTaskBehaviour, activityDef, context);
6
11
 
7
12
  task.broker.assertQueue('message', {autoDelete: false, durable: true});
8
13
  task.broker.bindQueue('message', 'api', '*.message.#', {durable: true});
@@ -11,140 +16,202 @@ export default function ReceiveTask(activityDef, context) {
11
16
  }
12
17
 
13
18
  export function ReceiveTaskBehaviour(activity) {
14
- const {id, type, broker, logger, behaviour = {}, getActivityById} = activity;
15
- const reference = behaviour.messageRef || {name: 'anonymous'};
19
+ const {id, type, behaviour} = activity;
16
20
 
17
- const referenceElement = reference.id && getActivityById(reference.id);
18
- const loopCharacteristics = behaviour.loopCharacteristics && behaviour.loopCharacteristics.Behaviour(activity, behaviour.loopCharacteristics);
21
+ this.id = id;
22
+ this.type = type;
19
23
 
20
- const source = {
21
- id,
22
- type,
23
- reference: {...reference, referenceType: 'message'},
24
- execute,
24
+ const reference = this.reference = {
25
+ name: 'anonymous',
26
+ ...behaviour.messageRef,
27
+ referenceType: 'message',
25
28
  };
26
29
 
27
- return source;
30
+ this.loopCharacteristics = behaviour.loopCharacteristics && new behaviour.loopCharacteristics.Behaviour(activity, behaviour.loopCharacteristics);
31
+ this.activity = activity;
32
+ this.broker = activity.broker;
28
33
 
29
- function execute(executeMessage) {
30
- const content = executeMessage.content;
31
- const {executionId} = content;
34
+ this[referenceElementSymbol] = reference.id && activity.getActivityById(reference.id);
35
+ }
32
36
 
33
- if (content.isRootScope) setupMessageHandling(executionId);
34
- if (loopCharacteristics && content.isRootScope) {
35
- return loopCharacteristics.execute(executeMessage);
36
- }
37
+ ReceiveTaskBehaviour.prototype.execute = function execute(executeMessage) {
38
+ return new ReceiveTaskExecution(this).execute(executeMessage);
39
+ };
37
40
 
38
- let completed;
41
+ function ReceiveTaskExecution(parent) {
42
+ const {activity, broker, loopCharacteristics, reference} = parent;
39
43
 
40
- const {message: referenceMessage, description} = resolveReference(executeMessage);
41
- broker.consume('message', onCatchMessage, {noAck: true, consumerTag: `_onmessage-${executionId}`});
44
+ this.id = activity.id;
45
+ this.logger = activity.logger;
46
+ this.reference = reference;
47
+ this.broker = broker;
48
+ this.loopCharacteristics = loopCharacteristics;
49
+ this.referenceElement = parent[referenceElementSymbol];
42
50
 
43
- if (completed) return;
51
+ this[completedSymbol] = false;
52
+ }
44
53
 
45
- broker.subscribeTmp('api', `activity.#.${executionId}`, onApiMessage, {noAck: true, consumerTag: `_api-${executionId}`, priority: 400});
54
+ const proto = ReceiveTaskExecution.prototype;
46
55
 
47
- logger.debug(`<${executionId} (${id})> expect ${description}`);
56
+ proto.execute = function execute(executeMessage) {
57
+ this[executeMessageSymbol] = executeMessage;
48
58
 
49
- broker.publish('event', 'activity.wait', cloneContent(content, {message: {...referenceMessage}}));
59
+ const executeContent = executeMessage.content;
60
+ const {executionId, isRootScope} = executeContent;
61
+ this.executionId = executionId;
50
62
 
51
- function onCatchMessage(routingKey, message) {
52
- const {content: delegateContent} = message;
63
+ const info = this[referenceInfoSymbol] = this._getReferenceInfo(executeMessage);
53
64
 
54
- const {id: signalId, executionId: signalExecutionId} = delegateContent.message || {};
55
- if (!referenceMessage.id && signalId || signalExecutionId) {
56
- if (loopCharacteristics && signalExecutionId !== executionId) return;
57
- if (signalId !== id && signalExecutionId !== executionId) return;
58
- logger.debug(`<${executionId} (${id})> caught direct message`);
59
- } else if (referenceMessage.id !== signalId) return;
60
- else {
61
- logger.debug(`<${executionId} (${id})> caught ${description}`);
62
- }
65
+ if (isRootScope) {
66
+ this._setupMessageHandling(executionId);
67
+ }
63
68
 
64
- const {type: messageType, correlationId} = message.properties;
65
- broker.publish('event', 'activity.consumed', cloneContent(content, {message: {...message.content.message}}), {correlationId, type: messageType});
66
- broker.publish('event', 'activity.catch', cloneContent(content, {message: message.content.message}), {type: 'catch', correlationId});
69
+ const loopCharacteristics = this.loopCharacteristics;
70
+ if (loopCharacteristics && executeMessage.content.isRootScope) {
71
+ return loopCharacteristics.execute(executeMessage);
72
+ }
67
73
 
68
- complete(message.content.message, {correlationId});
69
- }
74
+ const broker = this.broker;
75
+ broker.consume('message', this._onCatchMessage.bind(this), {
76
+ noAck: true,
77
+ consumerTag: `_onmessage-${executionId}`,
78
+ });
70
79
 
71
- function onApiMessage(routingKey, message) {
72
- const {type: messageType, correlationId} = message.properties;
73
- switch (messageType) {
74
- case 'message':
75
- case 'signal': {
76
- return complete(message.content.message, {correlationId});
77
- }
78
- case 'discard': {
79
- completed = true;
80
- stop();
81
- return broker.publish('execution', 'execute.discard', cloneContent(content), {correlationId});
82
- }
83
- case 'stop': {
84
- return stop();
85
- }
86
- }
87
- }
80
+ if (this[completedSymbol]) return;
88
81
 
89
- function complete(output, options) {
90
- completed = true;
91
- stop();
92
- return broker.publish('execution', 'execute.completed', cloneContent(content, {output}), options);
93
- }
82
+ broker.subscribeTmp('api', `activity.#.${executionId}`, this._onApiMessage.bind(this), {
83
+ noAck: true,
84
+ consumerTag: `_api-${executionId}`,
85
+ priority: 400,
86
+ });
94
87
 
95
- function stop() {
96
- broker.cancel(`_onmessage-${executionId}`);
97
- broker.cancel(`_api-${executionId}`);
98
- }
88
+ this._debug(`expect ${info.description}`);
89
+
90
+ broker.publish('event', 'activity.wait', cloneContent(executeContent, {message: {...info.message}}));
91
+ };
92
+
93
+ proto._onCatchMessage = function onCatchMessage(routingKey, message) {
94
+ const content = message.content;
95
+
96
+ const {id: signalId, executionId: signalExecutionId} = content.message || {};
97
+ const {message: referenceMessage, description} = this[referenceInfoSymbol];
98
+
99
+ if (!referenceMessage.id && signalId || signalExecutionId) {
100
+ if (this.loopCharacteristics && signalExecutionId !== this.executionId) return;
101
+ if (signalId !== this.id && signalExecutionId !== this.executionId) return;
102
+ this._debug('caught direct message');
103
+ } else if (referenceMessage.id !== signalId) return;
104
+ else {
105
+ this._debug(`caught ${description}`);
99
106
  }
100
107
 
101
- function setupMessageHandling(executionId) {
102
- broker.subscribeTmp('api', '#.signal.*', onDelegateMessage, {noAck: true, consumerTag: `_api-delegated-${executionId}`}, {noAck: true});
103
- broker.subscribeTmp('api', `activity.stop.${executionId}`, onStopApiMessage, {noAck: true, consumerTag: `_api-stop-${executionId}`, priority: 400});
104
- broker.subscribeTmp('execution', 'execute.#', onComplete, {noAck: true, consumerTag: `_execution-complete-${executionId}`}, {noAck: true});
108
+ const {type: messageType, correlationId} = message.properties;
109
+ const broker = this.broker;
110
+ const executeContent = this[executeMessageSymbol].content;
105
111
 
106
- function onDelegateMessage(_, message) {
107
- if (!message.properties.delegate) return;
108
- broker.sendToQueue('message', message.content, message.properties);
109
- }
112
+ broker.publish('event', 'activity.consumed', cloneContent(executeContent, {message: {...message.content.message}}), {correlationId, type: messageType});
113
+ broker.publish('event', 'activity.catch', cloneContent(executeContent, {message: message.content.message}), {type: 'catch', correlationId});
110
114
 
111
- function onStopApiMessage() {
112
- stop(true);
113
- }
115
+ this._complete(message.content.message, {correlationId});
116
+ };
114
117
 
115
- function onComplete(routingKey, {content}) {
116
- if (!content.isRootScope) return;
117
- switch (routingKey) {
118
- case 'execute.completed':
119
- case 'execute.error':
120
- case 'execute.discard':
121
- stop();
122
- break;
123
- }
118
+ proto._onApiMessage = function onApiMessage(routingKey, message) {
119
+ const {type: messageType, correlationId} = message.properties;
120
+ switch (messageType) {
121
+ case 'message':
122
+ case 'signal': {
123
+ return this._complete(message.content.message, {correlationId});
124
124
  }
125
-
126
- function stop(keepMessageQ) {
127
- broker.cancel(`_api-delegated-${executionId}`);
128
- broker.cancel(`_api-stop-${executionId}`);
129
- broker.cancel(`_execution-complete-${executionId}`);
130
- if (!keepMessageQ) broker.purgeQueue('message');
125
+ case 'discard': {
126
+ this[completedSymbol] = true;
127
+ this._stop();
128
+ return this.broker.publish('execution', 'execute.discard', cloneContent(this[executeMessageSymbol].content), {correlationId});
131
129
  }
130
+ case 'stop': {
131
+ return this._stop();
132
+ }
133
+ }
134
+ };
135
+
136
+ proto._complete = function complete(output, options) {
137
+ this[completedSymbol] = true;
138
+ this._stop();
139
+ return this.broker.publish('execution', 'execute.completed', cloneContent(this[executeMessageSymbol].content, {output}), options);
140
+ };
141
+
142
+ proto._stop = function stop() {
143
+ const broker = this.broker, executionId = this.executionId;
144
+ broker.cancel(`_onmessage-${executionId}`);
145
+ broker.cancel(`_api-${executionId}`);
146
+ };
147
+
148
+ proto._setupMessageHandling = function setupMessageHandling(executionId) {
149
+ const broker = this.broker;
150
+ broker.subscribeTmp('api', '#.signal.*', this._onDelegateMessage.bind(this), {
151
+ noAck: true,
152
+ consumerTag: `_api-delegated-${executionId}`,
153
+ }, {
154
+ noAck: true,
155
+ });
156
+ broker.subscribeTmp('api', `activity.stop.${executionId}`, this._onStopApiMessage.bind(this), {
157
+ noAck: true,
158
+ consumerTag: `_api-stop-${executionId}`,
159
+ priority: 400,
160
+ });
161
+ broker.subscribeTmp('execution', 'execute.#', this._onExecutionComplete.bind(this), {
162
+ noAck: true,
163
+ consumerTag: `_execution-complete-${executionId}`,
164
+ }, {
165
+ noAck: true,
166
+ });
167
+ };
168
+
169
+ proto._onDelegateMessage = function onDelegateMessage(_, message) {
170
+ if (!message.properties.delegate) return;
171
+ this.broker.sendToQueue('message', message.content, message.properties);
172
+ };
173
+
174
+ proto._onStopApiMessage = function onStopApiMessage() {
175
+ this._stopMessageHandling(true);
176
+ };
177
+
178
+ proto._onExecutionComplete = function onExecutionComplete(routingKey, {content}) {
179
+ if (!content.isRootScope) return;
180
+ switch (routingKey) {
181
+ case 'execute.completed':
182
+ case 'execute.error':
183
+ case 'execute.discard':
184
+ this._stopMessageHandling();
185
+ break;
186
+ }
187
+ };
188
+
189
+ proto._stopMessageHandling = function stop(keepMessageQ) {
190
+ const broker = this.broker, executionId = this.executionId;
191
+ broker.cancel(`_api-delegated-${executionId}`);
192
+ broker.cancel(`_api-stop-${executionId}`);
193
+ broker.cancel(`_execution-complete-${executionId}`);
194
+ if (!keepMessageQ) broker.purgeQueue('message');
195
+ };
196
+
197
+ proto._getReferenceInfo = function getReferenceInfo(message) {
198
+ const referenceElement = this.referenceElement;
199
+ if (!referenceElement) {
200
+ return {
201
+ message: {...this.reference},
202
+ description: 'anonymous message',
203
+ };
132
204
  }
133
205
 
134
- function resolveReference(message) {
135
- if (!referenceElement) {
136
- return {
137
- message: {...reference},
138
- description: 'anonymous message',
139
- };
140
- }
206
+ const result = {
207
+ message: referenceElement.resolve(message),
208
+ };
141
209
 
142
- const result = {
143
- message: referenceElement.resolve(message),
144
- };
210
+ result.description = `${result.message.name} <${result.message.id}>`;
145
211
 
146
- result.description = `${result.message.name} <${result.message.id}>`;
212
+ return result;
213
+ };
147
214
 
148
- return result;
149
- }
150
- }
215
+ proto._debug = function debug(msg) {
216
+ this.logger.debug(`<${this.executionId} (${this.id})> ${msg}`);
217
+ };
@@ -4,46 +4,43 @@ import { ActivityError } from '../error/Errors';
4
4
  import {cloneContent, cloneMessage} from '../messageHelper';
5
5
 
6
6
  export default function ScriptTask(activityDef, context) {
7
- return Activity(ScriptTaskBehaviour, activityDef, context);
7
+ return new Activity(ScriptTaskBehaviour, activityDef, context);
8
8
  }
9
9
 
10
10
  export function ScriptTaskBehaviour(activity) {
11
- const {id, type, behaviour, broker, logger, environment, emitFatal} = activity;
11
+ const {id, type, behaviour} = activity;
12
12
 
13
- const {scriptFormat} = activity.behaviour;
13
+ this.id = id;
14
+ this.type = type;
15
+ this.scriptFormat = behaviour.scriptFormat;
14
16
 
15
- const loopCharacteristics = behaviour.loopCharacteristics && behaviour.loopCharacteristics.Behaviour(activity, behaviour.loopCharacteristics);
17
+ this.loopCharacteristics = behaviour.loopCharacteristics && new behaviour.loopCharacteristics.Behaviour(activity, behaviour.loopCharacteristics);
18
+ this.activity = activity;
19
+ const environment = this.environment = activity.environment;
16
20
 
17
21
  environment.registerScript(activity);
22
+ }
18
23
 
19
- const source = {
20
- id,
21
- type,
22
- loopCharacteristics,
23
- execute,
24
- };
25
-
26
- return source;
27
-
28
- function execute(executeMessage) {
29
- const content = executeMessage.content;
30
- if (loopCharacteristics && content.isRootScope) {
31
- return loopCharacteristics.execute(executeMessage);
32
- }
24
+ ScriptTaskBehaviour.prototype.execute = function execute(executeMessage) {
25
+ const executeContent = executeMessage.content;
26
+ const loopCharacteristics = this.loopCharacteristics;
27
+ if (loopCharacteristics && executeContent.isRootScope) {
28
+ return loopCharacteristics.execute(executeMessage);
29
+ }
33
30
 
34
- const script = environment.getScript(scriptFormat, activity, cloneMessage(executeMessage));
35
- if (!script) {
36
- return emitFatal(new ActivityError(`Script format ${scriptFormat} is unsupported or was not registered for <${activity.id}>`, executeMessage), content);
37
- }
31
+ const activity = this.activity, scriptFormat = this.scriptFormat;
32
+ const script = this.environment.getScript(scriptFormat, activity, cloneMessage(executeMessage));
33
+ if (!script) {
34
+ return activity.emitFatal(new ActivityError(`Script format ${scriptFormat} is unsupported or was not registered for <${activity.id}>`, executeMessage), executeContent);
35
+ }
38
36
 
39
- return script.execute(ExecutionScope(activity, executeMessage), scriptCallback);
37
+ return script.execute(ExecutionScope(activity, executeMessage), scriptCallback);
40
38
 
41
- function scriptCallback(err, output) {
42
- if (err) {
43
- logger.error(`<${content.executionId} (${id})>`, err);
44
- return broker.publish('execution', 'execute.error', cloneContent(content, {error: new ActivityError(err.message, executeMessage, err)}, {mandatory: true}));
45
- }
46
- return broker.publish('execution', 'execute.completed', cloneContent(content, {output}));
39
+ function scriptCallback(err, output) {
40
+ if (err) {
41
+ activity.logger.error(`<${executeContent.executionId} (${activity.id})>`, err);
42
+ return activity.broker.publish('execution', 'execute.error', cloneContent(executeContent, {error: new ActivityError(err.message, executeMessage, err)}, {mandatory: true}));
47
43
  }
44
+ return activity.broker.publish('execution', 'execute.completed', cloneContent(executeContent, {output}));
48
45
  }
49
- }
46
+ };
@@ -1,24 +1,19 @@
1
1
  import ExecutionScope from '../activity/ExecutionScope';
2
2
 
3
3
  export default function ServiceImplementation(activity) {
4
- const {type: atype, behaviour, environment} = activity;
5
- const implementation = behaviour.implementation;
6
-
7
- const type = `${atype}:implementation`;
8
-
9
- return {
10
- type,
11
- implementation,
12
- execute,
13
- };
4
+ this.type = `${activity.type}:implementation`;
5
+ this.implementation = activity.behaviour.implementation;
6
+ this.activity = activity;
7
+ }
14
8
 
15
- function execute(executionMessage, callback) {
16
- const serviceFn = environment.resolveExpression(implementation, executionMessage);
9
+ ServiceImplementation.prototype.execute = function execute(executionMessage, callback) {
10
+ const activity = this.activity;
11
+ const implementation = this.implementation;
12
+ const serviceFn = activity.environment.resolveExpression(implementation, executionMessage);
17
13
 
18
- if (typeof serviceFn !== 'function') return callback(new Error(`Implementation ${implementation} did not resolve to a function`));
14
+ if (typeof serviceFn !== 'function') return callback(new Error(`Implementation ${implementation} did not resolve to a function`));
19
15
 
20
- serviceFn.call(activity, ExecutionScope(activity, executionMessage), (err, ...args) => {
21
- callback(err, args);
22
- });
23
- }
24
- }
16
+ serviceFn.call(activity, ExecutionScope(activity, executionMessage), (err, ...args) => {
17
+ callback(err, args);
18
+ });
19
+ };
@@ -3,80 +3,87 @@ import {ActivityError} from '../error/Errors';
3
3
  import {cloneMessage, cloneContent} from '../messageHelper';
4
4
 
5
5
  export default function ServiceTask(activityDef, context) {
6
- return Activity(ServiceTaskBehaviour, activityDef, context);
6
+ return new Activity(ServiceTaskBehaviour, activityDef, context);
7
7
  }
8
8
 
9
9
  export function ServiceTaskBehaviour(activity) {
10
- const {id, type, broker, logger, behaviour, environment, emitFatal} = activity;
11
- const loopCharacteristics = behaviour.loopCharacteristics && behaviour.loopCharacteristics.Behaviour(activity, behaviour.loopCharacteristics);
10
+ const {id, type, behaviour} = activity;
12
11
 
13
- const source = {
14
- id,
15
- type,
16
- loopCharacteristics,
17
- execute,
18
- getService,
19
- };
12
+ this.id = id;
13
+ this.type = type;
14
+ this.loopCharacteristics = behaviour.loopCharacteristics && new behaviour.loopCharacteristics.Behaviour(activity, behaviour.loopCharacteristics);
15
+ this.activity = activity;
16
+ this.environment = activity.environment;
17
+ this.broker = activity.broker;
18
+ }
20
19
 
21
- return source;
20
+ const proto = ServiceTaskBehaviour.prototype;
22
21
 
23
- function execute(executeMessage) {
24
- const content = executeMessage.content;
25
- if (loopCharacteristics && content.isRootScope) {
26
- return loopCharacteristics.execute(executeMessage);
27
- }
22
+ proto.execute = function execute(executeMessage) {
23
+ const executeContent = executeMessage.content;
24
+ const loopCharacteristics = this.loopCharacteristics;
25
+ if (loopCharacteristics && executeContent.isRootScope) {
26
+ return loopCharacteristics.execute(executeMessage);
27
+ }
28
28
 
29
- const {executionId} = content;
30
- const service = getService(executeMessage);
31
- if (!service) return emitFatal(new ActivityError(`<${id}> service not defined`, executeMessage), content);
29
+ const executionId = executeContent.executionId;
30
+ const service = this.service = this.getService(executeMessage);
31
+ if (!service) return this.activity.emitFatal(new ActivityError(`<${this.id}> service not defined`, executeMessage), executeContent);
32
32
 
33
- broker.subscribeTmp('api', `activity.#.${content.executionId}`, onApiMessage, {consumerTag: `_api-${executionId}`});
33
+ const broker = this.broker;
34
+ broker.subscribeTmp('api', `activity.#.${executionId}`, (...args) => this._onApiMessage(executeMessage, ...args), {consumerTag: `_api-${executionId}`});
34
35
 
35
- return service.execute(executeMessage, (err, output) => {
36
- broker.cancel(`_api-${executionId}`);
37
- if (err) {
38
- logger.error(`<${content.executionId} (${id})>`, err);
39
- return broker.publish('execution', 'execute.error', cloneContent(content, {error: new ActivityError(err.message, executeMessage, err)}, {mandatory: true}));
40
- }
36
+ return service.execute(executeMessage, (err, output) => {
37
+ broker.cancel(`_api-${executionId}`);
38
+ if (err) {
39
+ this.activity.logger.error(`<${executionId} (${this.id})>`, err);
40
+ return broker.publish('execution', 'execute.error', cloneContent(executeContent, {error: new ActivityError(err.message, executeMessage, err)}, {mandatory: true}));
41
+ }
41
42
 
42
- return broker.publish('execution', 'execute.completed', cloneContent(content, {output, state: 'complete'}));
43
- });
43
+ return broker.publish('execution', 'execute.completed', cloneContent(executeContent, {output, state: 'complete'}));
44
+ });
45
+ };
44
46
 
45
- function onApiMessage(_, message) {
46
- if (message.properties.type === 'discard') {
47
- broker.cancel(`_api-${executionId}`);
48
- if (service && service.discard) service.discard(message);
49
- logger.debug(`<${content.executionId} (${id})> discarded`);
50
- return broker.publish('execution', 'execute.discard', cloneContent(content, {state: 'discard'}));
51
- }
52
- if (message.properties.type === 'stop') {
53
- broker.cancel(`_api-${executionId}`);
54
- if (service && service.stop) service.stop(message);
55
- return logger.debug(`<${content.executionId} (${id})> stopped`);
56
- }
57
- }
47
+ proto.getService = function getService(message) {
48
+ let Service = this.activity.behaviour.Service;
49
+ if (!Service) {
50
+ Service = this.environment.settings.enableDummyService ? DummyService : null;
58
51
  }
52
+ return Service && new Service(this.activity, cloneMessage(message));
53
+ };
59
54
 
60
- function getService(message) {
61
- const Service = behaviour.Service;
62
- if (!Service) {
63
- return environment.settings.enableDummyService ? DummyService(activity) : null;
55
+ proto._onApiMessage = function onApiMessage(executeMessage, _, message) {
56
+ const broker = this.broker;
57
+ switch (message.properties.type) {
58
+ case 'discard': {
59
+ const executionId = executeMessage.content.executionId;
60
+ broker.cancel(`_api-${executionId}`);
61
+ const service = this.service;
62
+ if (service) {
63
+ if (service.discard) service.discard(message);
64
+ else if (service.stop) service.stop(message);
65
+ }
66
+ this.activity.logger.debug(`<${executionId} (${this.id})> discarded`);
67
+ return broker.publish('execution', 'execute.discard', cloneContent(executeMessage.content, {state: 'discard'}));
64
68
  }
65
- return Service(activity, cloneMessage(message));
66
- }
67
-
68
- function DummyService() {
69
- logger.debug(`<${id}> returning dummy service`);
70
-
71
- return {
72
- type: 'dummyservice',
73
- execute: executeDummyService,
74
- };
75
-
76
- function executeDummyService(...args) {
77
- logger.debug(`<${id}> executing dummy service`);
78
- const next = args.pop();
79
- next();
69
+ case 'stop': {
70
+ const executionId = executeMessage.content.executionId;
71
+ broker.cancel(`_api-${executionId}`);
72
+ const service = this.service;
73
+ if (service && service.stop) service.stop(message);
74
+ return this.activity.logger.debug(`<${executionId} (${this.id})> stopped`);
80
75
  }
81
76
  }
77
+ };
78
+
79
+ function DummyService(activity) {
80
+ this.type = 'dummyservice';
81
+ this.activity = activity;
82
82
  }
83
+
84
+ DummyService.prototype.execute = function executeDummyService(...args) {
85
+ const activity = this.activity;
86
+ activity.logger.debug(`<${activity.id}> executing dummy service`);
87
+ const next = args.pop();
88
+ next();
89
+ };