bpmn-elements 6.0.1 → 8.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +341 -0
- package/README.md +3 -0
- package/dist/index.js +52 -44
- package/dist/src/Api.js +77 -76
- package/dist/src/Context.js +176 -172
- package/dist/src/Environment.js +110 -102
- package/dist/src/EventBroker.js +89 -88
- package/dist/src/ExtensionsMapper.js +2 -2
- package/dist/src/MessageFormatter.js +164 -95
- package/dist/src/Scripts.js +6 -2
- package/dist/src/activity/Activity.js +1105 -916
- package/dist/src/activity/ActivityExecution.js +342 -297
- package/dist/src/activity/Dummy.js +3 -3
- package/dist/src/definition/Definition.js +498 -444
- package/dist/src/definition/DefinitionExecution.js +710 -408
- package/dist/src/error/Errors.js +17 -7
- package/dist/src/eventDefinitions/CancelEventDefinition.js +190 -150
- package/dist/src/eventDefinitions/CompensateEventDefinition.js +194 -161
- package/dist/src/eventDefinitions/ConditionalEventDefinition.js +197 -135
- package/dist/src/eventDefinitions/ErrorEventDefinition.js +207 -165
- package/dist/src/eventDefinitions/EscalationEventDefinition.js +175 -141
- package/dist/src/eventDefinitions/EventDefinitionExecution.js +157 -129
- package/dist/src/eventDefinitions/LinkEventDefinition.js +174 -149
- package/dist/src/eventDefinitions/MessageEventDefinition.js +213 -176
- package/dist/src/eventDefinitions/SignalEventDefinition.js +203 -161
- package/dist/src/eventDefinitions/TerminateEventDefinition.js +21 -23
- package/dist/src/eventDefinitions/TimerEventDefinition.js +243 -228
- package/dist/src/events/BoundaryEvent.js +180 -144
- package/dist/src/events/EndEvent.js +18 -23
- package/dist/src/events/IntermediateCatchEvent.js +44 -58
- package/dist/src/events/IntermediateThrowEvent.js +18 -23
- package/dist/src/events/StartEvent.js +109 -94
- package/dist/src/flows/Association.js +94 -100
- package/dist/src/flows/MessageFlow.js +86 -103
- package/dist/src/flows/SequenceFlow.js +173 -182
- package/dist/src/gateways/EventBasedGateway.js +88 -84
- package/dist/src/gateways/ExclusiveGateway.js +13 -16
- package/dist/src/gateways/InclusiveGateway.js +11 -14
- package/dist/src/gateways/ParallelGateway.js +11 -14
- package/dist/src/getPropertyValue.js +34 -34
- package/dist/src/io/BpmnIO.js +17 -14
- package/dist/src/io/EnvironmentDataObject.js +33 -29
- package/dist/src/io/EnvironmentDataStore.js +33 -29
- package/dist/src/io/EnvironmentDataStoreReference.js +35 -31
- package/dist/src/io/InputOutputSpecification.js +177 -168
- package/dist/src/io/Properties.js +117 -124
- package/dist/src/messageHelper.js +1 -1
- package/dist/src/process/Process.js +439 -362
- package/dist/src/process/ProcessExecution.js +748 -646
- package/dist/src/shared.js +2 -2
- package/dist/src/tasks/CallActivity.js +160 -0
- package/dist/src/tasks/LoopCharacteristics.js +309 -330
- package/dist/src/tasks/ReceiveTask.js +233 -182
- package/dist/src/tasks/ScriptTask.js +35 -41
- package/dist/src/tasks/ServiceImplementation.js +13 -20
- package/dist/src/tasks/ServiceTask.js +82 -75
- package/dist/src/tasks/SignalTask.js +97 -93
- package/dist/src/tasks/StandardLoopCharacteristics.js +1 -1
- package/dist/src/tasks/SubProcess.js +193 -175
- package/dist/src/tasks/Task.js +17 -19
- package/index.js +2 -0
- package/package.json +16 -16
- package/src/Api.js +65 -59
- package/src/Context.js +145 -140
- package/src/Environment.js +116 -100
- package/src/EventBroker.js +67 -68
- package/src/ExtensionsMapper.js +2 -2
- package/src/MessageFormatter.js +132 -74
- package/src/activity/Activity.js +914 -776
- package/src/activity/ActivityExecution.js +293 -247
- package/src/activity/Dummy.js +2 -2
- package/src/definition/Definition.js +437 -401
- package/src/definition/DefinitionExecution.js +598 -340
- package/src/error/Errors.js +11 -6
- package/src/eventDefinitions/CancelEventDefinition.js +164 -121
- package/src/eventDefinitions/CompensateEventDefinition.js +159 -124
- package/src/eventDefinitions/ConditionalEventDefinition.js +147 -104
- package/src/eventDefinitions/ErrorEventDefinition.js +190 -131
- package/src/eventDefinitions/EscalationEventDefinition.js +139 -101
- package/src/eventDefinitions/EventDefinitionExecution.js +127 -95
- package/src/eventDefinitions/LinkEventDefinition.js +160 -129
- package/src/eventDefinitions/MessageEventDefinition.js +178 -121
- package/src/eventDefinitions/SignalEventDefinition.js +162 -106
- package/src/eventDefinitions/TerminateEventDefinition.js +19 -19
- package/src/eventDefinitions/TimerEventDefinition.js +202 -167
- package/src/events/BoundaryEvent.js +156 -115
- package/src/events/EndEvent.js +15 -18
- package/src/events/IntermediateCatchEvent.js +40 -44
- package/src/events/IntermediateThrowEvent.js +15 -18
- package/src/events/StartEvent.js +84 -50
- package/src/flows/Association.js +98 -112
- package/src/flows/MessageFlow.js +81 -97
- package/src/flows/SequenceFlow.js +146 -160
- package/src/gateways/EventBasedGateway.js +75 -68
- package/src/gateways/ExclusiveGateway.js +8 -13
- package/src/gateways/InclusiveGateway.js +8 -13
- package/src/gateways/ParallelGateway.js +8 -13
- package/src/getPropertyValue.js +34 -33
- package/src/io/BpmnIO.js +20 -15
- package/src/io/EnvironmentDataObject.js +29 -18
- package/src/io/EnvironmentDataStore.js +29 -18
- package/src/io/EnvironmentDataStoreReference.js +31 -20
- package/src/io/InputOutputSpecification.js +154 -157
- package/src/io/Properties.js +95 -97
- package/src/process/Process.js +378 -333
- package/src/process/ProcessExecution.js +603 -553
- package/src/tasks/CallActivity.js +130 -0
- package/src/tasks/LoopCharacteristics.js +290 -289
- package/src/tasks/ReceiveTask.js +174 -107
- package/src/tasks/ScriptTask.js +27 -30
- package/src/tasks/ServiceImplementation.js +13 -18
- package/src/tasks/ServiceTask.js +67 -60
- package/src/tasks/SignalTask.js +77 -52
- package/src/tasks/StandardLoopCharacteristics.js +1 -1
- package/src/tasks/SubProcess.js +184 -157
- package/src/tasks/Task.js +15 -19
package/src/activity/Activity.js
CHANGED
|
@@ -7,943 +7,1081 @@ import {Formatter} from '../MessageFormatter';
|
|
|
7
7
|
import {cloneContent, cloneParent, cloneMessage} from '../messageHelper';
|
|
8
8
|
import {makeErrorFromMessage, ActivityError} from '../error/Errors';
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
10
|
+
const kActivityDef = Symbol.for('activityDefinition');
|
|
11
|
+
const kBpmnIo = Symbol.for('bpmnIo');
|
|
12
|
+
const kConsuming = Symbol.for('consuming');
|
|
13
|
+
const kCounters = Symbol.for('counters');
|
|
14
|
+
const kEventDefinitions = Symbol.for('eventDefinitions');
|
|
15
|
+
const kExec = Symbol.for('exec');
|
|
16
|
+
const kExecuteMessage = Symbol.for('executeMessage');
|
|
17
|
+
const kExtensions = Symbol.for('extensions');
|
|
18
|
+
const kFlags = Symbol.for('flags');
|
|
19
|
+
const kFlows = Symbol.for('flows');
|
|
20
|
+
const kFormatter = Symbol.for('formatter');
|
|
21
|
+
const kMessageHandlers = Symbol.for('messageHandlers');
|
|
22
|
+
const kStateMessage = Symbol.for('stateMessage');
|
|
23
|
+
|
|
24
|
+
export default Activity;
|
|
25
|
+
|
|
26
|
+
function Activity(Behaviour, activityDef, context) {
|
|
27
|
+
const {id, type = 'activity', name, behaviour = {}} = activityDef;
|
|
28
|
+
const {attachedTo: attachedToRef, eventDefinitions} = behaviour;
|
|
16
29
|
|
|
17
|
-
|
|
18
|
-
|
|
30
|
+
this[kActivityDef] = activityDef;
|
|
31
|
+
this.id = id;
|
|
32
|
+
this.type = type;
|
|
33
|
+
this.name = name;
|
|
34
|
+
this.behaviour = {...behaviour, eventDefinitions};
|
|
35
|
+
this.Behaviour = Behaviour;
|
|
36
|
+
this.parent = activityDef.parent ? cloneParent(activityDef.parent) : {};
|
|
37
|
+
this.logger = context.environment.Logger(type.toLowerCase());
|
|
38
|
+
this.environment = context.environment;
|
|
39
|
+
this.context = context;
|
|
40
|
+
this[kCounters] = {
|
|
41
|
+
taken: 0,
|
|
42
|
+
discarded: 0,
|
|
43
|
+
};
|
|
19
44
|
|
|
20
|
-
const {attachedTo: attachedToRef, eventDefinitions} = behaviour;
|
|
21
45
|
let attachedToActivity, attachedTo;
|
|
22
|
-
|
|
23
46
|
if (attachedToRef) {
|
|
24
47
|
attachedTo = attachedToRef.id;
|
|
25
48
|
attachedToActivity = context.getActivityById(attachedToRef.id);
|
|
26
49
|
}
|
|
27
50
|
|
|
28
|
-
const
|
|
29
|
-
const outboundSequenceFlows = getOutboundSequenceFlows(id) || [];
|
|
30
|
-
const inboundAssociations = getInboundAssociations(id) || [];
|
|
51
|
+
const {broker, on, once, waitFor, emitFatal} = ActivityBroker(this);
|
|
31
52
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
let execution, initExecutionId, executionId, stateMessage, status, stopped = false, executeMessage, consumingRunQ;
|
|
53
|
+
this.broker = broker;
|
|
54
|
+
this.on = on;
|
|
55
|
+
this.once = once;
|
|
56
|
+
this.waitFor = waitFor;
|
|
57
|
+
this.emitFatal = emitFatal;
|
|
38
58
|
|
|
59
|
+
const inboundSequenceFlows = context.getInboundSequenceFlows(id);
|
|
60
|
+
const inboundAssociations = context.getInboundAssociations(id);
|
|
39
61
|
const inboundTriggers = attachedToActivity ? [attachedToActivity] : inboundSequenceFlows.slice();
|
|
40
|
-
const
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
62
|
+
const outboundSequenceFlows = context.getOutboundSequenceFlows(id);
|
|
63
|
+
const flows = this[kFlows] = {
|
|
64
|
+
inboundSequenceFlows,
|
|
65
|
+
inboundAssociations,
|
|
66
|
+
inboundJoinFlows: [],
|
|
67
|
+
inboundTriggers,
|
|
68
|
+
outboundSequenceFlows,
|
|
69
|
+
outboundEvaluator: new OutboundEvaluator(this, outboundSequenceFlows),
|
|
45
70
|
};
|
|
46
71
|
|
|
47
|
-
const
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
isThrowing,
|
|
72
|
+
const isForCompensation = !!behaviour.isForCompensation;
|
|
73
|
+
const isParallelJoin = activityDef.isParallelGateway && flows.inboundSequenceFlows.length > 1;
|
|
74
|
+
this[kFlags] = {
|
|
75
|
+
isEnd: flows.outboundSequenceFlows.length === 0,
|
|
76
|
+
isStart: flows.inboundSequenceFlows.length === 0 && !attachedTo && !behaviour.triggeredByEvent && !isForCompensation,
|
|
77
|
+
isSubProcess: activityDef.isSubProcess,
|
|
78
|
+
isMultiInstance: !!behaviour.loopCharacteristics,
|
|
55
79
|
isForCompensation,
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
environment,
|
|
61
|
-
inbound: inboundSequenceFlows,
|
|
62
|
-
outbound: outboundSequenceFlows,
|
|
63
|
-
get counters() {
|
|
64
|
-
return {...counters};
|
|
65
|
-
},
|
|
66
|
-
get executionId() {
|
|
67
|
-
return executionId;
|
|
68
|
-
},
|
|
69
|
-
get status() {
|
|
70
|
-
return status;
|
|
71
|
-
},
|
|
72
|
-
get stopped() {
|
|
73
|
-
return stopped;
|
|
74
|
-
},
|
|
75
|
-
get isRunning() {
|
|
76
|
-
if (!consumingRunQ) return false;
|
|
77
|
-
return !!status;
|
|
78
|
-
},
|
|
79
|
-
Behaviour,
|
|
80
|
-
activate,
|
|
81
|
-
deactivate,
|
|
82
|
-
evaluateOutbound,
|
|
83
|
-
logger,
|
|
84
|
-
discard,
|
|
85
|
-
getApi,
|
|
86
|
-
getActivityById,
|
|
87
|
-
getState,
|
|
88
|
-
init,
|
|
89
|
-
recover,
|
|
90
|
-
resume,
|
|
91
|
-
run,
|
|
92
|
-
shake,
|
|
93
|
-
stop,
|
|
94
|
-
next: step && next,
|
|
80
|
+
attachedTo,
|
|
81
|
+
isTransaction: activityDef.isTransaction,
|
|
82
|
+
isParallelJoin,
|
|
83
|
+
isThrowing: activityDef.isThrowing,
|
|
95
84
|
};
|
|
85
|
+
this[kExec] = {};
|
|
96
86
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
const runQ = broker.getQueue('run-q');
|
|
105
|
-
const executionQ = broker.getQueue('execution-q');
|
|
106
|
-
const inboundQ = broker.assertQueue('inbound-q', {durable: true, autoDelete: false});
|
|
107
|
-
|
|
108
|
-
const formatRunQ = broker.getQueue('format-run-q');
|
|
109
|
-
const formatter = Formatter({id, broker, logger}, formatRunQ);
|
|
87
|
+
this[kMessageHandlers] = {
|
|
88
|
+
onInbound: isParallelJoin ? this._onJoinInbound.bind(this) : this._onInbound.bind(this),
|
|
89
|
+
onRunMessage: this._onRunMessage.bind(this),
|
|
90
|
+
onApiMessage: this._onApiMessage.bind(this),
|
|
91
|
+
onExecutionMessage: this._onExecutionMessage.bind(this),
|
|
92
|
+
};
|
|
110
93
|
|
|
94
|
+
const onInboundEvent = this._onInboundEvent.bind(this);
|
|
95
|
+
broker.assertQueue('inbound-q', {durable: true, autoDelete: false});
|
|
111
96
|
if (isForCompensation) {
|
|
112
|
-
|
|
97
|
+
for (const trigger of inboundAssociations) {
|
|
113
98
|
trigger.broker.subscribeTmp('event', '#', onInboundEvent, {noAck: true, consumerTag: `_inbound-${id}`});
|
|
114
|
-
}
|
|
99
|
+
}
|
|
115
100
|
} else {
|
|
116
|
-
|
|
101
|
+
for (const trigger of inboundTriggers) {
|
|
117
102
|
if (trigger.isSequenceFlow) trigger.broker.subscribeTmp('event', 'flow.#', onInboundEvent, {noAck: true, consumerTag: `_inbound-${id}`});
|
|
118
103
|
else trigger.broker.subscribeTmp('event', 'activity.#', onInboundEvent, {noAck: true, consumerTag: `_inbound-${id}`});
|
|
119
|
-
}
|
|
104
|
+
}
|
|
120
105
|
}
|
|
121
106
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
get: () => broker,
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
Object.defineProperty(activityApi, 'execution', {
|
|
128
|
-
enumerable: true,
|
|
129
|
-
get: () => execution,
|
|
130
|
-
});
|
|
131
|
-
|
|
132
|
-
const bpmnIo = BpmnIO(activityApi, context);
|
|
133
|
-
|
|
134
|
-
const loaedEventDefinitions = eventDefinitions && eventDefinitions.map((ed) => ed.Behaviour(activityApi, ed, context));
|
|
135
|
-
Object.defineProperty(activityApi, 'eventDefinitions', {
|
|
136
|
-
enumerable: true,
|
|
137
|
-
get: () => loaedEventDefinitions,
|
|
138
|
-
});
|
|
139
|
-
|
|
140
|
-
const extensions = context.loadExtensions(activityApi);
|
|
141
|
-
Object.defineProperty(activityApi, 'extensions', {
|
|
142
|
-
enumerable: true,
|
|
143
|
-
get: () => extensions,
|
|
144
|
-
});
|
|
145
|
-
|
|
146
|
-
return activityApi;
|
|
107
|
+
this[kEventDefinitions] = eventDefinitions && eventDefinitions.map((ed) => new ed.Behaviour(this, ed, this.context));
|
|
108
|
+
}
|
|
147
109
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
110
|
+
const proto = Activity.prototype;
|
|
111
|
+
|
|
112
|
+
Object.defineProperty(proto, 'counters', {
|
|
113
|
+
enumerable: true,
|
|
114
|
+
get() {
|
|
115
|
+
return {...this[kCounters]};
|
|
116
|
+
},
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
Object.defineProperty(proto, 'execution', {
|
|
120
|
+
enumerable: true,
|
|
121
|
+
get() {
|
|
122
|
+
return this[kExec].execution;
|
|
123
|
+
},
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
Object.defineProperty(proto, 'executionId', {
|
|
127
|
+
enumerable: true,
|
|
128
|
+
get() {
|
|
129
|
+
return this[kExec].executionId;
|
|
130
|
+
},
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
Object.defineProperty(proto, 'bpmnIo', {
|
|
134
|
+
enumerable: true,
|
|
135
|
+
get() {
|
|
136
|
+
if (kBpmnIo in this) return this[kBpmnIo];
|
|
137
|
+
const bpmnIo = this[kBpmnIo] = new BpmnIO(this, this.context);
|
|
138
|
+
return bpmnIo;
|
|
139
|
+
},
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
Object.defineProperty(proto, 'extensions', {
|
|
143
|
+
enumerable: true,
|
|
144
|
+
get() {
|
|
145
|
+
if (kExtensions in this) return this[kExtensions];
|
|
146
|
+
const extensions = this[kExtensions] = this.context.loadExtensions(this);
|
|
147
|
+
return extensions;
|
|
148
|
+
},
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
Object.defineProperty(proto, 'formatter', {
|
|
152
|
+
enumerable: true,
|
|
153
|
+
get() {
|
|
154
|
+
let formatter = this[kFormatter];
|
|
155
|
+
if (formatter) return formatter;
|
|
156
|
+
|
|
157
|
+
const broker = this.broker;
|
|
158
|
+
formatter = this[kFormatter] = new Formatter({
|
|
159
|
+
id: this.id,
|
|
160
|
+
broker,
|
|
161
|
+
logger: this.logger,
|
|
162
|
+
}, broker.getQueue('format-run-q'));
|
|
163
|
+
return formatter;
|
|
164
|
+
},
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
Object.defineProperty(proto, 'isRunning', {
|
|
168
|
+
enumerable: true,
|
|
169
|
+
get() {
|
|
170
|
+
if (!this[kConsuming]) return false;
|
|
171
|
+
return !!this.status;
|
|
172
|
+
},
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
Object.defineProperty(proto, 'outbound', {
|
|
176
|
+
enumerable: true,
|
|
177
|
+
get() {
|
|
178
|
+
return this[kFlows].outboundSequenceFlows;
|
|
179
|
+
},
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
Object.defineProperty(proto, 'inbound', {
|
|
183
|
+
enumerable: true,
|
|
184
|
+
get() {
|
|
185
|
+
return this[kFlows].inboundSequenceFlows;
|
|
186
|
+
},
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
Object.defineProperty(proto, 'isEnd', {
|
|
190
|
+
enumerable: true,
|
|
191
|
+
get() {
|
|
192
|
+
return this[kFlags].isEnd;
|
|
193
|
+
},
|
|
194
|
+
});
|
|
195
|
+
Object.defineProperty(proto, 'isStart', {
|
|
196
|
+
enumerable: true,
|
|
197
|
+
get() {
|
|
198
|
+
return this[kFlags].isStart;
|
|
199
|
+
},
|
|
200
|
+
});
|
|
201
|
+
Object.defineProperty(proto, 'isSubProcess', {
|
|
202
|
+
enumerable: true,
|
|
203
|
+
get() {
|
|
204
|
+
return this[kFlags].isSubProcess;
|
|
205
|
+
},
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
Object.defineProperty(proto, 'isMultiInstance', {
|
|
209
|
+
enumerable: true,
|
|
210
|
+
get() {
|
|
211
|
+
return this[kFlags].isMultiInstance;
|
|
212
|
+
},
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
Object.defineProperty(proto, 'isThrowing', {
|
|
216
|
+
enumerable: true,
|
|
217
|
+
get() {
|
|
218
|
+
return this[kFlags].isThrowing;
|
|
219
|
+
},
|
|
220
|
+
});
|
|
221
|
+
Object.defineProperty(proto, 'isForCompensation', {
|
|
222
|
+
enumerable: true,
|
|
223
|
+
get() {
|
|
224
|
+
return this[kFlags].isForCompensation;
|
|
225
|
+
},
|
|
226
|
+
});
|
|
227
|
+
Object.defineProperty(proto, 'triggeredByEvent', {
|
|
228
|
+
enumerable: true,
|
|
229
|
+
get() {
|
|
230
|
+
return this[kActivityDef].triggeredByEvent;
|
|
231
|
+
},
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
Object.defineProperty(proto, 'attachedTo', {
|
|
235
|
+
enumerable: true,
|
|
236
|
+
get() {
|
|
237
|
+
const attachedToId = this[kFlags].attachedTo;
|
|
238
|
+
if (!attachedToId) return null;
|
|
239
|
+
return this.getActivityById(attachedToId);
|
|
240
|
+
},
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
Object.defineProperty(proto, 'eventDefinitions', {
|
|
244
|
+
enumerable: true,
|
|
245
|
+
get() {
|
|
246
|
+
return this[kEventDefinitions];
|
|
247
|
+
},
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
proto.activate = function activate() {
|
|
251
|
+
if (this[kFlags].isForCompensation) return;
|
|
252
|
+
return this._consumeInbound();
|
|
253
|
+
};
|
|
254
|
+
|
|
255
|
+
proto.deactivate = function deactivate() {
|
|
256
|
+
const broker = this.broker;
|
|
257
|
+
broker.cancel('_run-on-inbound');
|
|
258
|
+
broker.cancel('_format-consumer');
|
|
259
|
+
};
|
|
260
|
+
|
|
261
|
+
proto.init = function init(initContent) {
|
|
262
|
+
const id = this.id;
|
|
263
|
+
const exec = this[kExec];
|
|
264
|
+
const executionId = exec.initExecutionId = exec.initExecutionId || getUniqueId(id);
|
|
265
|
+
this.logger.debug(`<${id}> initialized with executionId <${executionId}>`);
|
|
266
|
+
this._publishEvent('init', this._createMessage({...initContent, executionId}));
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
proto.run = function run(runContent) {
|
|
270
|
+
const id = this.id;
|
|
271
|
+
if (this.isRunning) throw new Error(`activity <${id}> is already running`);
|
|
272
|
+
|
|
273
|
+
const exec = this[kExec];
|
|
274
|
+
const executionId = exec.executionId = exec.initExecutionId || getUniqueId(id);
|
|
275
|
+
exec.initExecutionId = null;
|
|
276
|
+
|
|
277
|
+
this._consumeApi();
|
|
278
|
+
|
|
279
|
+
const content = this._createMessage({...runContent, executionId});
|
|
280
|
+
const broker = this.broker;
|
|
281
|
+
|
|
282
|
+
broker.publish('run', 'run.enter', content);
|
|
283
|
+
broker.publish('run', 'run.start', cloneContent(content));
|
|
284
|
+
|
|
285
|
+
this._consumeRunQ();
|
|
286
|
+
};
|
|
287
|
+
|
|
288
|
+
proto.recover = function recover(state) {
|
|
289
|
+
if (this.isRunning) throw new Error(`cannot recover running activity <${this.id}>`);
|
|
290
|
+
if (!state) return;
|
|
291
|
+
|
|
292
|
+
this.stopped = state.stopped;
|
|
293
|
+
this.status = state.status;
|
|
294
|
+
const exec = this[kExec];
|
|
295
|
+
exec.executionId = state.executionId;
|
|
296
|
+
|
|
297
|
+
this[kCounters] = {...this[kCounters], ...state.counters};
|
|
298
|
+
|
|
299
|
+
if (state.execution) {
|
|
300
|
+
exec.execution = new ActivityExecution(this, this.context).recover(state.execution);
|
|
152
301
|
}
|
|
153
302
|
|
|
154
|
-
|
|
155
|
-
if (activityApi.isRunning) throw new Error(`activity <${id}> is already running`);
|
|
156
|
-
|
|
157
|
-
executionId = initExecutionId || getUniqueId(id);
|
|
158
|
-
initExecutionId = undefined;
|
|
159
|
-
|
|
160
|
-
consumeApi();
|
|
161
|
-
|
|
162
|
-
const content = createMessage({...runContent, executionId});
|
|
303
|
+
this.broker.recover(state.broker);
|
|
163
304
|
|
|
164
|
-
|
|
165
|
-
|
|
305
|
+
return this;
|
|
306
|
+
};
|
|
166
307
|
|
|
167
|
-
|
|
308
|
+
proto.resume = function resume() {
|
|
309
|
+
if (this[kConsuming]) {
|
|
310
|
+
throw new Error(`cannot resume running activity <${this.id}>`);
|
|
168
311
|
}
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
312
|
+
if (!this.status) return this.activate();
|
|
313
|
+
|
|
314
|
+
this.stopped = false;
|
|
315
|
+
|
|
316
|
+
this._consumeApi();
|
|
317
|
+
|
|
318
|
+
const content = this._createMessage();
|
|
319
|
+
this.broker.publish('run', 'run.resume', content, {persistent: false});
|
|
320
|
+
this._consumeRunQ();
|
|
321
|
+
};
|
|
322
|
+
|
|
323
|
+
proto.discard = function discard(discardContent) {
|
|
324
|
+
if (!this.status) return this._runDiscard(discardContent);
|
|
325
|
+
const execution = this[kExec].execution;
|
|
326
|
+
if (execution && !execution.completed) return execution.discard();
|
|
327
|
+
|
|
328
|
+
this._deactivateRunConsumers();
|
|
329
|
+
const broker = this.broker;
|
|
330
|
+
broker.getQueue('run-q').purge();
|
|
331
|
+
broker.publish('run', 'run.discard', cloneContent(this[kStateMessage].content));
|
|
332
|
+
this._consumeRunQ();
|
|
333
|
+
};
|
|
334
|
+
|
|
335
|
+
proto.stop = function stop() {
|
|
336
|
+
if (!this[kConsuming]) return;
|
|
337
|
+
return this.getApi().stop();
|
|
338
|
+
};
|
|
339
|
+
|
|
340
|
+
proto.next = function next() {
|
|
341
|
+
if (!this.environment.settings.step) return;
|
|
342
|
+
const stateMessage = this[kStateMessage];
|
|
343
|
+
if (!stateMessage) return;
|
|
344
|
+
if (this.status === 'executing') return false;
|
|
345
|
+
if (this.status === 'formatting') return false;
|
|
346
|
+
const current = stateMessage;
|
|
347
|
+
stateMessage.ack();
|
|
348
|
+
return current;
|
|
349
|
+
};
|
|
350
|
+
|
|
351
|
+
proto.shake = function shake() {
|
|
352
|
+
this._shakeOutbound({content: this._createMessage()});
|
|
353
|
+
};
|
|
354
|
+
|
|
355
|
+
proto.evaluateOutbound = function evaluateOutbound(fromMessage, discardRestAtTake, callback) {
|
|
356
|
+
return this[kFlows].outboundEvaluator.evaluate(fromMessage, discardRestAtTake, callback);
|
|
357
|
+
};
|
|
358
|
+
|
|
359
|
+
proto.getState = function getState() {
|
|
360
|
+
const msg = this._createMessage();
|
|
361
|
+
|
|
362
|
+
const exec = this[kExec];
|
|
363
|
+
return {
|
|
364
|
+
...msg,
|
|
365
|
+
executionId: exec.executionId,
|
|
366
|
+
stopped: this.stopped,
|
|
367
|
+
behaviour: {...this.behaviour},
|
|
368
|
+
counters: this.counters,
|
|
369
|
+
broker: this.broker.getState(true),
|
|
370
|
+
execution: exec.execution && exec.execution.getState(),
|
|
371
|
+
};
|
|
372
|
+
};
|
|
373
|
+
|
|
374
|
+
proto.getApi = function getApi(message) {
|
|
375
|
+
const execution = this[kExec].execution;
|
|
376
|
+
if (execution && !execution.completed) return execution.getApi(message);
|
|
377
|
+
return ActivityApi(this.broker, message || this[kStateMessage]);
|
|
378
|
+
};
|
|
379
|
+
|
|
380
|
+
proto.getActivityById = function getActivityById(elementId) {
|
|
381
|
+
return this.context.getActivityById(elementId);
|
|
382
|
+
};
|
|
383
|
+
|
|
384
|
+
proto._runDiscard = function runDiscard(discardContent) {
|
|
385
|
+
const exec = this[kExec];
|
|
386
|
+
const executionId = exec.executionId = exec.initExecutionId || getUniqueId(this.id);
|
|
387
|
+
exec.initExecutionId = null;
|
|
388
|
+
|
|
389
|
+
this._consumeApi();
|
|
390
|
+
|
|
391
|
+
const content = this._createMessage({...discardContent, executionId});
|
|
392
|
+
this.broker.publish('run', 'run.discard', content);
|
|
393
|
+
|
|
394
|
+
this._consumeRunQ();
|
|
395
|
+
};
|
|
396
|
+
|
|
397
|
+
proto._discardRun = function discardRun() {
|
|
398
|
+
const status = this.status;
|
|
399
|
+
if (!status) return;
|
|
400
|
+
|
|
401
|
+
const execution = this[kExec].execution;
|
|
402
|
+
if (execution && !execution.completed) return;
|
|
403
|
+
switch (status) {
|
|
404
|
+
case 'executing':
|
|
405
|
+
case 'error':
|
|
406
|
+
case 'discarded':
|
|
407
|
+
return;
|
|
205
408
|
}
|
|
206
409
|
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
410
|
+
this._deactivateRunConsumers();
|
|
411
|
+
if (this.extensions) this.extensions.deactivate();
|
|
412
|
+
const broker = this.broker;
|
|
413
|
+
broker.getQueue('run-q').purge();
|
|
414
|
+
broker.publish('run', 'run.discard', cloneContent(this[kStateMessage].content));
|
|
415
|
+
this._consumeRunQ();
|
|
416
|
+
};
|
|
212
417
|
|
|
213
|
-
|
|
418
|
+
proto._shakeOutbound = function shakeOutbound(sourceMessage) {
|
|
419
|
+
const message = cloneMessage(sourceMessage);
|
|
420
|
+
message.content.sequence = message.content.sequence || [];
|
|
421
|
+
message.content.sequence.push({id: this.id, type: this.type});
|
|
214
422
|
|
|
215
|
-
|
|
423
|
+
const broker = this.broker;
|
|
424
|
+
this.broker.publish('api', 'activity.shake.start', message.content, {persistent: false, type: 'shake'});
|
|
216
425
|
|
|
217
|
-
|
|
218
|
-
broker.publish('
|
|
219
|
-
consumeRunQ();
|
|
426
|
+
if (this[kFlags].isEnd) {
|
|
427
|
+
return broker.publish('event', 'activity.shake.end', message.content, {persistent: false, type: 'shake'});
|
|
220
428
|
}
|
|
221
429
|
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
if (execution && !execution.completed) return execution.discard();
|
|
430
|
+
for (const flow of this[kFlows].outboundSequenceFlows) flow.shake(message);
|
|
431
|
+
};
|
|
225
432
|
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
433
|
+
proto._consumeInbound = function consumeInbound() {
|
|
434
|
+
if (this.status) return;
|
|
435
|
+
const inboundQ = this.broker.getQueue('inbound-q');
|
|
436
|
+
if (this[kFlags].isParallelJoin) {
|
|
437
|
+
return inboundQ.consume(this[kMessageHandlers].onInbound, {consumerTag: '_run-on-inbound', prefetch: 1000});
|
|
230
438
|
}
|
|
231
439
|
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
440
|
+
return inboundQ.consume(this[kMessageHandlers].onInbound, {consumerTag: '_run-on-inbound'});
|
|
441
|
+
};
|
|
442
|
+
|
|
443
|
+
proto._onInbound = function onInbound(routingKey, message) {
|
|
444
|
+
message.ack();
|
|
445
|
+
const id = this.id;
|
|
446
|
+
const broker = this.broker;
|
|
447
|
+
broker.cancel('_run-on-inbound');
|
|
448
|
+
|
|
449
|
+
const content = message.content;
|
|
450
|
+
const inbound = [cloneContent(content)];
|
|
451
|
+
|
|
452
|
+
switch (routingKey) {
|
|
453
|
+
case 'association.take':
|
|
454
|
+
case 'flow.take':
|
|
455
|
+
case 'activity.enter':
|
|
456
|
+
return this.run({
|
|
457
|
+
message: content.message,
|
|
458
|
+
inbound,
|
|
459
|
+
});
|
|
460
|
+
case 'flow.discard':
|
|
461
|
+
case 'activity.discard': {
|
|
462
|
+
let discardSequence;
|
|
463
|
+
if (content.discardSequence) discardSequence = content.discardSequence.slice();
|
|
464
|
+
return this._runDiscard({inbound, discardSequence});
|
|
241
465
|
}
|
|
466
|
+
case 'association.complete': {
|
|
467
|
+
broker.cancel('_run-on-inbound');
|
|
242
468
|
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
runQ.purge();
|
|
246
|
-
broker.publish('run', 'run.discard', cloneContent(stateMessage.content));
|
|
247
|
-
consumeRunQ();
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
function runDiscard(discardContent = {}) {
|
|
251
|
-
executionId = initExecutionId || getUniqueId(id);
|
|
469
|
+
const compensationId = `${brokerSafeId(id)}_${brokerSafeId(content.sequenceId)}`;
|
|
470
|
+
this.logger.debug(`<${id}> completed compensation with id <${compensationId}>`);
|
|
252
471
|
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
const content = createMessage({...discardContent, executionId});
|
|
258
|
-
broker.publish('run', 'run.discard', content);
|
|
259
|
-
|
|
260
|
-
consumeRunQ();
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
function stop() {
|
|
264
|
-
if (!consumingRunQ) return;
|
|
265
|
-
return getApi().stop();
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
function onStop(message) {
|
|
269
|
-
const running = consumingRunQ;
|
|
270
|
-
|
|
271
|
-
stopped = true;
|
|
272
|
-
|
|
273
|
-
consumingRunQ = false;
|
|
274
|
-
broker.cancel('_activity-run');
|
|
275
|
-
broker.cancel('_activity-api');
|
|
276
|
-
broker.cancel('_activity-execution');
|
|
277
|
-
broker.cancel('_run-on-inbound');
|
|
278
|
-
broker.cancel('_format-consumer');
|
|
279
|
-
|
|
280
|
-
if (running) {
|
|
281
|
-
if (extensions) extensions.deactivate(message || createMessage());
|
|
282
|
-
publishEvent('stop');
|
|
472
|
+
return this._publishEvent('compensation.end', this._createMessage({
|
|
473
|
+
executionId: compensationId,
|
|
474
|
+
}));
|
|
283
475
|
}
|
|
284
476
|
}
|
|
477
|
+
};
|
|
285
478
|
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
479
|
+
proto._onJoinInbound = function onJoinInbound(routingKey, message) {
|
|
480
|
+
const {content} = message;
|
|
481
|
+
const {inboundSequenceFlows, inboundJoinFlows, inboundTriggers} = this[kFlows];
|
|
482
|
+
const idx = inboundJoinFlows.findIndex((msg) => msg.content.id === content.id);
|
|
290
483
|
|
|
291
|
-
|
|
292
|
-
broker.cancel('_run-on-inbound');
|
|
293
|
-
broker.cancel('_format-consumer');
|
|
294
|
-
}
|
|
484
|
+
inboundJoinFlows.push(message);
|
|
295
485
|
|
|
296
|
-
|
|
297
|
-
if (consumingRunQ) return;
|
|
486
|
+
if (idx > -1) return;
|
|
298
487
|
|
|
299
|
-
|
|
300
|
-
|
|
488
|
+
const allTouched = inboundJoinFlows.length >= inboundTriggers.length;
|
|
489
|
+
if (!allTouched) {
|
|
490
|
+
const remaining = inboundSequenceFlows.filter((inb, i, list) => list.indexOf(inb) === i).length - inboundJoinFlows.length;
|
|
491
|
+
return this.logger.debug(`<${this.id}> inbound ${message.content.action} from <${message.content.id}>, ${remaining} remaining`);
|
|
301
492
|
}
|
|
302
493
|
|
|
303
|
-
|
|
304
|
-
if (!executionId) return;
|
|
494
|
+
const evaluatedInbound = inboundJoinFlows.splice(0);
|
|
305
495
|
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
496
|
+
let taken;
|
|
497
|
+
const inbound = evaluatedInbound.map((im) => {
|
|
498
|
+
if (im.fields.routingKey === 'flow.take') taken = true;
|
|
499
|
+
im.ack();
|
|
500
|
+
return cloneContent(im.content);
|
|
501
|
+
});
|
|
309
502
|
|
|
310
|
-
|
|
311
|
-
if (
|
|
312
|
-
|
|
313
|
-
|
|
503
|
+
const discardSequence = !taken && evaluatedInbound.reduce((result, im) => {
|
|
504
|
+
if (!im.content.discardSequence) return result;
|
|
505
|
+
for (const sourceId of im.content.discardSequence) {
|
|
506
|
+
if (result.indexOf(sourceId) === -1) result.push(sourceId);
|
|
314
507
|
}
|
|
508
|
+
return result;
|
|
509
|
+
}, []);
|
|
315
510
|
|
|
316
|
-
|
|
317
|
-
}
|
|
511
|
+
this.broker.cancel('_run-on-inbound');
|
|
318
512
|
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
broker.cancel('_activity-execution');
|
|
323
|
-
consumingRunQ = false;
|
|
324
|
-
}
|
|
513
|
+
if (!taken) return this._runDiscard({inbound, discardSequence});
|
|
514
|
+
return this.run({inbound});
|
|
515
|
+
};
|
|
325
516
|
|
|
326
|
-
|
|
327
|
-
|
|
517
|
+
proto._onInboundEvent = function onInboundEvent(routingKey, message) {
|
|
518
|
+
const {fields, content, properties} = message;
|
|
519
|
+
const id = this.id;
|
|
520
|
+
const inboundQ = this.broker.getQueue('inbound-q');
|
|
328
521
|
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
inboundQ.queueMessage(fields, cloneContent(content), properties);
|
|
334
|
-
}
|
|
335
|
-
break;
|
|
336
|
-
}
|
|
337
|
-
case 'flow.shake': {
|
|
338
|
-
shakeOutbound(message);
|
|
339
|
-
break;
|
|
340
|
-
}
|
|
341
|
-
case 'association.take':
|
|
342
|
-
case 'flow.take':
|
|
343
|
-
case 'flow.discard':
|
|
522
|
+
switch (routingKey) {
|
|
523
|
+
case 'activity.enter':
|
|
524
|
+
case 'activity.discard': {
|
|
525
|
+
if (content.id === this[kFlags].attachedTo) {
|
|
344
526
|
inboundQ.queueMessage(fields, cloneContent(content), properties);
|
|
345
|
-
break;
|
|
346
|
-
case 'association.discard': {
|
|
347
|
-
logger.debug(`<${id}> compensation discarded`);
|
|
348
|
-
inboundQ.purge();
|
|
349
|
-
break;
|
|
350
527
|
}
|
|
351
|
-
|
|
352
|
-
|
|
528
|
+
break;
|
|
529
|
+
}
|
|
530
|
+
case 'flow.shake': {
|
|
531
|
+
return this._shakeOutbound(message);
|
|
532
|
+
}
|
|
533
|
+
case 'association.take':
|
|
534
|
+
case 'flow.take':
|
|
535
|
+
case 'flow.discard':
|
|
536
|
+
return inboundQ.queueMessage(fields, cloneContent(content), properties);
|
|
537
|
+
case 'association.discard': {
|
|
538
|
+
this.logger.debug(`<${id}> compensation discarded`);
|
|
539
|
+
return inboundQ.purge();
|
|
540
|
+
}
|
|
541
|
+
case 'association.complete': {
|
|
542
|
+
if (!this[kFlags].isForCompensation) break;
|
|
353
543
|
|
|
354
|
-
|
|
544
|
+
inboundQ.queueMessage(fields, cloneContent(content), properties);
|
|
355
545
|
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
546
|
+
const compensationId = `${brokerSafeId(id)}_${brokerSafeId(content.sequenceId)}`;
|
|
547
|
+
this._publishEvent('compensation.start', this._createMessage({
|
|
548
|
+
executionId: compensationId,
|
|
549
|
+
placeholder: true,
|
|
550
|
+
}));
|
|
361
551
|
|
|
362
|
-
|
|
552
|
+
this.logger.debug(`<${id}> start compensation with id <${compensationId}>`);
|
|
363
553
|
|
|
364
|
-
|
|
365
|
-
break;
|
|
366
|
-
}
|
|
554
|
+
return this._consumeInbound();
|
|
367
555
|
}
|
|
368
556
|
}
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
case 'flow.discard':
|
|
387
|
-
case 'activity.discard': {
|
|
388
|
-
let discardSequence;
|
|
389
|
-
if (content.discardSequence) discardSequence = content.discardSequence.slice();
|
|
390
|
-
runDiscard({inbound, discardSequence});
|
|
391
|
-
break;
|
|
392
|
-
}
|
|
393
|
-
case 'association.complete': {
|
|
394
|
-
broker.cancel('_run-on-inbound');
|
|
395
|
-
|
|
396
|
-
const compensationId = `${brokerSafeId(id)}_${brokerSafeId(content.sequenceId)}`;
|
|
397
|
-
logger.debug(`<${id}> completed compensation with id <${compensationId}>`);
|
|
398
|
-
|
|
399
|
-
publishEvent('compensation.end', createMessage({
|
|
400
|
-
executionId: compensationId,
|
|
401
|
-
}));
|
|
402
|
-
break;
|
|
403
|
-
}
|
|
557
|
+
};
|
|
558
|
+
|
|
559
|
+
proto._consumeRunQ = function consumeRunQ() {
|
|
560
|
+
if (this[kConsuming]) return;
|
|
561
|
+
|
|
562
|
+
this[kConsuming] = true;
|
|
563
|
+
this.broker.getQueue('run-q').assertConsumer(this[kMessageHandlers].onRunMessage, {exclusive: true, consumerTag: '_activity-run'});
|
|
564
|
+
};
|
|
565
|
+
|
|
566
|
+
proto._onRunMessage = function onRunMessage(routingKey, message, messageProperties) {
|
|
567
|
+
switch (routingKey) {
|
|
568
|
+
case 'run.outbound.discard':
|
|
569
|
+
case 'run.outbound.take':
|
|
570
|
+
case 'run.next':
|
|
571
|
+
return this._continueRunMessage(routingKey, message, messageProperties);
|
|
572
|
+
case 'run.resume': {
|
|
573
|
+
return this._onResumeMessage(message);
|
|
404
574
|
}
|
|
405
575
|
}
|
|
406
576
|
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
577
|
+
const preStatus = this.status;
|
|
578
|
+
this.status = 'formatting';
|
|
579
|
+
return this.formatter.format(message, (err, formattedContent, formatted) => {
|
|
580
|
+
if (err) return this.emitFatal(err, message.content);
|
|
581
|
+
if (formatted) message.content = formattedContent;
|
|
582
|
+
this.status = preStatus;
|
|
583
|
+
this._continueRunMessage(routingKey, message, messageProperties);
|
|
584
|
+
});
|
|
585
|
+
};
|
|
410
586
|
|
|
411
|
-
|
|
587
|
+
proto._continueRunMessage = function continueRunMessage(routingKey, message) {
|
|
588
|
+
const isRedelivered = message.fields.redelivered;
|
|
589
|
+
const content = cloneContent(message.content);
|
|
590
|
+
const correlationId = message.properties.correlationId;
|
|
412
591
|
|
|
413
|
-
|
|
592
|
+
const id = this.id;
|
|
593
|
+
const step = this.environment.settings.step;
|
|
594
|
+
this[kStateMessage] = message;
|
|
414
595
|
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
return logger.debug(`<${id}> inbound ${message.content.action} from <${message.content.id}>, ${remaining} remaining`);
|
|
419
|
-
}
|
|
596
|
+
switch (routingKey) {
|
|
597
|
+
case 'run.enter': {
|
|
598
|
+
this.logger.debug(`<${id}> enter`, isRedelivered ? 'redelivered' : '');
|
|
420
599
|
|
|
421
|
-
|
|
600
|
+
this.status = 'entered';
|
|
601
|
+
if (!isRedelivered) {
|
|
602
|
+
this[kExec].execution = null;
|
|
603
|
+
}
|
|
422
604
|
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
if (im.fields.routingKey === 'flow.take') taken = true;
|
|
426
|
-
im.ack();
|
|
427
|
-
return cloneContent(im.content);
|
|
428
|
-
});
|
|
605
|
+
if (this.extensions) this.extensions.activate(cloneMessage(message), this);
|
|
606
|
+
if (this.bpmnIo) this.bpmnIo.activate(message);
|
|
429
607
|
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
});
|
|
435
|
-
return result;
|
|
436
|
-
}, []);
|
|
608
|
+
if (!isRedelivered) this._publishEvent('enter', content, {correlationId});
|
|
609
|
+
break;
|
|
610
|
+
}
|
|
611
|
+
case 'run.discard': {
|
|
612
|
+
this.logger.debug(`<${id}> discard`, isRedelivered ? 'redelivered' : '');
|
|
437
613
|
|
|
438
|
-
|
|
614
|
+
this.status = 'discard';
|
|
615
|
+
this[kExec].execution = null;
|
|
439
616
|
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
}
|
|
617
|
+
if (this.extensions) this.extensions.activate(cloneMessage(message), this);
|
|
618
|
+
if (this.bpmnIo) this.bpmnIo.activate(message);
|
|
443
619
|
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
case 'run.outbound.take':
|
|
448
|
-
case 'run.next':
|
|
449
|
-
return continueRunMessage(routingKey, message, messageProperties);
|
|
450
|
-
case 'run.resume': {
|
|
451
|
-
return onResumeMessage();
|
|
620
|
+
if (!isRedelivered) {
|
|
621
|
+
this.broker.publish('run', 'run.discarded', content, {correlationId});
|
|
622
|
+
this._publishEvent('discard', content);
|
|
452
623
|
}
|
|
624
|
+
break;
|
|
453
625
|
}
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
status = preStatus;
|
|
461
|
-
continueRunMessage(routingKey, message, messageProperties);
|
|
462
|
-
});
|
|
463
|
-
|
|
464
|
-
function onResumeMessage() {
|
|
465
|
-
message.ack();
|
|
466
|
-
|
|
467
|
-
const {fields} = stateMessage;
|
|
468
|
-
|
|
469
|
-
switch (fields.routingKey) {
|
|
470
|
-
case 'run.enter':
|
|
471
|
-
case 'run.start':
|
|
472
|
-
case 'run.discarded':
|
|
473
|
-
case 'run.end':
|
|
474
|
-
case 'run.leave':
|
|
475
|
-
break;
|
|
476
|
-
default:
|
|
477
|
-
return;
|
|
626
|
+
case 'run.start': {
|
|
627
|
+
this.logger.debug(`<${id}> start`, isRedelivered ? 'redelivered' : '');
|
|
628
|
+
this.status = 'started';
|
|
629
|
+
if (!isRedelivered) {
|
|
630
|
+
this.broker.publish('run', 'run.execute', content, {correlationId});
|
|
631
|
+
this._publishEvent('start', content, {correlationId});
|
|
478
632
|
}
|
|
479
633
|
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
logger.debug(`<${id}> resume from ${message.content.status}`);
|
|
483
|
-
|
|
484
|
-
return broker.publish('run', fields.routingKey, cloneContent(stateMessage.content), stateMessage.properties);
|
|
634
|
+
break;
|
|
485
635
|
}
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
if (!isRedelivered) publishEvent('enter', content, {correlationId});
|
|
509
|
-
break;
|
|
636
|
+
case 'run.execute.passthrough': {
|
|
637
|
+
const execution = this.execution;
|
|
638
|
+
if (!isRedelivered && execution) {
|
|
639
|
+
this[kExecuteMessage] = message;
|
|
640
|
+
return execution.passthrough(message);
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
case 'run.execute': {
|
|
644
|
+
this.status = 'executing';
|
|
645
|
+
this[kExecuteMessage] = message;
|
|
646
|
+
|
|
647
|
+
this.broker.getQueue('execution-q').assertConsumer(this[kMessageHandlers].onExecutionMessage, {exclusive: true, consumerTag: '_activity-execution'});
|
|
648
|
+
const exec = this[kExec];
|
|
649
|
+
if (!exec.execution) exec.execution = new ActivityExecution(this, this.context);
|
|
650
|
+
|
|
651
|
+
if (isRedelivered) {
|
|
652
|
+
return this._resumeExtensions(message, (err, formattedContent) => {
|
|
653
|
+
if (err) return this.emitFatal(err, message.content);
|
|
654
|
+
if (formattedContent) message.content = formattedContent;
|
|
655
|
+
this.status = 'executing';
|
|
656
|
+
return exec.execution.execute(message);
|
|
657
|
+
});
|
|
510
658
|
}
|
|
511
|
-
case 'run.discard': {
|
|
512
|
-
logger.debug(`<${id}> discard`, isRedelivered ? 'redelivered' : '');
|
|
513
|
-
|
|
514
|
-
status = 'discard';
|
|
515
|
-
execution = undefined;
|
|
516
659
|
|
|
517
|
-
|
|
518
|
-
|
|
660
|
+
return exec.execution.execute(message);
|
|
661
|
+
}
|
|
662
|
+
case 'run.end': {
|
|
663
|
+
if (this.status === 'end') break;
|
|
519
664
|
|
|
520
|
-
|
|
521
|
-
broker.publish('run', 'run.discarded', content, {correlationId});
|
|
522
|
-
publishEvent('discard', content);
|
|
523
|
-
}
|
|
524
|
-
break;
|
|
525
|
-
}
|
|
526
|
-
case 'run.start': {
|
|
527
|
-
logger.debug(`<${id}> start`, isRedelivered ? 'redelivered' : '');
|
|
528
|
-
status = 'started';
|
|
529
|
-
if (!isRedelivered) {
|
|
530
|
-
broker.publish('run', 'run.execute', content, {correlationId});
|
|
531
|
-
publishEvent('start', content, {correlationId});
|
|
532
|
-
}
|
|
533
|
-
|
|
534
|
-
break;
|
|
535
|
-
}
|
|
536
|
-
case 'run.execute.passthrough': {
|
|
537
|
-
if (!isRedelivered && execution) {
|
|
538
|
-
executeMessage = message;
|
|
539
|
-
return execution.passthrough(message);
|
|
540
|
-
}
|
|
541
|
-
}
|
|
542
|
-
case 'run.execute': {
|
|
543
|
-
status = 'executing';
|
|
544
|
-
executeMessage = message;
|
|
545
|
-
|
|
546
|
-
executionQ.assertConsumer(onExecutionMessage, {exclusive: true, consumerTag: '_activity-execution'});
|
|
547
|
-
execution = execution || ActivityExecution(activityApi, context);
|
|
548
|
-
|
|
549
|
-
if (isRedelivered) {
|
|
550
|
-
return resumeExtensions(message, (err, formattedContent) => {
|
|
551
|
-
if (err) return emitFatal(err, message.content);
|
|
552
|
-
if (formattedContent) message.content = formattedContent;
|
|
553
|
-
status = 'executing';
|
|
554
|
-
return execution.execute(message);
|
|
555
|
-
});
|
|
556
|
-
}
|
|
557
|
-
|
|
558
|
-
return execution.execute(message);
|
|
559
|
-
}
|
|
560
|
-
case 'run.end': {
|
|
561
|
-
if (status === 'end') break;
|
|
665
|
+
this[kCounters].taken++;
|
|
562
666
|
|
|
563
|
-
|
|
667
|
+
this.status = 'end';
|
|
564
668
|
|
|
565
|
-
|
|
669
|
+
return this._doRunLeave(message, false, () => {
|
|
670
|
+
this._publishEvent('end', content, {correlationId});
|
|
671
|
+
if (!step) message.ack();
|
|
672
|
+
});
|
|
673
|
+
}
|
|
674
|
+
case 'run.error': {
|
|
675
|
+
this._publishEvent('error', {
|
|
676
|
+
...content,
|
|
677
|
+
error: isRedelivered ? makeErrorFromMessage(message) : content.error,
|
|
678
|
+
}, {correlationId});
|
|
679
|
+
break;
|
|
680
|
+
}
|
|
681
|
+
case 'run.discarded': {
|
|
682
|
+
this.logger.debug(`<${content.executionId} (${id})> discarded`);
|
|
683
|
+
this[kCounters].discarded++;
|
|
566
684
|
|
|
567
|
-
|
|
685
|
+
this.status = 'discarded';
|
|
686
|
+
content.outbound = undefined;
|
|
568
687
|
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
if (!step) ack();
|
|
688
|
+
if (!isRedelivered) {
|
|
689
|
+
return this._doRunLeave(message, true, () => {
|
|
690
|
+
if (!step) message.ack();
|
|
572
691
|
});
|
|
573
692
|
}
|
|
574
|
-
case 'run.error': {
|
|
575
|
-
publishEvent('error', cloneContent(content, {
|
|
576
|
-
error: fields.redelivered ? makeErrorFromMessage(message) : content.error,
|
|
577
|
-
}), {correlationId});
|
|
578
|
-
break;
|
|
579
|
-
}
|
|
580
|
-
case 'run.discarded': {
|
|
581
|
-
logger.debug(`<${executionId} (${id})> discarded`);
|
|
582
|
-
counters.discarded++;
|
|
583
693
|
|
|
584
|
-
|
|
585
|
-
|
|
694
|
+
break;
|
|
695
|
+
}
|
|
696
|
+
case 'run.outbound.take': {
|
|
697
|
+
const flow = this._getOutboundSequenceFlowById(content.flow.id);
|
|
698
|
+
message.ack();
|
|
699
|
+
return flow.take(content.flow);
|
|
700
|
+
}
|
|
701
|
+
case 'run.outbound.discard': {
|
|
702
|
+
const flow = this._getOutboundSequenceFlowById(content.flow.id);
|
|
703
|
+
message.ack();
|
|
704
|
+
return flow.discard(content.flow);
|
|
705
|
+
}
|
|
706
|
+
case 'run.leave': {
|
|
707
|
+
this.status = undefined;
|
|
586
708
|
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
if (!step) ack();
|
|
590
|
-
});
|
|
591
|
-
}
|
|
709
|
+
if (this.bpmnIo) this.bpmnIo.deactivate(message);
|
|
710
|
+
if (this.extensions) this.extensions.deactivate(message);
|
|
592
711
|
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
const flow = getOutboundSequenceFlowById(content.flow.id);
|
|
597
|
-
ack();
|
|
598
|
-
return flow.take(content.flow);
|
|
712
|
+
if (!isRedelivered) {
|
|
713
|
+
this.broker.publish('run', 'run.next', content, {persistent: false});
|
|
714
|
+
this._publishEvent('leave', content, {correlationId});
|
|
599
715
|
}
|
|
600
|
-
case 'run.outbound.discard': {
|
|
601
|
-
const flow = getOutboundSequenceFlowById(content.flow.id);
|
|
602
|
-
ack();
|
|
603
|
-
return flow.discard(content.flow);
|
|
604
|
-
}
|
|
605
|
-
case 'run.leave': {
|
|
606
|
-
status = undefined;
|
|
607
|
-
|
|
608
|
-
if (bpmnIo) bpmnIo.deactivate(message);
|
|
609
|
-
if (extensions) extensions.deactivate(message);
|
|
610
|
-
|
|
611
|
-
if (!isRedelivered) {
|
|
612
|
-
broker.publish('run', 'run.next', cloneContent(content), {persistent: false});
|
|
613
|
-
publishEvent('leave', content, {correlationId});
|
|
614
|
-
}
|
|
615
716
|
|
|
616
|
-
|
|
617
|
-
}
|
|
618
|
-
case 'run.next':
|
|
619
|
-
consumeInbound();
|
|
620
|
-
break;
|
|
717
|
+
break;
|
|
621
718
|
}
|
|
719
|
+
case 'run.next':
|
|
720
|
+
this._consumeInbound();
|
|
721
|
+
break;
|
|
722
|
+
}
|
|
622
723
|
|
|
623
|
-
|
|
724
|
+
if (!step) message.ack();
|
|
725
|
+
};
|
|
624
726
|
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
727
|
+
proto._onExecutionMessage = function onExecutionMessage(routingKey, message) {
|
|
728
|
+
const executeMessage = this[kExecuteMessage];
|
|
729
|
+
const content = cloneContent({
|
|
730
|
+
...executeMessage.content,
|
|
731
|
+
...message.content,
|
|
732
|
+
executionId: executeMessage.content.executionId,
|
|
733
|
+
parent: {...this.parent},
|
|
734
|
+
});
|
|
631
735
|
|
|
632
|
-
|
|
633
|
-
if (err) {
|
|
634
|
-
return publishEvent('error', cloneContent(content, {error: err}), {correlationId});
|
|
635
|
-
}
|
|
736
|
+
const {correlationId} = message.properties;
|
|
636
737
|
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
}), {correlationId});
|
|
738
|
+
this._publishEvent(routingKey, content, message.properties);
|
|
739
|
+
const broker = this.broker;
|
|
640
740
|
|
|
641
|
-
|
|
741
|
+
switch (routingKey) {
|
|
742
|
+
case 'execution.outbound.take': {
|
|
743
|
+
return this._doOutbound(message, false, (err, outbound) => {
|
|
744
|
+
message.ack();
|
|
745
|
+
if (err) return this.emitFatal(err, content);
|
|
746
|
+
broker.publish('run', 'run.execute.passthrough', cloneContent(content, {outbound}));
|
|
747
|
+
return this._ackRunExecuteMessage();
|
|
642
748
|
});
|
|
643
749
|
}
|
|
750
|
+
case 'execution.error': {
|
|
751
|
+
this.status = 'error';
|
|
752
|
+
broker.publish('run', 'run.error', content, {correlationId});
|
|
753
|
+
broker.publish('run', 'run.discarded', content, {correlationId});
|
|
754
|
+
break;
|
|
755
|
+
}
|
|
756
|
+
case 'execution.discard':
|
|
757
|
+
this.status = 'discarded';
|
|
758
|
+
broker.publish('run', 'run.discarded', content, {correlationId});
|
|
759
|
+
break;
|
|
760
|
+
default: {
|
|
761
|
+
this.status = 'executed';
|
|
762
|
+
broker.publish('run', 'run.end', content, {correlationId});
|
|
763
|
+
}
|
|
644
764
|
}
|
|
645
765
|
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
766
|
+
message.ack();
|
|
767
|
+
this._ackRunExecuteMessage();
|
|
768
|
+
};
|
|
769
|
+
|
|
770
|
+
proto._ackRunExecuteMessage = function ackRunExecuteMessage() {
|
|
771
|
+
if (this.environment.settings.step) return;
|
|
772
|
+
const executeMessage = this[kExecuteMessage];
|
|
773
|
+
this[kExecuteMessage] = null;
|
|
774
|
+
executeMessage.ack();
|
|
775
|
+
};
|
|
776
|
+
|
|
777
|
+
proto._doRunLeave = function doRunLeave(message, isDiscarded, onOutbound) {
|
|
778
|
+
const {content, properties} = message;
|
|
779
|
+
const correlationId = properties.correlationId;
|
|
780
|
+
if (content.ignoreOutbound) {
|
|
781
|
+
this.broker.publish('run', 'run.leave', cloneContent(content), {correlationId});
|
|
782
|
+
return onOutbound();
|
|
661
783
|
}
|
|
662
784
|
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
...
|
|
666
|
-
|
|
667
|
-
executionId: executeMessage.content.executionId,
|
|
668
|
-
parent: {...parent},
|
|
669
|
-
});
|
|
785
|
+
return this._doOutbound(cloneMessage(message), isDiscarded, (err, outbound) => {
|
|
786
|
+
if (err) {
|
|
787
|
+
return this._publishEvent('error', {...content, error: err}, {correlationId});
|
|
788
|
+
}
|
|
670
789
|
|
|
671
|
-
|
|
790
|
+
this.broker.publish('run', 'run.leave', cloneContent(content, {
|
|
791
|
+
...(outbound.length ? {outbound} : undefined),
|
|
792
|
+
}), {correlationId});
|
|
672
793
|
|
|
673
|
-
|
|
794
|
+
onOutbound();
|
|
795
|
+
});
|
|
796
|
+
};
|
|
674
797
|
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
message.ack();
|
|
679
|
-
if (err) return emitFatal(err, content);
|
|
680
|
-
broker.publish('run', 'run.execute.passthrough', cloneContent(content, {outbound}));
|
|
681
|
-
return ackRunExecuteMessage();
|
|
682
|
-
});
|
|
683
|
-
}
|
|
684
|
-
case 'execution.error': {
|
|
685
|
-
status = 'error';
|
|
686
|
-
broker.publish('run', 'run.error', content, {correlationId});
|
|
687
|
-
broker.publish('run', 'run.discarded', content, {correlationId});
|
|
688
|
-
break;
|
|
689
|
-
}
|
|
690
|
-
case 'execution.discard':
|
|
691
|
-
status = 'discarded';
|
|
692
|
-
broker.publish('run', 'run.discarded', content, {correlationId});
|
|
693
|
-
break;
|
|
694
|
-
default: {
|
|
695
|
-
status = 'executed';
|
|
696
|
-
broker.publish('run', 'run.end', content, {correlationId});
|
|
697
|
-
}
|
|
698
|
-
}
|
|
798
|
+
proto._doOutbound = function doOutbound(fromMessage, isDiscarded, callback) {
|
|
799
|
+
const outboundSequenceFlows = this[kFlows].outboundSequenceFlows;
|
|
800
|
+
if (!outboundSequenceFlows.length) return callback(null, []);
|
|
699
801
|
|
|
700
|
-
|
|
701
|
-
ackRunExecuteMessage();
|
|
802
|
+
const fromContent = fromMessage.content;
|
|
702
803
|
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
804
|
+
let discardSequence = fromContent.discardSequence;
|
|
805
|
+
if (isDiscarded && !discardSequence && this[kFlags].attachedTo && fromContent.inbound && fromContent.inbound[0]) {
|
|
806
|
+
discardSequence = [fromContent.inbound[0].id];
|
|
807
|
+
}
|
|
706
808
|
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
809
|
+
let outboundFlows;
|
|
810
|
+
if (isDiscarded) {
|
|
811
|
+
outboundFlows = outboundSequenceFlows.map((flow) => formatFlowAction(flow, {action: 'discard'}));
|
|
812
|
+
} else if (fromContent.outbound && fromContent.outbound.length) {
|
|
813
|
+
outboundFlows = outboundSequenceFlows.map((flow) => formatFlowAction(flow, fromContent.outbound.filter((f) => f.id === flow.id).pop()));
|
|
711
814
|
}
|
|
712
815
|
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
case 'discard': {
|
|
717
|
-
discardRun(message);
|
|
718
|
-
break;
|
|
719
|
-
}
|
|
720
|
-
case 'stop': {
|
|
721
|
-
onStop(message);
|
|
722
|
-
break;
|
|
723
|
-
}
|
|
724
|
-
case 'shake': {
|
|
725
|
-
shakeOutbound(message);
|
|
726
|
-
break;
|
|
727
|
-
}
|
|
728
|
-
}
|
|
816
|
+
if (outboundFlows) {
|
|
817
|
+
this._doRunOutbound(outboundFlows, fromContent, discardSequence);
|
|
818
|
+
return callback(null, outboundFlows);
|
|
729
819
|
}
|
|
730
820
|
|
|
731
|
-
|
|
732
|
-
|
|
821
|
+
return this.evaluateOutbound(fromMessage, fromContent.outboundTakeOne, (err, evaluatedOutbound) => {
|
|
822
|
+
if (err) return callback(new ActivityError(err.message, fromMessage, err));
|
|
823
|
+
const outbound = this._doRunOutbound(evaluatedOutbound, fromContent, discardSequence);
|
|
824
|
+
return callback(null, outbound);
|
|
825
|
+
});
|
|
826
|
+
};
|
|
827
|
+
|
|
828
|
+
proto._doRunOutbound = function doRunOutbound(outboundList, content, discardSequence) {
|
|
829
|
+
for (const outboundFlow of outboundList) {
|
|
830
|
+
const {id: flowId, action} = outboundFlow;
|
|
831
|
+
this.broker.publish('run', 'run.outbound.' + action, cloneContent(content, {
|
|
832
|
+
flow: {
|
|
833
|
+
...outboundFlow,
|
|
834
|
+
sequenceId: getUniqueId(`${flowId}_${action}`),
|
|
835
|
+
...(discardSequence ? {discardSequence: discardSequence.slice()} : undefined),
|
|
836
|
+
},
|
|
837
|
+
}));
|
|
838
|
+
}
|
|
839
|
+
return outboundList;
|
|
840
|
+
};
|
|
841
|
+
|
|
842
|
+
proto._onResumeMessage = function onResumeMessage(message) {
|
|
843
|
+
message.ack();
|
|
844
|
+
|
|
845
|
+
const stateMessage = this[kStateMessage];
|
|
846
|
+
const {fields} = stateMessage;
|
|
847
|
+
|
|
848
|
+
switch (fields.routingKey) {
|
|
849
|
+
case 'run.enter':
|
|
850
|
+
case 'run.start':
|
|
851
|
+
case 'run.discarded':
|
|
852
|
+
case 'run.end':
|
|
853
|
+
case 'run.leave':
|
|
854
|
+
break;
|
|
855
|
+
default:
|
|
856
|
+
return;
|
|
733
857
|
}
|
|
734
858
|
|
|
735
|
-
|
|
736
|
-
const message = cloneMessage(sourceMessage);
|
|
737
|
-
message.content.sequence = message.content.sequence || [];
|
|
738
|
-
message.content.sequence.push({id, type});
|
|
859
|
+
if (!fields.redelivered) return;
|
|
739
860
|
|
|
740
|
-
|
|
861
|
+
this.logger.debug(`<${this.id}> resume from ${message.content.status}`);
|
|
741
862
|
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
}
|
|
863
|
+
return this.broker.publish('run', fields.routingKey, cloneContent(stateMessage.content), stateMessage.properties);
|
|
864
|
+
};
|
|
745
865
|
|
|
746
|
-
|
|
747
|
-
}
|
|
866
|
+
proto._publishEvent = function publishEvent(state, content, properties = {}) {
|
|
867
|
+
this.broker.publish('event', `activity.${state}`, cloneContent(content, {state}), {
|
|
868
|
+
...properties,
|
|
869
|
+
type: state,
|
|
870
|
+
mandatory: state === 'error',
|
|
871
|
+
persistent: 'persistent' in properties ? properties.persistent : state !== 'stop',
|
|
872
|
+
});
|
|
873
|
+
};
|
|
748
874
|
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
if (!content) content = createMessage();
|
|
752
|
-
broker.publish('event', `activity.${state}`, {...content, state}, {
|
|
753
|
-
...messageProperties,
|
|
754
|
-
type: state,
|
|
755
|
-
mandatory: state === 'error',
|
|
756
|
-
persistent: 'persistent' in messageProperties ? messageProperties.persistent : state !== 'stop',
|
|
757
|
-
});
|
|
758
|
-
}
|
|
875
|
+
proto._onStop = function onStop(message) {
|
|
876
|
+
const running = this[kConsuming];
|
|
759
877
|
|
|
760
|
-
|
|
761
|
-
if (!outboundSequenceFlows.length) return callback(null, []);
|
|
878
|
+
this.stopped = true;
|
|
762
879
|
|
|
763
|
-
|
|
880
|
+
this[kConsuming] = false;
|
|
881
|
+
const broker = this.broker;
|
|
882
|
+
broker.cancel('_activity-run');
|
|
883
|
+
broker.cancel('_activity-api');
|
|
884
|
+
broker.cancel('_activity-execution');
|
|
885
|
+
broker.cancel('_run-on-inbound');
|
|
886
|
+
broker.cancel('_format-consumer');
|
|
764
887
|
|
|
765
|
-
|
|
766
|
-
if (
|
|
767
|
-
|
|
888
|
+
if (running) {
|
|
889
|
+
if (this.extensions) this.extensions.deactivate(message || this._createMessage());
|
|
890
|
+
this._publishEvent('stop', this._createMessage());
|
|
891
|
+
}
|
|
892
|
+
};
|
|
893
|
+
|
|
894
|
+
proto._consumeApi = function consumeApi() {
|
|
895
|
+
const executionId = this[kExec].executionId;
|
|
896
|
+
if (!executionId) return;
|
|
897
|
+
const broker = this.broker;
|
|
898
|
+
broker.cancel('_activity-api');
|
|
899
|
+
broker.subscribeTmp('api', `activity.*.${executionId}`, this[kMessageHandlers].onApiMessage, {noAck: true, consumerTag: '_activity-api', priority: 100});
|
|
900
|
+
};
|
|
901
|
+
|
|
902
|
+
proto._onApiMessage = function onApiMessage(routingKey, message) {
|
|
903
|
+
switch (message.properties.type) {
|
|
904
|
+
case 'discard': {
|
|
905
|
+
return this._discardRun(message);
|
|
768
906
|
}
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
if (isDiscarded) {
|
|
772
|
-
outboundFlows = outboundSequenceFlows.map((flow) => formatFlowAction(flow, {action: 'discard'}));
|
|
773
|
-
} else if (fromContent.outbound && fromContent.outbound.length) {
|
|
774
|
-
outboundFlows = outboundSequenceFlows.map((flow) => formatFlowAction(flow, fromContent.outbound.filter((f) => f.id === flow.id).pop()));
|
|
907
|
+
case 'stop': {
|
|
908
|
+
return this._onStop(message);
|
|
775
909
|
}
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
doRunOutbound(outboundFlows);
|
|
779
|
-
return callback(null, outboundFlows);
|
|
910
|
+
case 'shake': {
|
|
911
|
+
return this._shakeOutbound(message);
|
|
780
912
|
}
|
|
913
|
+
}
|
|
914
|
+
};
|
|
915
|
+
|
|
916
|
+
proto._createMessage = function createMessage(override) {
|
|
917
|
+
const name = this.name, status = this.status, parent = this.parent;
|
|
918
|
+
const result = {
|
|
919
|
+
...override,
|
|
920
|
+
id: this.id,
|
|
921
|
+
type: this.type,
|
|
922
|
+
...(name ? {name} : undefined),
|
|
923
|
+
...(status ? {status} : undefined),
|
|
924
|
+
...(parent ? {parent: cloneParent(parent)} : undefined),
|
|
925
|
+
};
|
|
781
926
|
|
|
782
|
-
|
|
783
|
-
|
|
927
|
+
for (const [flag, value] of Object.entries(this[kFlags])) {
|
|
928
|
+
if (value) result[flag] = value;
|
|
929
|
+
}
|
|
784
930
|
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
});
|
|
931
|
+
return result;
|
|
932
|
+
};
|
|
788
933
|
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
broker.publish('run', 'run.outbound.' + action, cloneContent(fromContent, {
|
|
793
|
-
flow: {
|
|
794
|
-
...outboundFlow,
|
|
795
|
-
sequenceId: getUniqueId(`${flowId}_${action}`),
|
|
796
|
-
...(discardSequence ? {discardSequence: discardSequence.slice()} : undefined),
|
|
797
|
-
},
|
|
798
|
-
}));
|
|
799
|
-
|
|
800
|
-
return outboundFlow;
|
|
801
|
-
});
|
|
802
|
-
}
|
|
803
|
-
}
|
|
934
|
+
proto._getOutboundSequenceFlowById = function getOutboundSequenceFlowById(flowId) {
|
|
935
|
+
return this[kFlows].outboundSequenceFlows.find((flow) => flow.id === flowId);
|
|
936
|
+
};
|
|
804
937
|
|
|
805
|
-
|
|
806
|
-
|
|
938
|
+
proto._resumeExtensions = function resumeExtensions(message, callback) {
|
|
939
|
+
const extensions = this.extensions, bpmnIo = this.bpmnIo;
|
|
940
|
+
if (!extensions && !bpmnIo) return callback();
|
|
807
941
|
|
|
808
|
-
|
|
809
|
-
|
|
942
|
+
if (extensions) extensions.activate(cloneMessage(message), this);
|
|
943
|
+
if (bpmnIo) bpmnIo.activate(cloneMessage(message), this);
|
|
810
944
|
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
945
|
+
this.status = 'formatting';
|
|
946
|
+
return this.formatter.format(message, (err, formattedContent, formatted) => {
|
|
947
|
+
if (err) return callback(err);
|
|
948
|
+
return callback(null, formatted && formattedContent);
|
|
949
|
+
});
|
|
950
|
+
};
|
|
951
|
+
|
|
952
|
+
proto._deactivateRunConsumers = function _deactivateRunConsumers() {
|
|
953
|
+
const broker = this.broker;
|
|
954
|
+
broker.cancel('_activity-api');
|
|
955
|
+
broker.cancel('_activity-run');
|
|
956
|
+
broker.cancel('_activity-execution');
|
|
957
|
+
this[kConsuming] = false;
|
|
958
|
+
};
|
|
959
|
+
|
|
960
|
+
function OutboundEvaluator(activity, outboundFlows) {
|
|
961
|
+
this.activity = activity;
|
|
962
|
+
this.broker = activity.broker;
|
|
963
|
+
const flows = this.outboundFlows = outboundFlows.slice();
|
|
964
|
+
const defaultFlowIdx = flows.findIndex(({isDefault}) => isDefault);
|
|
965
|
+
if (defaultFlowIdx > -1) {
|
|
966
|
+
const [defaultFlow] = flows.splice(defaultFlowIdx, 1);
|
|
967
|
+
flows.push(defaultFlow);
|
|
818
968
|
}
|
|
819
969
|
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
if (!outboundSequenceFlows.length) return completed();
|
|
970
|
+
this.defaultFlowIdx = outboundFlows.findIndex(({isDefault}) => isDefault);
|
|
971
|
+
this._onEvaluated = this.onEvaluated.bind(this);
|
|
972
|
+
this.evaluateArgs = {};
|
|
973
|
+
}
|
|
825
974
|
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
975
|
+
OutboundEvaluator.prototype.evaluate = function evaluate(fromMessage, discardRestAtTake, callback) {
|
|
976
|
+
const outboundFlows = this.outboundFlows;
|
|
977
|
+
|
|
978
|
+
const args = this.evaluateArgs = {
|
|
979
|
+
fromMessage,
|
|
980
|
+
evaluationId: fromMessage.content.executionId,
|
|
981
|
+
discardRestAtTake,
|
|
982
|
+
callback,
|
|
983
|
+
conditionMet: false,
|
|
984
|
+
result: {},
|
|
985
|
+
takenCount: 0,
|
|
986
|
+
};
|
|
835
987
|
|
|
836
|
-
|
|
837
|
-
const {id: flowId, action} = evalContent;
|
|
988
|
+
if (!outboundFlows.length) return this.completed();
|
|
838
989
|
|
|
839
|
-
|
|
840
|
-
takenCount++;
|
|
841
|
-
conditionMet = true;
|
|
842
|
-
}
|
|
990
|
+
const flows = args.flows = outboundFlows.slice();
|
|
843
991
|
|
|
844
|
-
|
|
992
|
+
this.broker.subscribeTmp('execution', 'evaluate.flow.#', this._onEvaluated, {
|
|
993
|
+
consumerTag: `_flow-evaluation-${args.evaluationId}`,
|
|
994
|
+
});
|
|
845
995
|
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
}
|
|
996
|
+
return this.evaluateFlow(flows.shift());
|
|
997
|
+
};
|
|
849
998
|
|
|
850
|
-
|
|
851
|
-
|
|
999
|
+
OutboundEvaluator.prototype.onEvaluated = function onEvaluated(routingKey, message) {
|
|
1000
|
+
const content = message.content;
|
|
1001
|
+
const {id: flowId, action, evaluationId} = message.content;
|
|
1002
|
+
const args = this.evaluateArgs;
|
|
852
1003
|
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
return completed();
|
|
858
|
-
}
|
|
1004
|
+
if (action === 'take') {
|
|
1005
|
+
args.takenCount++;
|
|
1006
|
+
args.conditionMet = true;
|
|
1007
|
+
}
|
|
859
1008
|
|
|
860
|
-
|
|
861
|
-
outbound[nextFlow.id] = formatFlowAction(nextFlow, {action: 'discard'});
|
|
862
|
-
return completed();
|
|
863
|
-
}
|
|
1009
|
+
args.result[flowId] = content;
|
|
864
1010
|
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
1011
|
+
if ('result' in content) {
|
|
1012
|
+
this.activity.logger.debug(`<${evaluationId} (${this.activity.id})> flow <${flowId}> evaluated to: ${!!content.result}`);
|
|
1013
|
+
}
|
|
868
1014
|
|
|
869
|
-
|
|
1015
|
+
let nextFlow = args.flows.shift();
|
|
1016
|
+
if (!nextFlow) return this.completed();
|
|
870
1017
|
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
1018
|
+
if (args.discardRestAtTake && args.conditionMet) {
|
|
1019
|
+
do {
|
|
1020
|
+
args.result[nextFlow.id] = formatFlowAction(nextFlow, {action: 'discard'});
|
|
1021
|
+
} while ((nextFlow = args.flows.shift()));
|
|
1022
|
+
return this.completed();
|
|
1023
|
+
}
|
|
874
1024
|
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
}
|
|
1025
|
+
if (args.conditionMet && nextFlow.isDefault) {
|
|
1026
|
+
args.result[nextFlow.id] = formatFlowAction(nextFlow, {action: 'discard'});
|
|
1027
|
+
return this.completed();
|
|
1028
|
+
}
|
|
880
1029
|
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
...flow,
|
|
885
|
-
...(message !== undefined ? {message} : undefined),
|
|
886
|
-
});
|
|
887
|
-
return result;
|
|
888
|
-
}, []);
|
|
1030
|
+
message.ack();
|
|
1031
|
+
this.evaluateFlow(nextFlow);
|
|
1032
|
+
};
|
|
889
1033
|
|
|
890
|
-
|
|
891
|
-
|
|
1034
|
+
OutboundEvaluator.prototype.evaluateFlow = function evaluateFlow(flow) {
|
|
1035
|
+
const broker = this.broker;
|
|
1036
|
+
if (flow.isDefault) {
|
|
1037
|
+
return broker.publish('execution', 'evaluate.flow.take', formatFlowAction(flow, {action: 'take'}), {persistent: false});
|
|
1038
|
+
}
|
|
892
1039
|
|
|
893
|
-
|
|
894
|
-
|
|
1040
|
+
const flowCondition = flow.getCondition();
|
|
1041
|
+
if (!flowCondition) {
|
|
1042
|
+
return broker.publish('execution', 'evaluate.flow.take', formatFlowAction(flow, {action: 'take'}), {persistent: false});
|
|
1043
|
+
}
|
|
895
1044
|
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
1045
|
+
const {fromMessage, evaluationId} = this.evaluateArgs;
|
|
1046
|
+
flowCondition.execute(cloneMessage(fromMessage), (err, result) => {
|
|
1047
|
+
if (err) return this.completed(err);
|
|
1048
|
+
const action = result ? 'take' : 'discard';
|
|
1049
|
+
return broker.publish('execution', 'evaluate.flow.' + action, formatFlowAction(flow, {
|
|
1050
|
+
action,
|
|
1051
|
+
result,
|
|
1052
|
+
evaluationId,
|
|
1053
|
+
}), {persistent: false});
|
|
1054
|
+
});
|
|
1055
|
+
};
|
|
899
1056
|
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
}
|
|
1057
|
+
OutboundEvaluator.prototype.completed = function completed(err) {
|
|
1058
|
+
const {callback, evaluationId, fromMessage, result, takenCount} = this.evaluateArgs;
|
|
1059
|
+
this.broker.cancel(`_flow-evaluation-${evaluationId}`);
|
|
904
1060
|
|
|
905
|
-
|
|
906
|
-
if (err) return completed(err);
|
|
907
|
-
const action = result ? 'take' : 'discard';
|
|
908
|
-
return broker.publish('execution', 'evaluate.flow.' + action, formatFlowAction(flow, {
|
|
909
|
-
action,
|
|
910
|
-
result,
|
|
911
|
-
}), {persistent: false});
|
|
912
|
-
});
|
|
913
|
-
}
|
|
914
|
-
}
|
|
1061
|
+
if (err) return callback(err);
|
|
915
1062
|
|
|
916
|
-
|
|
917
|
-
|
|
1063
|
+
if (!takenCount && this.outboundFlows.length) {
|
|
1064
|
+
const nonTakenError = new ActivityError(`<${this.activity.id}> no conditional flow taken`, fromMessage);
|
|
1065
|
+
return callback(nonTakenError);
|
|
918
1066
|
}
|
|
919
1067
|
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
...
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
stopped,
|
|
928
|
-
behaviour: {...behaviour},
|
|
929
|
-
counters: {...counters},
|
|
930
|
-
broker: broker.getState(true),
|
|
931
|
-
execution: execution && execution.getState(),
|
|
932
|
-
};
|
|
1068
|
+
const message = fromMessage.content.message;
|
|
1069
|
+
const evaluationResult = [];
|
|
1070
|
+
for (const flow of Object.values(result)) {
|
|
1071
|
+
evaluationResult.push({
|
|
1072
|
+
...flow,
|
|
1073
|
+
...(message !== undefined ? {message} : undefined),
|
|
1074
|
+
});
|
|
933
1075
|
}
|
|
934
1076
|
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
if (!stateMessage) return;
|
|
938
|
-
if (status === 'executing') return false;
|
|
939
|
-
if (status === 'formatting') return false;
|
|
940
|
-
const current = stateMessage;
|
|
941
|
-
stateMessage.ack();
|
|
942
|
-
return current;
|
|
943
|
-
}
|
|
1077
|
+
return callback(null, evaluationResult);
|
|
1078
|
+
};
|
|
944
1079
|
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
1080
|
+
function formatFlowAction(flow, options) {
|
|
1081
|
+
return {
|
|
1082
|
+
...options,
|
|
1083
|
+
id: flow.id,
|
|
1084
|
+
action: options.action,
|
|
1085
|
+
...(flow.isDefault ? {isDefault: true} : undefined),
|
|
1086
|
+
};
|
|
949
1087
|
}
|