arvo-event-handler
Version:
Type-safe event handler system with versioning, telemetry, and contract validation for distributed Arvo event-driven architectures, featuring routing and multi-handler support.
626 lines (625 loc) • 43.7 kB
JavaScript
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
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);
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (g && (g = 0, op[0] && (_ = 0)), _) try {
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;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ArvoOrchestrator = void 0;
var api_1 = require("@opentelemetry/api");
var arvo_core_1 = require("arvo-core");
var utils_1 = require("../utils");
var error_1 = require("./error");
var SyncEventResource_1 = require("../SyncEventResource");
var AbstractArvoEventHandler_1 = __importDefault(require("../AbstractArvoEventHandler"));
var errors_1 = require("../errors");
var ArvoDomain_1 = require("../ArvoDomain");
/**
* Orchestrates state machine execution and lifecycle management.
* Handles machine resolution, state management, event processing and error handling.
*/
var ArvoOrchestrator = /** @class */ (function (_super) {
__extends(ArvoOrchestrator, _super);
/**
* Creates a new orchestrator instance
* @param params - Configuration parameters
* @throws Error if machines in registry have different sources
*/
function ArvoOrchestrator(_a) {
var executionunits = _a.executionunits, memory = _a.memory, registry = _a.registry, executionEngine = _a.executionEngine, requiresResourceLocking = _a.requiresResourceLocking, systemErrorDomain = _a.systemErrorDomain;
var _this = _super.call(this) || this;
_this.systemErrorDomain = [];
_this.executionunits = executionunits;
var representativeMachine = registry.machines[0];
var lastSeenVersions = [];
for (var _i = 0, _b = registry.machines; _i < _b.length; _i++) {
var machine = _b[_i];
if (representativeMachine.source !== machine.source) {
throw new Error("All the machines in the orchestrator must have type '".concat(representativeMachine.source, "'"));
}
if (lastSeenVersions.includes(machine.version)) {
throw new Error("An orchestrator must have unique machine versions. Machine ID:".concat(machine.id, " has duplicate version ").concat(machine.version, "."));
}
lastSeenVersions.push(machine.version);
}
_this.registry = registry;
_this.executionEngine = executionEngine;
_this.syncEventResource = new SyncEventResource_1.SyncEventResource(memory, requiresResourceLocking);
_this.systemErrorDomain = systemErrorDomain;
return _this;
}
Object.defineProperty(ArvoOrchestrator.prototype, "source", {
get: function () {
return this.registry.machines[0].source;
},
enumerable: false,
configurable: true
});
Object.defineProperty(ArvoOrchestrator.prototype, "requiresResourceLocking", {
get: function () {
return this.syncEventResource.requiresResourceLocking;
},
enumerable: false,
configurable: true
});
Object.defineProperty(ArvoOrchestrator.prototype, "memory", {
get: function () {
return this.syncEventResource.memory;
},
enumerable: false,
configurable: true
});
Object.defineProperty(ArvoOrchestrator.prototype, "domain", {
get: function () {
return this.registry.machines[0].contracts.self.domain;
},
enumerable: false,
configurable: true
});
/**
* Creates emittable event from execution result
* @param event - Source event to emit
* @param machine - Machine that generated event
* @param otelHeaders - OpenTelemetry headers
* @param orchestrationParentSubject - Parent orchestration subject
* @param sourceEvent - Original triggering event
* @param initEventId - The id of the event which initiated the orchestration in the first place
* @param _domain - The domain of the event.
*
* @throws {ContractViolation} On schema/contract mismatch
* @throws {ExecutionViolation} On invalid parentSubject$$ format
*/
ArvoOrchestrator.prototype.createEmittableEvent = function (event, machine, otelHeaders, orchestrationParentSubject, sourceEvent, initEventId, _domain) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
(0, arvo_core_1.logToSpan)({
level: 'INFO',
message: "Creating emittable event: ".concat(event.type),
});
var selfContract = machine.contracts.self;
var serviceContract = Object.fromEntries(Object.values(machine.contracts.services).map(function (item) { return [item.accepts.type, item]; }));
var schema = null;
var contract = null;
var subject = sourceEvent.subject;
var parentId = sourceEvent.id;
var domain = (0, ArvoDomain_1.resolveEventDomain)({
domainToResolve: _domain,
handlerSelfContract: selfContract,
eventContract: null,
triggeringEvent: sourceEvent,
});
if (event.type === selfContract.metadata.completeEventType) {
(0, arvo_core_1.logToSpan)({
level: 'INFO',
message: "Creating event for machine workflow completion: ".concat(event.type),
});
contract = selfContract;
schema = selfContract.emits[selfContract.metadata.completeEventType];
subject = orchestrationParentSubject !== null && orchestrationParentSubject !== void 0 ? orchestrationParentSubject : sourceEvent.subject;
parentId = initEventId;
domain = (0, ArvoDomain_1.resolveEventDomain)({
domainToResolve: _domain,
handlerSelfContract: selfContract,
eventContract: selfContract,
triggeringEvent: sourceEvent,
});
}
else if (serviceContract[event.type]) {
(0, arvo_core_1.logToSpan)({
level: 'INFO',
message: "Creating service event for external system: ".concat(event.type),
});
contract = serviceContract[event.type];
schema = serviceContract[event.type].accepts.schema;
domain = (0, ArvoDomain_1.resolveEventDomain)({
domainToResolve: _domain,
handlerSelfContract: selfContract,
eventContract: contract,
triggeringEvent: sourceEvent,
});
// If the event is to call another orchestrator then, extract the parent subject
// passed to it and then form an new subject. This allows for event chaining
// between orchestrators
if (contract.metadata.contractType === 'ArvoOrchestratorContract') {
if (event.data.parentSubject$$) {
try {
arvo_core_1.ArvoOrchestrationSubject.parse(event.data.parentSubject$$);
}
catch (_m) {
throw new errors_1.ExecutionViolation("Invalid parentSubject$$ for the event(type='".concat(event.type, "', uri='").concat((_a = event.dataschema) !== null && _a !== void 0 ? _a : arvo_core_1.EventDataschemaUtil.create(contract), "').It must be follow the ArvoOrchestrationSubject schema. The easiest way is to use the current orchestration subject by storing the subject via the context block in the machine definition."));
}
}
try {
if (event.data.parentSubject$$) {
subject = arvo_core_1.ArvoOrchestrationSubject.from({
orchestator: contract.accepts.type,
version: contract.version,
subject: event.data.parentSubject$$,
domain: domain !== null && domain !== void 0 ? domain : null,
meta: {
redirectto: (_b = event.redirectto) !== null && _b !== void 0 ? _b : this.source,
},
});
}
else {
subject = arvo_core_1.ArvoOrchestrationSubject.new({
version: contract.version,
orchestator: contract.accepts.type,
initiator: this.source,
domain: domain !== null && domain !== void 0 ? domain : undefined,
meta: {
redirectto: (_c = event.redirectto) !== null && _c !== void 0 ? _c : this.source,
},
});
}
}
catch (error) {
// This is a execution violation because it indicates faulty parent subject
// or some fundamental error with subject creation which must be not be propagated
// any further and investigated manually.
throw new errors_1.ExecutionViolation("Orchestration subject creation failed due to invalid parameters - Event: ".concat(event.type, " - Check event emit parameters in the machine definition. ").concat(error === null || error === void 0 ? void 0 : error.message));
}
}
}
var finalDataschema = event.dataschema;
var finalData = event.data;
// finally if the contract and the schema are available
// then use them to validate the event. Otherwise just use
// the data from the incoming event which is raw and created
// by the machine
if (contract && schema) {
try {
finalData = schema.parse(event.data);
finalDataschema = arvo_core_1.EventDataschemaUtil.create(contract);
}
catch (error) {
throw new errors_1.ContractViolation("Invalid event data: Schema validation failed - Check emit parameters in machine definition.\nEvent type: ".concat(event.type, "\nDetails: ").concat(error.message));
}
}
// Create the event
var emittableEvent = (0, arvo_core_1.createArvoEvent)({
source: this.source,
type: event.type,
subject: subject,
dataschema: finalDataschema !== null && finalDataschema !== void 0 ? finalDataschema : undefined,
data: finalData,
to: (_d = event.to) !== null && _d !== void 0 ? _d : event.type,
accesscontrol: (_f = (_e = event.accesscontrol) !== null && _e !== void 0 ? _e : sourceEvent.accesscontrol) !== null && _f !== void 0 ? _f : undefined,
// The orchestrator does not respect redirectto from the source event
redirectto: (_g = event.redirectto) !== null && _g !== void 0 ? _g : this.source,
executionunits: (_h = event.executionunits) !== null && _h !== void 0 ? _h : this.executionunits,
traceparent: (_j = otelHeaders.traceparent) !== null && _j !== void 0 ? _j : undefined,
tracestate: (_k = otelHeaders.tracestate) !== null && _k !== void 0 ? _k : undefined,
parentid: parentId,
domain: domain !== null && domain !== void 0 ? domain : undefined,
}, (_l = event.__extensions) !== null && _l !== void 0 ? _l : {});
(0, arvo_core_1.logToSpan)({
level: 'INFO',
message: "Event created successfully: ".concat(emittableEvent.type),
});
return emittableEvent;
};
/**
* Core orchestration method that executes state machines in response to events.
*
* @param event - Event triggering the execution
* @param opentelemetry - OpenTelemetry configuration
* @returns Object containing domained events
*
* @throws {TransactionViolation} Lock/state operations failed
* @throws {ExecutionViolation} Invalid event structure/flow
* @throws {ContractViolation} Schema/contract mismatch
* @throws {ConfigViolation} Missing/invalid machine version
*/
ArvoOrchestrator.prototype.execute = function (event_1) {
return __awaiter(this, arguments, void 0, function (event, opentelemetry) {
var _a;
var _this = this;
if (opentelemetry === void 0) { opentelemetry = {
inheritFrom: 'EVENT',
}; }
return __generator(this, function (_b) {
switch (_b.label) {
case 0: return [4 /*yield*/, arvo_core_1.ArvoOpenTelemetry.getInstance().startActiveSpan({
name: "Orchestrator<".concat(this.registry.machines[0].contracts.self.uri, ">@<").concat(event.type, ">"),
spanOptions: {
kind: api_1.SpanKind.PRODUCER,
attributes: __assign((_a = {}, _a[arvo_core_1.ArvoExecution.ATTR_SPAN_KIND] = arvo_core_1.ArvoExecutionSpanKind.ORCHESTRATOR, _a[arvo_core_1.OpenInference.ATTR_SPAN_KIND] = arvo_core_1.OpenInferenceSpanKind.CHAIN, _a), Object.fromEntries(Object.entries(event.otelAttributes).map(function (_a) {
var key = _a[0], value = _a[1];
return ["to_process.0.".concat(key), value];
}))),
},
context: opentelemetry.inheritFrom === 'EVENT'
? {
inheritFrom: 'TRACE_HEADERS',
traceHeaders: {
traceparent: event.traceparent,
tracestate: event.tracestate,
},
}
: {
inheritFrom: 'CONTEXT',
context: api_1.context.active(),
},
disableSpanManagement: true,
fn: function (span) { return __awaiter(_this, void 0, void 0, function () {
var otelHeaders, orchestrationParentSubject, initEventId, acquiredLock, parsedEventSubject, machine, _a, name_1, version, inputValidation, state, executionResult, rawMachineEmittedEvents, emittables, _i, rawMachineEmittedEvents_1, item, createdDomain, _b, _c, _dom, evt, _d, _e, _f, key, value, error_2, e, parsedEventSubject, result, _g, _h, _dom, _j, _k, _l, key, value;
var _this = this;
var _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z;
return __generator(this, function (_0) {
switch (_0.label) {
case 0:
(0, arvo_core_1.logToSpan)({
level: 'INFO',
message: "Orchestrator starting execution for ".concat(event.type, " on subject ").concat(event.subject),
});
otelHeaders = (0, arvo_core_1.currentOpenTelemetryHeaders)();
orchestrationParentSubject = null;
initEventId = null;
acquiredLock = null;
_0.label = 1;
case 1:
_0.trys.push([1, 5, 6, 8]);
///////////////////////////////////////////////////////////////
// Subject resolution, machine resolution and input validation
///////////////////////////////////////////////////////////////
this.syncEventResource.validateEventSubject(event, span);
parsedEventSubject = arvo_core_1.ArvoOrchestrationSubject.parse(event.subject);
span.setAttributes({
'arvo.parsed.subject.orchestrator.name': parsedEventSubject.orchestrator.name,
'arvo.parsed.subject.orchestrator.version': parsedEventSubject.orchestrator.version,
});
// The wrong source is not a big violation. May be some routing went wrong. So just ignore
if (parsedEventSubject.orchestrator.name !== this.source) {
(0, arvo_core_1.logToSpan)({
level: 'WARNING',
message: "Event subject mismatch detected. Expected orchestrator '".concat(this.source, "' but subject indicates '").concat(parsedEventSubject.orchestrator.name, "'. This indicates either a routing error or a non-applicable event that can be safely ignored."),
});
(0, arvo_core_1.logToSpan)({
level: 'INFO',
message: 'Orchestration executed with issues and emitted 0 events',
});
return [2 /*return*/, {
events: [],
}];
}
(0, arvo_core_1.logToSpan)({
level: 'INFO',
message: "Resolving machine for event ".concat(event.type),
});
machine = this.registry.resolve(event, {
inheritFrom: 'CONTEXT',
});
// Unable to find a machine is a big configuration bug and violation
if (!machine) {
_a = parsedEventSubject.orchestrator, name_1 = _a.name, version = _a.version;
throw new errors_1.ConfigViolation("Machine resolution failed: No machine found matching orchestrator name='".concat(name_1, "' and version='").concat(version, "'."));
}
(0, arvo_core_1.logToSpan)({
level: 'INFO',
message: "Input validation started for event ".concat(event.type, " on machine ").concat(machine.source),
});
inputValidation = machine.validateInput(event);
if (inputValidation.type === 'CONTRACT_UNRESOLVED') {
// This is a configuration error because the contract was never
// configured in the machine. That is why it was unresolved. It
// signifies a problem in configration not the data or event flow
throw new errors_1.ConfigViolation('Contract validation failed - Event does not match any registered contract schemas in the machine');
}
if (inputValidation.type === 'INVALID_DATA' || inputValidation.type === 'INVALID') {
// This is a contract error becuase there is a configuration but
// the event data received was invalid due to conflicting data
// or event dataschema did not match the contract data schema. This
// signifies an issue with event flow because unexpected events
// are being received
throw new errors_1.ContractViolation("Input validation failed - Event data does not meet contract requirements: ".concat(inputValidation.error.message));
}
return [4 /*yield*/, this.syncEventResource.acquireLock(event, span)];
case 2:
///////////////////////////////////////////////////////////////
// State locking, acquiry machine exection
///////////////////////////////////////////////////////////////
acquiredLock = _0.sent();
if (acquiredLock === 'NOT_ACQUIRED') {
throw new error_1.TransactionViolation({
cause: error_1.TransactionViolationCause.LOCK_UNACQUIRED,
message: 'Lock acquisition denied - Unable to obtain exclusive access to event processing',
initiatingEvent: event,
});
}
if (acquiredLock === 'ACQUIRED') {
(0, arvo_core_1.logToSpan)({
level: 'INFO',
message: "This execution acquired lock at resource '".concat(event.subject, "'"),
});
}
return [4 /*yield*/, this.syncEventResource.acquireState(event, span)];
case 3:
state = _0.sent();
orchestrationParentSubject = (_m = state === null || state === void 0 ? void 0 : state.parentSubject) !== null && _m !== void 0 ? _m : null;
initEventId = (_o = state === null || state === void 0 ? void 0 : state.initEventId) !== null && _o !== void 0 ? _o : event.id;
if (!state) {
(0, arvo_core_1.logToSpan)({
level: 'INFO',
message: "Initializing new execution state for subject: ".concat(event.subject),
});
if (event.type !== this.source) {
(0, arvo_core_1.logToSpan)({
level: 'WARNING',
message: "Invalid initialization event detected. Expected type '".concat(this.source, "' but received '").concat(event.type, "'. This may indicate an incorrectly routed event or a non-initialization event that can be safely ignored."),
});
return [2 /*return*/, {
events: [],
}];
}
}
else {
(0, arvo_core_1.logToSpan)({
level: 'INFO',
message: "Resuming execution with existing state for subject: ".concat(event.subject),
});
}
// In case the event is the init event then
// extract the parent subject from it and assume
// it to be the orchestration parent subject
if (event.type === this.source) {
orchestrationParentSubject = (_q = (_p = event === null || event === void 0 ? void 0 : event.data) === null || _p === void 0 ? void 0 : _p.parentSubject$$) !== null && _q !== void 0 ? _q : null;
}
executionResult = this.executionEngine.execute({
state: (_r = state === null || state === void 0 ? void 0 : state.state) !== null && _r !== void 0 ? _r : null,
event: event,
machine: machine,
}, { inheritFrom: 'CONTEXT' });
span.setAttribute('arvo.orchestration.status', executionResult.state.status);
rawMachineEmittedEvents = executionResult.events;
// In case execution of the machine has finished
// and the final output has been created, then in
// that case, make the raw event as the final output
// is not even raw enough to be called an event yet
if (executionResult.finalOutput) {
rawMachineEmittedEvents.push({
type: machine.contracts.self
.metadata.completeEventType,
data: executionResult.finalOutput,
to: (_t = (_s = parsedEventSubject.meta) === null || _s === void 0 ? void 0 : _s.redirectto) !== null && _t !== void 0 ? _t : parsedEventSubject.execution.initiator,
domain: orchestrationParentSubject
? [arvo_core_1.ArvoOrchestrationSubject.parse(orchestrationParentSubject).execution.domain]
: [null],
});
}
emittables = [];
for (_i = 0, rawMachineEmittedEvents_1 = rawMachineEmittedEvents; _i < rawMachineEmittedEvents_1.length; _i++) {
item = rawMachineEmittedEvents_1[_i];
createdDomain = new Set();
for (_b = 0, _c = Array.from(new Set((_u = item.domain) !== null && _u !== void 0 ? _u : [null])); _b < _c.length; _b++) {
_dom = _c[_b];
evt = this.createEmittableEvent(item, machine, otelHeaders, orchestrationParentSubject, event, initEventId, _dom);
// Making sure the raw event broadcast is actually unique as the
// domain resolution (especially for symbolic) can only happen
// in the createEmittableEvent
if (createdDomain.has(evt.domain))
continue;
createdDomain.add(evt.domain);
emittables.push(evt);
for (_d = 0, _e = Object.entries(emittables[emittables.length - 1].otelAttributes); _d < _e.length; _d++) {
_f = _e[_d], key = _f[0], value = _f[1];
span.setAttribute("to_emit.".concat(emittables.length - 1, ".").concat(key), value);
}
}
}
(0, arvo_core_1.logToSpan)({
level: 'INFO',
message: "Machine execution completed - Status: ".concat(executionResult.state.status, ", Generated events: ").concat(executionResult.events.length),
});
// Write to the memory
return [4 /*yield*/, this.syncEventResource.persistState(event, {
initEventId: initEventId,
subject: event.subject,
parentSubject: orchestrationParentSubject,
status: executionResult.state.status,
value: (_v = executionResult.state.value) !== null && _v !== void 0 ? _v : null,
state: executionResult.state,
events: {
consumed: event.toJSON(),
produced: emittables.map(function (item) { return item.toJSON(); }),
},
machineDefinition: JSON.stringify(machine.logic.config),
}, state, span)];
case 4:
// Write to the memory
_0.sent();
(0, arvo_core_1.logToSpan)({
level: 'INFO',
message: "State update persisted in memory for subject ".concat(event.subject),
});
(0, arvo_core_1.logToSpan)({
level: 'INFO',
message: "Orchestration successfully executed and emitted ".concat(emittables.length, " events"),
});
return [2 /*return*/, { events: emittables }];
case 5:
error_2 = _0.sent();
e = (0, utils_1.isError)(error_2)
? error_2
: new errors_1.ExecutionViolation("Non-Error object thrown during machine execution: ".concat(typeof error_2, ". This indicates a serious implementation flaw."));
(0, arvo_core_1.exceptionToSpan)(e);
span.setStatus({
code: api_1.SpanStatusCode.ERROR,
message: e.message,
});
// For any violation errors bubble them up to the
// called of the function so that they can
// be handled gracefully
if (e.name.includes('ViolationError')) {
(0, arvo_core_1.logToSpan)({
level: 'CRITICAL',
message: "Orchestrator violation error: ".concat(e.message),
});
throw e;
}
(0, arvo_core_1.logToSpan)({
level: 'ERROR',
message: "Orchestrator execution failed: ".concat(e.message),
});
parsedEventSubject = null;
try {
parsedEventSubject = arvo_core_1.ArvoOrchestrationSubject.parse(event.subject);
}
catch (e) {
(0, arvo_core_1.logToSpan)({
level: 'WARNING',
message: "Unable to parse event subject: ".concat(e.message),
});
}
result = [];
for (_g = 0, _h = Array.from(new Set(this.systemErrorDomain
? this.systemErrorDomain.map(function (item) {
return (0, ArvoDomain_1.resolveEventDomain)({
domainToResolve: item,
triggeringEvent: event,
handlerSelfContract: _this.registry.machines[0].contracts.self,
eventContract: _this.registry.machines[0].contracts.self,
});
})
: [event.domain, this.domain, null])); _g < _h.length; _g++) {
_dom = _h[_g];
result.push((0, arvo_core_1.createArvoOrchestratorEventFactory)(this.registry.machines[0].contracts.self).systemError({
source: this.source,
// If the initiator of the workflow exist then match the
// subject so that it can incorporate it in its state. If
// parent does not exist then this is the root workflow so
// use its own subject
subject: orchestrationParentSubject !== null && orchestrationParentSubject !== void 0 ? orchestrationParentSubject : event.subject,
// The system error must always go back to
// the source which initiated it
to: (_w = parsedEventSubject === null || parsedEventSubject === void 0 ? void 0 : parsedEventSubject.execution.initiator) !== null && _w !== void 0 ? _w : event.source,
error: e,
traceparent: (_x = otelHeaders.traceparent) !== null && _x !== void 0 ? _x : undefined,
tracestate: (_y = otelHeaders.tracestate) !== null && _y !== void 0 ? _y : undefined,
accesscontrol: (_z = event.accesscontrol) !== null && _z !== void 0 ? _z : undefined,
executionunits: this.executionunits,
// If there is initEventID then use that.
// Otherwise, use event id. If the error is in init event
// then it will be the same as initEventId. Otherwise,
// we still would know what cause this error
parentid: initEventId !== null && initEventId !== void 0 ? initEventId : event.id,
domain: _dom,
}));
for (_j = 0, _k = Object.entries(result[result.length - 1].otelAttributes); _j < _k.length; _j++) {
_l = _k[_j], key = _l[0], value = _l[1];
span.setAttribute("to_emit.".concat(result.length - 1, ".").concat(key), value);
}
}
return [2 /*return*/, {
events: result,
}];
case 6: return [4 /*yield*/, this.syncEventResource.releaseLock(event, acquiredLock, span)];
case 7:
_0.sent();
span.end();
return [7 /*endfinally*/];
case 8: return [2 /*return*/];
}
});
}); },
})];
case 1: return [2 /*return*/, _b.sent()];
}
});
});
};
Object.defineProperty(ArvoOrchestrator.prototype, "systemErrorSchema", {
/**
* Gets the error schema for this orchestrator
*/
get: function () {
return {
type: this.registry.machines[0].contracts.self.systemError.type,
schema: arvo_core_1.ArvoErrorSchema,
};
},
enumerable: false,
configurable: true
});
return ArvoOrchestrator;
}(AbstractArvoEventHandler_1.default));
exports.ArvoOrchestrator = ArvoOrchestrator;