arvo-event-handler 3.0.6 → 3.0.8
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/dist/ArvoEventHandler/helpers.d.ts +2 -2
- package/dist/ArvoEventHandler/index.d.ts +4 -4
- package/dist/ArvoEventHandler/index.js +13 -34
- package/dist/ArvoEventHandler/types.d.ts +5 -3
- package/dist/ArvoMachine/createMachine.d.ts +4 -2
- package/dist/ArvoMachine/createMachine.js +1 -1
- package/dist/ArvoMachine/types.d.ts +5 -1
- package/dist/ArvoOrchestrationUtils/createEmitableEvent.d.ts +30 -0
- package/dist/ArvoOrchestrationUtils/createEmitableEvent.js +160 -0
- package/dist/ArvoOrchestrationUtils/error.d.ts +18 -0
- package/dist/{ArvoOrchestrator → ArvoOrchestrationUtils}/error.js +14 -9
- package/dist/ArvoOrchestrationUtils/handlerErrors.d.ts +35 -0
- package/dist/ArvoOrchestrationUtils/handlerErrors.js +184 -0
- package/dist/ArvoOrchestrationUtils/orchestrationExecutionState.d.ts +11 -0
- package/dist/ArvoOrchestrationUtils/orchestrationExecutionState.js +7 -0
- package/dist/ArvoOrchestrationUtils/orchestrationExecutionWrapper/acquireLockWithValidation.d.ts +8 -0
- package/dist/ArvoOrchestrationUtils/orchestrationExecutionWrapper/acquireLockWithValidation.js +69 -0
- package/dist/ArvoOrchestrationUtils/orchestrationExecutionWrapper/index.d.ts +40 -0
- package/dist/ArvoOrchestrationUtils/orchestrationExecutionWrapper/index.js +228 -0
- package/dist/ArvoOrchestrationUtils/orchestrationExecutionWrapper/validateAndParseSubject.d.ts +7 -0
- package/dist/ArvoOrchestrationUtils/orchestrationExecutionWrapper/validateAndParseSubject.js +25 -0
- package/dist/ArvoOrchestrationUtils/types.d.ts +1 -0
- package/dist/ArvoOrchestrationUtils/types.js +2 -0
- package/dist/ArvoOrchestrator/factory.js +13 -0
- package/dist/ArvoOrchestrator/index.d.ts +5 -21
- package/dist/ArvoOrchestrator/index.js +93 -505
- package/dist/ArvoOrchestrator/types.d.ts +5 -4
- package/dist/ArvoResumable/factory.d.ts +2 -2
- package/dist/ArvoResumable/factory.js +1 -1
- package/dist/ArvoResumable/index.d.ts +6 -20
- package/dist/ArvoResumable/index.js +85 -462
- package/dist/ArvoResumable/types.d.ts +8 -5
- package/dist/IArvoEventHandler/index.d.ts +36 -0
- package/dist/IArvoEventHandler/index.js +2 -0
- package/dist/MachineExecutionEngine/index.d.ts +1 -1
- package/dist/MachineRegistry/index.d.ts +1 -1
- package/dist/SyncEventResource/index.d.ts +1 -1
- package/dist/SyncEventResource/index.js +1 -1
- package/dist/index.d.ts +14 -13
- package/dist/index.js +24 -23
- package/dist/types.d.ts +2 -2
- package/dist/utils/SimpleEventBroker/helper.d.ts +2 -2
- package/package.json +5 -4
- package/dist/AbstractArvoEventHandler/index.d.ts +0 -91
- package/dist/AbstractArvoEventHandler/index.js +0 -18
- package/dist/ArvoOrchestrator/error.d.ts +0 -16
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
|
3
|
+
__assign = Object.assign || function(t) {
|
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
+
s = arguments[i];
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
};
|
|
11
|
+
return __assign.apply(this, arguments);
|
|
12
|
+
};
|
|
13
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
14
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
15
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
16
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
17
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
18
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
19
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
23
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
24
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
25
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
26
|
+
function step(op) {
|
|
27
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
28
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
29
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
30
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
31
|
+
switch (op[0]) {
|
|
32
|
+
case 0: case 1: t = op; break;
|
|
33
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
34
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
35
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
36
|
+
default:
|
|
37
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
38
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
39
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
40
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
41
|
+
if (t[2]) _.ops.pop();
|
|
42
|
+
_.trys.pop(); continue;
|
|
43
|
+
}
|
|
44
|
+
op = body.call(thisArg, _);
|
|
45
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
46
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
50
|
+
exports.handleOrchestrationErrors = exports.createSystemErrorEvents = void 0;
|
|
51
|
+
var api_1 = require("@opentelemetry/api");
|
|
52
|
+
var arvo_core_1 = require("arvo-core");
|
|
53
|
+
var ArvoDomain_1 = require("../ArvoDomain");
|
|
54
|
+
var errors_1 = require("../errors");
|
|
55
|
+
var utils_1 = require("../utils");
|
|
56
|
+
/**
|
|
57
|
+
* Creates system error events
|
|
58
|
+
*/
|
|
59
|
+
var createSystemErrorEvents = function (_a) {
|
|
60
|
+
var _b, _c, _d, _e;
|
|
61
|
+
var error = _a.error, event = _a.event, otelHeaders = _a.otelHeaders, orchestrationParentSubject = _a.orchestrationParentSubject, initEventId = _a.initEventId, selfContract = _a.selfContract, systemErrorDomain = _a.systemErrorDomain, executionunits = _a.executionunits, source = _a.source, domain = _a.domain;
|
|
62
|
+
// In case of none transaction errors like errors from
|
|
63
|
+
// the machine or the event creation etc, the are workflow
|
|
64
|
+
// error and shuold be handled by the workflow. Then are
|
|
65
|
+
// called system error and must be sent
|
|
66
|
+
// to the initiator. In as good of a format as possible
|
|
67
|
+
var parsedEventSubject = null;
|
|
68
|
+
try {
|
|
69
|
+
parsedEventSubject = arvo_core_1.ArvoOrchestrationSubject.parse(event.subject);
|
|
70
|
+
}
|
|
71
|
+
catch (e) {
|
|
72
|
+
(0, arvo_core_1.logToSpan)({
|
|
73
|
+
level: 'WARNING',
|
|
74
|
+
message: "Unable to parse event subject: ".concat(e.message),
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
var domainSets = new Set(systemErrorDomain
|
|
78
|
+
? systemErrorDomain.map(function (item) {
|
|
79
|
+
return (0, ArvoDomain_1.resolveEventDomain)({
|
|
80
|
+
domainToResolve: item,
|
|
81
|
+
handlerSelfContract: selfContract,
|
|
82
|
+
eventContract: selfContract,
|
|
83
|
+
triggeringEvent: event,
|
|
84
|
+
});
|
|
85
|
+
})
|
|
86
|
+
: [event.domain, domain, null]);
|
|
87
|
+
var result = [];
|
|
88
|
+
for (var _i = 0, _f = Array.from(domainSets); _i < _f.length; _i++) {
|
|
89
|
+
var _dom = _f[_i];
|
|
90
|
+
result.push((0, arvo_core_1.createArvoOrchestratorEventFactory)(selfContract).systemError({
|
|
91
|
+
source: source,
|
|
92
|
+
// If the initiator of the workflow exist then match the
|
|
93
|
+
// subject so that it can incorporate it in its state. If
|
|
94
|
+
// parent does not exist then this is the root workflow so
|
|
95
|
+
// use its own subject
|
|
96
|
+
subject: orchestrationParentSubject !== null && orchestrationParentSubject !== void 0 ? orchestrationParentSubject : event.subject,
|
|
97
|
+
// The system error must always go back to
|
|
98
|
+
// the source which initiated it
|
|
99
|
+
to: (_b = parsedEventSubject === null || parsedEventSubject === void 0 ? void 0 : parsedEventSubject.execution.initiator) !== null && _b !== void 0 ? _b : event.source,
|
|
100
|
+
error: error,
|
|
101
|
+
traceparent: (_c = otelHeaders.traceparent) !== null && _c !== void 0 ? _c : undefined,
|
|
102
|
+
tracestate: (_d = otelHeaders.tracestate) !== null && _d !== void 0 ? _d : undefined,
|
|
103
|
+
accesscontrol: (_e = event.accesscontrol) !== null && _e !== void 0 ? _e : undefined,
|
|
104
|
+
executionunits: executionunits,
|
|
105
|
+
// If there is initEventID then use that.
|
|
106
|
+
// Otherwise, use event id. If the error is in init event
|
|
107
|
+
// then it will be the same as initEventId. Otherwise,
|
|
108
|
+
// we still would know what cause this error
|
|
109
|
+
parentid: initEventId !== null && initEventId !== void 0 ? initEventId : event.id,
|
|
110
|
+
domain: _dom,
|
|
111
|
+
}));
|
|
112
|
+
}
|
|
113
|
+
return result;
|
|
114
|
+
};
|
|
115
|
+
exports.createSystemErrorEvents = createSystemErrorEvents;
|
|
116
|
+
var handleOrchestrationErrors = function (_handlerType, param, span) { return __awaiter(void 0, void 0, void 0, function () {
|
|
117
|
+
var handlerType, error, errorEvents, _i, _a, _b, errEvtIdx, errEvt, _c, _d, _e, key, value;
|
|
118
|
+
return __generator(this, function (_f) {
|
|
119
|
+
switch (_f.label) {
|
|
120
|
+
case 0:
|
|
121
|
+
handlerType = {
|
|
122
|
+
orchestrator: 'ArvoOrchestrator',
|
|
123
|
+
resumable: 'ArvoResumable',
|
|
124
|
+
}[_handlerType];
|
|
125
|
+
error = (0, utils_1.isError)(param.error)
|
|
126
|
+
? param.error
|
|
127
|
+
: new errors_1.ExecutionViolation("Non-Error object thrown during machine execution: ".concat(typeof param.error, ". This indicates a serious implementation flaw."));
|
|
128
|
+
(0, arvo_core_1.exceptionToSpan)(error, span);
|
|
129
|
+
span.setStatus({
|
|
130
|
+
code: api_1.SpanStatusCode.ERROR,
|
|
131
|
+
message: error.message,
|
|
132
|
+
});
|
|
133
|
+
// Don't persist state on a violation
|
|
134
|
+
//
|
|
135
|
+
// A violation means that there is something
|
|
136
|
+
// wrong in the state persitance or it is a
|
|
137
|
+
// error which will be handled outside the
|
|
138
|
+
// Arvo mechanism. So, it makes sense that it
|
|
139
|
+
// does not impact the state. The violations
|
|
140
|
+
// can be used to trigger retries as well
|
|
141
|
+
if ((0, arvo_core_1.isViolationError)(error)) {
|
|
142
|
+
(0, arvo_core_1.logToSpan)({
|
|
143
|
+
level: 'CRITICAL',
|
|
144
|
+
message: "".concat(handlerType || 'Arvo orchestration handler', " violation error: ").concat(error.message),
|
|
145
|
+
});
|
|
146
|
+
return [2 /*return*/, {
|
|
147
|
+
errorToThrow: error,
|
|
148
|
+
events: null,
|
|
149
|
+
}];
|
|
150
|
+
}
|
|
151
|
+
return [4 /*yield*/, param.syncEventResource
|
|
152
|
+
.persistState(param.event, {
|
|
153
|
+
executionStatus: 'failure',
|
|
154
|
+
subject: param.event.subject,
|
|
155
|
+
error: param.error,
|
|
156
|
+
}, null, span)
|
|
157
|
+
.catch(function (e) {
|
|
158
|
+
(0, arvo_core_1.logToSpan)({
|
|
159
|
+
level: 'CRITICAL',
|
|
160
|
+
message: "Error in orchestrator persisting the failure state: ".concat(e.message),
|
|
161
|
+
});
|
|
162
|
+
})];
|
|
163
|
+
case 1:
|
|
164
|
+
_f.sent();
|
|
165
|
+
(0, arvo_core_1.logToSpan)({
|
|
166
|
+
level: 'ERROR',
|
|
167
|
+
message: "".concat(handlerType || 'Arvo orchestration handler', " execution failed: ").concat(error.message),
|
|
168
|
+
});
|
|
169
|
+
errorEvents = (0, exports.createSystemErrorEvents)(__assign(__assign({}, param), { error: error }));
|
|
170
|
+
for (_i = 0, _a = Object.entries(errorEvents); _i < _a.length; _i++) {
|
|
171
|
+
_b = _a[_i], errEvtIdx = _b[0], errEvt = _b[1];
|
|
172
|
+
for (_c = 0, _d = Object.entries(errEvt.otelAttributes); _c < _d.length; _c++) {
|
|
173
|
+
_e = _d[_c], key = _e[0], value = _e[1];
|
|
174
|
+
span.setAttribute("to_emit.".concat(errEvtIdx, ".").concat(key), value);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
return [2 /*return*/, {
|
|
178
|
+
errorToThrow: null,
|
|
179
|
+
events: errorEvents,
|
|
180
|
+
}];
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
}); };
|
|
184
|
+
exports.handleOrchestrationErrors = handleOrchestrationErrors;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export declare const OrchestrationExecutionStatus: {
|
|
2
|
+
readonly FAILURE: "failure";
|
|
3
|
+
readonly NORMAL: "normal";
|
|
4
|
+
};
|
|
5
|
+
export type OrchestrationExecutionMemoryRecord<T extends Record<string, unknown>> = (T & {
|
|
6
|
+
executionStatus: typeof OrchestrationExecutionStatus.NORMAL;
|
|
7
|
+
}) | (Partial<T> & {
|
|
8
|
+
executionStatus: typeof OrchestrationExecutionStatus.FAILURE;
|
|
9
|
+
error: Error;
|
|
10
|
+
subject: string;
|
|
11
|
+
});
|
package/dist/ArvoOrchestrationUtils/orchestrationExecutionWrapper/acquireLockWithValidation.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Span } from '@opentelemetry/api';
|
|
2
|
+
import { type ArvoEvent } from 'arvo-core';
|
|
3
|
+
import type { SyncEventResource } from '../../SyncEventResource';
|
|
4
|
+
import type { AcquiredLockStatusType } from '../../SyncEventResource/types';
|
|
5
|
+
/**
|
|
6
|
+
* Handles lock acquisition with proper error handling
|
|
7
|
+
*/
|
|
8
|
+
export declare const acquireLockWithValidation: (syncEventResource: SyncEventResource<Record<string, any>>, event: ArvoEvent, span: Span) => Promise<AcquiredLockStatusType>;
|
package/dist/ArvoOrchestrationUtils/orchestrationExecutionWrapper/acquireLockWithValidation.js
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
13
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.acquireLockWithValidation = void 0;
|
|
40
|
+
var arvo_core_1 = require("arvo-core");
|
|
41
|
+
var error_1 = require("../error");
|
|
42
|
+
/**
|
|
43
|
+
* Handles lock acquisition with proper error handling
|
|
44
|
+
*/
|
|
45
|
+
var acquireLockWithValidation = function (syncEventResource, event, span) { return __awaiter(void 0, void 0, void 0, function () {
|
|
46
|
+
var acquiredLock;
|
|
47
|
+
return __generator(this, function (_a) {
|
|
48
|
+
switch (_a.label) {
|
|
49
|
+
case 0: return [4 /*yield*/, syncEventResource.acquireLock(event, span)];
|
|
50
|
+
case 1:
|
|
51
|
+
acquiredLock = _a.sent();
|
|
52
|
+
if (acquiredLock === 'NOT_ACQUIRED') {
|
|
53
|
+
throw new error_1.TransactionViolation({
|
|
54
|
+
cause: error_1.TransactionViolationCause.LOCK_UNACQUIRED,
|
|
55
|
+
message: 'Lock acquisition denied - Unable to obtain exclusive access to event processing',
|
|
56
|
+
initiatingEvent: event,
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
if (acquiredLock === 'ACQUIRED') {
|
|
60
|
+
(0, arvo_core_1.logToSpan)({
|
|
61
|
+
level: 'INFO',
|
|
62
|
+
message: "This execution acquired lock at resource '".concat(event.subject, "'"),
|
|
63
|
+
}, span);
|
|
64
|
+
}
|
|
65
|
+
return [2 /*return*/, acquiredLock];
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
}); };
|
|
69
|
+
exports.acquireLockWithValidation = acquireLockWithValidation;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { type Span } from '@opentelemetry/api';
|
|
2
|
+
import { type ArvoEvent, type ArvoOrchestrationSubjectContent, type ArvoOrchestratorContract, type ArvoSemanticVersion, type OpenTelemetryHeaders, type VersionedArvoContract } from 'arvo-core';
|
|
3
|
+
import type IArvoEventHandler from '../../IArvoEventHandler';
|
|
4
|
+
import type { SyncEventResource } from '../../SyncEventResource';
|
|
5
|
+
import type { ArvoEventHandlerOpenTelemetryOptions } from '../../types';
|
|
6
|
+
import type { OrchestrationExecutionMemoryRecord } from '../orchestrationExecutionState';
|
|
7
|
+
import type { ArvoOrchestrationHandlerType } from '../types';
|
|
8
|
+
export type OrchestrationExecutionContext<TState extends OrchestrationExecutionMemoryRecord<Record<string, any>>> = {
|
|
9
|
+
event: ArvoEvent;
|
|
10
|
+
opentelemetry: ArvoEventHandlerOpenTelemetryOptions;
|
|
11
|
+
spanName: string;
|
|
12
|
+
source: string;
|
|
13
|
+
syncEventResource: SyncEventResource<TState>;
|
|
14
|
+
executionunits: number;
|
|
15
|
+
systemErrorDomain?: (string | null)[];
|
|
16
|
+
selfContract: VersionedArvoContract<ArvoOrchestratorContract, ArvoSemanticVersion>;
|
|
17
|
+
domain: string | null;
|
|
18
|
+
_handlerType: ArvoOrchestrationHandlerType;
|
|
19
|
+
};
|
|
20
|
+
export type CoreExecutionFn<TState extends OrchestrationExecutionMemoryRecord<Record<string, any>>> = (params: {
|
|
21
|
+
span: any;
|
|
22
|
+
otelHeaders: OpenTelemetryHeaders;
|
|
23
|
+
orchestrationParentSubject: string | null;
|
|
24
|
+
initEventId: string;
|
|
25
|
+
parsedEventSubject: ArvoOrchestrationSubjectContent;
|
|
26
|
+
state: TState | null;
|
|
27
|
+
_handlerType: ArvoOrchestrationHandlerType;
|
|
28
|
+
}) => Promise<{
|
|
29
|
+
emittables: ArvoEvent[];
|
|
30
|
+
newState: TState;
|
|
31
|
+
}>;
|
|
32
|
+
export declare const returnEventsWithLogging: (param: Awaited<ReturnType<IArvoEventHandler["execute"]>>, span: Span) => Awaited<ReturnType<IArvoEventHandler["execute"]>>;
|
|
33
|
+
/**
|
|
34
|
+
* Wraps orchestration execution with common infrastructure:
|
|
35
|
+
* - OpenTelemetry span management
|
|
36
|
+
* - Lock acquisition and release
|
|
37
|
+
* - State management
|
|
38
|
+
* - Error handling and system error generation
|
|
39
|
+
*/
|
|
40
|
+
export declare const executeWithOrchestrationWrapper: <TState extends OrchestrationExecutionMemoryRecord<Record<string, any>>>({ event, opentelemetry, spanName, source, syncEventResource, executionunits, systemErrorDomain, selfContract, domain, _handlerType, }: OrchestrationExecutionContext<TState>, coreExecutionFn: CoreExecutionFn<TState>) => Promise<Awaited<ReturnType<IArvoEventHandler["execute"]>>>;
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
|
3
|
+
__assign = Object.assign || function(t) {
|
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
+
s = arguments[i];
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
};
|
|
11
|
+
return __assign.apply(this, arguments);
|
|
12
|
+
};
|
|
13
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
14
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
15
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
16
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
17
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
18
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
19
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
23
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
24
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
25
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
26
|
+
function step(op) {
|
|
27
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
28
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
29
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
30
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
31
|
+
switch (op[0]) {
|
|
32
|
+
case 0: case 1: t = op; break;
|
|
33
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
34
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
35
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
36
|
+
default:
|
|
37
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
38
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
39
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
40
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
41
|
+
if (t[2]) _.ops.pop();
|
|
42
|
+
_.trys.pop(); continue;
|
|
43
|
+
}
|
|
44
|
+
op = body.call(thisArg, _);
|
|
45
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
46
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
50
|
+
exports.executeWithOrchestrationWrapper = exports.returnEventsWithLogging = void 0;
|
|
51
|
+
var api_1 = require("@opentelemetry/api");
|
|
52
|
+
var arvo_core_1 = require("arvo-core");
|
|
53
|
+
var handlerErrors_1 = require("../handlerErrors");
|
|
54
|
+
var acquireLockWithValidation_1 = require("./acquireLockWithValidation");
|
|
55
|
+
var validateAndParseSubject_1 = require("./validateAndParseSubject");
|
|
56
|
+
var returnEventsWithLogging = function (param, span) {
|
|
57
|
+
var _a, _b;
|
|
58
|
+
(0, arvo_core_1.logToSpan)({
|
|
59
|
+
level: 'INFO',
|
|
60
|
+
message: "Execution completed with issues and emitted ".concat((_b = (_a = param.events) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0, " events"),
|
|
61
|
+
}, span);
|
|
62
|
+
return param;
|
|
63
|
+
};
|
|
64
|
+
exports.returnEventsWithLogging = returnEventsWithLogging;
|
|
65
|
+
/**
|
|
66
|
+
* Wraps orchestration execution with common infrastructure:
|
|
67
|
+
* - OpenTelemetry span management
|
|
68
|
+
* - Lock acquisition and release
|
|
69
|
+
* - State management
|
|
70
|
+
* - Error handling and system error generation
|
|
71
|
+
*/
|
|
72
|
+
var executeWithOrchestrationWrapper = function (_a, coreExecutionFn_1) { return __awaiter(void 0, [_a, coreExecutionFn_1], void 0, function (_b, coreExecutionFn) {
|
|
73
|
+
var _c;
|
|
74
|
+
var event = _b.event, opentelemetry = _b.opentelemetry, spanName = _b.spanName, source = _b.source, syncEventResource = _b.syncEventResource, executionunits = _b.executionunits, systemErrorDomain = _b.systemErrorDomain, selfContract = _b.selfContract, domain = _b.domain, _handlerType = _b._handlerType;
|
|
75
|
+
return __generator(this, function (_d) {
|
|
76
|
+
switch (_d.label) {
|
|
77
|
+
case 0: return [4 /*yield*/, arvo_core_1.ArvoOpenTelemetry.getInstance().startActiveSpan({
|
|
78
|
+
name: spanName,
|
|
79
|
+
spanOptions: {
|
|
80
|
+
kind: api_1.SpanKind.PRODUCER,
|
|
81
|
+
attributes: __assign((_c = {}, _c[arvo_core_1.ArvoExecution.ATTR_SPAN_KIND] = arvo_core_1.ArvoExecutionSpanKind.ORCHESTRATOR, _c[arvo_core_1.OpenInference.ATTR_SPAN_KIND] = arvo_core_1.OpenInferenceSpanKind.CHAIN, _c), Object.fromEntries(Object.entries(event.otelAttributes).map(function (_a) {
|
|
82
|
+
var key = _a[0], value = _a[1];
|
|
83
|
+
return ["to_process.0.".concat(key), value];
|
|
84
|
+
}))),
|
|
85
|
+
},
|
|
86
|
+
context: opentelemetry.inheritFrom === 'EVENT'
|
|
87
|
+
? {
|
|
88
|
+
inheritFrom: 'TRACE_HEADERS',
|
|
89
|
+
traceHeaders: {
|
|
90
|
+
traceparent: event.traceparent,
|
|
91
|
+
tracestate: event.tracestate,
|
|
92
|
+
},
|
|
93
|
+
}
|
|
94
|
+
: {
|
|
95
|
+
inheritFrom: 'CONTEXT',
|
|
96
|
+
context: api_1.context.active(),
|
|
97
|
+
},
|
|
98
|
+
disableSpanManagement: true,
|
|
99
|
+
fn: function (span) { return __awaiter(void 0, void 0, void 0, function () {
|
|
100
|
+
var otelHeaders, orchestrationParentSubject, initEventId, acquiredLock, parsedEventSubject, state, _a, emittables, newState, i, _i, _b, _c, key, value, error_1, _d, errorToThrow, errorEvents;
|
|
101
|
+
var _e, _f, _g, _h;
|
|
102
|
+
return __generator(this, function (_j) {
|
|
103
|
+
switch (_j.label) {
|
|
104
|
+
case 0:
|
|
105
|
+
(0, arvo_core_1.logToSpan)({
|
|
106
|
+
level: 'INFO',
|
|
107
|
+
message: "Starting execution for ".concat(event.type, " on subject ").concat(event.subject),
|
|
108
|
+
}, span);
|
|
109
|
+
otelHeaders = (0, arvo_core_1.currentOpenTelemetryHeaders)();
|
|
110
|
+
orchestrationParentSubject = null;
|
|
111
|
+
initEventId = null;
|
|
112
|
+
acquiredLock = null;
|
|
113
|
+
_j.label = 1;
|
|
114
|
+
case 1:
|
|
115
|
+
_j.trys.push([1, 6, 8, 10]);
|
|
116
|
+
parsedEventSubject = (0, validateAndParseSubject_1.validateAndParseSubject)(event, source, syncEventResource, span, 'orchestrator');
|
|
117
|
+
if (!parsedEventSubject) {
|
|
118
|
+
return [2 /*return*/, (0, exports.returnEventsWithLogging)({ events: [] }, span)];
|
|
119
|
+
}
|
|
120
|
+
return [4 /*yield*/, (0, acquireLockWithValidation_1.acquireLockWithValidation)(syncEventResource, event, span)];
|
|
121
|
+
case 2:
|
|
122
|
+
// Lock acquisition
|
|
123
|
+
acquiredLock = _j.sent();
|
|
124
|
+
return [4 /*yield*/, syncEventResource.acquireState(event, span)];
|
|
125
|
+
case 3:
|
|
126
|
+
state = _j.sent();
|
|
127
|
+
if ((state === null || state === void 0 ? void 0 : state.executionStatus) === 'failure') {
|
|
128
|
+
(0, arvo_core_1.logToSpan)({
|
|
129
|
+
level: 'WARNING',
|
|
130
|
+
message: "The orchestration has failed in a previous event. Ignoring event id: ".concat(event.id, " with event subject: ").concat(event.subject),
|
|
131
|
+
}, span);
|
|
132
|
+
return [2 /*return*/, (0, exports.returnEventsWithLogging)({ events: [] }, span)];
|
|
133
|
+
}
|
|
134
|
+
orchestrationParentSubject = (_e = state === null || state === void 0 ? void 0 : state.parentSubject) !== null && _e !== void 0 ? _e : null;
|
|
135
|
+
initEventId = (_f = state === null || state === void 0 ? void 0 : state.initEventId) !== null && _f !== void 0 ? _f : null;
|
|
136
|
+
if (!state) {
|
|
137
|
+
(0, arvo_core_1.logToSpan)({
|
|
138
|
+
level: 'INFO',
|
|
139
|
+
message: "Initializing new execution state for subject: ".concat(event.subject),
|
|
140
|
+
});
|
|
141
|
+
if (event.type !== source) {
|
|
142
|
+
(0, arvo_core_1.logToSpan)({
|
|
143
|
+
level: 'WARNING',
|
|
144
|
+
message: "Invalid initialization event detected. Expected type '".concat(source, "' but received '").concat(event.type, "'."),
|
|
145
|
+
});
|
|
146
|
+
return [2 /*return*/, (0, exports.returnEventsWithLogging)({ events: [] }, span)];
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
else {
|
|
150
|
+
(0, arvo_core_1.logToSpan)({
|
|
151
|
+
level: 'INFO',
|
|
152
|
+
message: "Resuming execution with existing state for subject: ".concat(event.subject),
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
// Extract parent subject from init event if applicable
|
|
156
|
+
if (event.type === source) {
|
|
157
|
+
orchestrationParentSubject = (_h = (_g = event === null || event === void 0 ? void 0 : event.data) === null || _g === void 0 ? void 0 : _g.parentSubject$$) !== null && _h !== void 0 ? _h : null;
|
|
158
|
+
}
|
|
159
|
+
return [4 /*yield*/, coreExecutionFn({
|
|
160
|
+
span: span,
|
|
161
|
+
otelHeaders: otelHeaders,
|
|
162
|
+
orchestrationParentSubject: orchestrationParentSubject,
|
|
163
|
+
initEventId: initEventId !== null && initEventId !== void 0 ? initEventId : event.id,
|
|
164
|
+
parsedEventSubject: parsedEventSubject,
|
|
165
|
+
state: state,
|
|
166
|
+
_handlerType: _handlerType,
|
|
167
|
+
})];
|
|
168
|
+
case 4:
|
|
169
|
+
_a = _j.sent(), emittables = _a.emittables, newState = _a.newState;
|
|
170
|
+
span.setAttribute("arvo.".concat(_handlerType, ".execution.status"), newState.executionStatus);
|
|
171
|
+
// Add OpenTelemetry attributes for emitted events
|
|
172
|
+
for (i = 0; i < emittables.length; i++) {
|
|
173
|
+
for (_i = 0, _b = Object.entries(emittables[i].otelAttributes); _i < _b.length; _i++) {
|
|
174
|
+
_c = _b[_i], key = _c[0], value = _c[1];
|
|
175
|
+
span.setAttribute("to_emit.".concat(i, ".").concat(key), value);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
// Persist state
|
|
179
|
+
return [4 /*yield*/, syncEventResource.persistState(event, newState, state, span)];
|
|
180
|
+
case 5:
|
|
181
|
+
// Persist state
|
|
182
|
+
_j.sent();
|
|
183
|
+
(0, arvo_core_1.logToSpan)({
|
|
184
|
+
level: 'INFO',
|
|
185
|
+
message: "State update persisted in memory for subject ".concat(event.subject),
|
|
186
|
+
});
|
|
187
|
+
(0, arvo_core_1.logToSpan)({
|
|
188
|
+
level: 'INFO',
|
|
189
|
+
message: "Execution successfully completed and emitted ".concat(emittables.length, " events"),
|
|
190
|
+
});
|
|
191
|
+
return [2 /*return*/, (0, exports.returnEventsWithLogging)({ events: emittables }, span)];
|
|
192
|
+
case 6:
|
|
193
|
+
error_1 = _j.sent();
|
|
194
|
+
return [4 /*yield*/, (0, handlerErrors_1.handleOrchestrationErrors)(_handlerType, {
|
|
195
|
+
error: error_1,
|
|
196
|
+
event: event,
|
|
197
|
+
otelHeaders: otelHeaders,
|
|
198
|
+
orchestrationParentSubject: orchestrationParentSubject,
|
|
199
|
+
initEventId: initEventId,
|
|
200
|
+
selfContract: selfContract,
|
|
201
|
+
systemErrorDomain: systemErrorDomain,
|
|
202
|
+
executionunits: executionunits,
|
|
203
|
+
source: source,
|
|
204
|
+
domain: domain,
|
|
205
|
+
syncEventResource: syncEventResource,
|
|
206
|
+
}, span)];
|
|
207
|
+
case 7:
|
|
208
|
+
_d = _j.sent(), errorToThrow = _d.errorToThrow, errorEvents = _d.events;
|
|
209
|
+
if (errorToThrow)
|
|
210
|
+
throw errorToThrow;
|
|
211
|
+
return [2 /*return*/, {
|
|
212
|
+
events: errorEvents,
|
|
213
|
+
}];
|
|
214
|
+
case 8: return [4 /*yield*/, syncEventResource.releaseLock(event, acquiredLock, span)];
|
|
215
|
+
case 9:
|
|
216
|
+
_j.sent();
|
|
217
|
+
span.end();
|
|
218
|
+
return [7 /*endfinally*/];
|
|
219
|
+
case 10: return [2 /*return*/];
|
|
220
|
+
}
|
|
221
|
+
});
|
|
222
|
+
}); },
|
|
223
|
+
})];
|
|
224
|
+
case 1: return [2 /*return*/, _d.sent()];
|
|
225
|
+
}
|
|
226
|
+
});
|
|
227
|
+
}); };
|
|
228
|
+
exports.executeWithOrchestrationWrapper = executeWithOrchestrationWrapper;
|
package/dist/ArvoOrchestrationUtils/orchestrationExecutionWrapper/validateAndParseSubject.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { Span } from '@opentelemetry/api';
|
|
2
|
+
import { type ArvoEvent, type ArvoOrchestrationSubjectContent } from 'arvo-core';
|
|
3
|
+
import type { SyncEventResource } from '../../SyncEventResource';
|
|
4
|
+
/**
|
|
5
|
+
* Validates and parses orchestration subject
|
|
6
|
+
*/
|
|
7
|
+
export declare const validateAndParseSubject: (event: ArvoEvent, expectedSource: string, syncEventResource: SyncEventResource<any>, span: Span, handlerType: "orchestrator" | "resumable") => ArvoOrchestrationSubjectContent | null;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validateAndParseSubject = void 0;
|
|
4
|
+
var arvo_core_1 = require("arvo-core");
|
|
5
|
+
/**
|
|
6
|
+
* Validates and parses orchestration subject
|
|
7
|
+
*/
|
|
8
|
+
var validateAndParseSubject = function (event, expectedSource, syncEventResource, span, handlerType) {
|
|
9
|
+
var _a;
|
|
10
|
+
syncEventResource.validateEventSubject(event, span);
|
|
11
|
+
var parsedEventSubject = arvo_core_1.ArvoOrchestrationSubject.parse(event.subject);
|
|
12
|
+
span.setAttributes((_a = {},
|
|
13
|
+
_a["arvo.parsed.subject.".concat(handlerType, ".name")] = parsedEventSubject.orchestrator.name,
|
|
14
|
+
_a["arvo.parsed.subject.".concat(handlerType, ".version")] = parsedEventSubject.orchestrator.version,
|
|
15
|
+
_a));
|
|
16
|
+
if (parsedEventSubject.orchestrator.name !== expectedSource) {
|
|
17
|
+
(0, arvo_core_1.logToSpan)({
|
|
18
|
+
level: 'WARNING',
|
|
19
|
+
message: "Event subject mismatch - expected '".concat(expectedSource, "' but got '").concat(parsedEventSubject.orchestrator.name, "'"),
|
|
20
|
+
}, span);
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
return parsedEventSubject;
|
|
24
|
+
};
|
|
25
|
+
exports.validateAndParseSubject = validateAndParseSubject;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type ArvoOrchestrationHandlerType = 'orchestrator' | 'resumable';
|
|
@@ -13,6 +13,7 @@ exports.createArvoOrchestrator = void 0;
|
|
|
13
13
|
var _1 = require(".");
|
|
14
14
|
var MachineExecutionEngine_1 = require("../MachineExecutionEngine");
|
|
15
15
|
var MachineRegistry_1 = require("../MachineRegistry");
|
|
16
|
+
var errors_1 = require("../errors");
|
|
16
17
|
/**
|
|
17
18
|
* Creates a new Arvo orchestrator instance with default components.
|
|
18
19
|
* For custom components, use ArvoOrchestrator constructor directly.
|
|
@@ -46,6 +47,18 @@ var createArvoOrchestrator = function (_a) {
|
|
|
46
47
|
}
|
|
47
48
|
var registry = new (MachineRegistry_1.MachineRegistry.bind.apply(MachineRegistry_1.MachineRegistry, __spreadArray([void 0], machines, false)))();
|
|
48
49
|
var requiresResourceLocking = machines.some(function (machine) { return machine.requiresResourceLocking; });
|
|
50
|
+
var representativeMachine = registry.machines[0];
|
|
51
|
+
var lastSeenVersions = [];
|
|
52
|
+
for (var _i = 0, _b = registry.machines; _i < _b.length; _i++) {
|
|
53
|
+
var machine = _b[_i];
|
|
54
|
+
if (representativeMachine.source !== machine.source) {
|
|
55
|
+
throw new errors_1.ConfigViolation("All the machines in the orchestrator must have type '".concat(representativeMachine.source, "'"));
|
|
56
|
+
}
|
|
57
|
+
if (lastSeenVersions.includes(machine.version)) {
|
|
58
|
+
throw new errors_1.ConfigViolation("An orchestrator must have unique machine versions. Machine ID:".concat(machine.id, " has duplicate version ").concat(machine.version, "."));
|
|
59
|
+
}
|
|
60
|
+
lastSeenVersions.push(machine.version);
|
|
61
|
+
}
|
|
49
62
|
return new _1.ArvoOrchestrator({
|
|
50
63
|
executionunits: executionunits,
|
|
51
64
|
memory: memory,
|