durable-functions
Version:
Durable Functions library for Node.js Azure Functions
56 lines • 3.65 kB
JavaScript
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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Orchestrator = void 0;
const DurableOrchestrationContext_1 = require("./DurableOrchestrationContext");
const TaskOrchestrationExecutor_1 = require("./TaskOrchestrationExecutor");
const ReplaySchema_1 = require("./ReplaySchema");
const DurableOrchestrationBindingInfo_1 = require("./DurableOrchestrationBindingInfo");
const HistoryEventType_1 = require("../history/HistoryEventType");
const Utils_1 = require("../util/Utils");
class Orchestrator {
constructor(fn) {
this.fn = fn;
}
listen() {
return this.handle.bind(this);
}
handle(orchestrationTrigger, context) {
return __awaiter(this, void 0, void 0, function* () {
this.taskOrchestrationExecutor = new TaskOrchestrationExecutor_1.TaskOrchestrationExecutor();
const orchestrationBinding = Utils_1.Utils.getInstancesOf({ trigger: orchestrationTrigger }, new DurableOrchestrationBindingInfo_1.DurableOrchestrationBindingInfoReqFields())[0];
if (!orchestrationBinding) {
throw new Error("Could not find an orchestrationClient binding on context.");
}
const state = orchestrationBinding.history;
const input = orchestrationBinding.input;
const instanceId = orchestrationBinding.instanceId;
let upperSchemaVersion;
const extensionUpperSchemaVersion = orchestrationBinding.upperSchemaVersionNew
? orchestrationBinding.upperSchemaVersionNew
: orchestrationBinding.upperSchemaVersion;
if (Object.values(ReplaySchema_1.ReplaySchema).includes(extensionUpperSchemaVersion)) {
upperSchemaVersion = extensionUpperSchemaVersion;
}
else {
upperSchemaVersion = ReplaySchema_1.LatestReplaySchema;
}
const decisionStartedEvent = Utils_1.Utils.ensureNonNull(state.find((e) => e.EventType === HistoryEventType_1.HistoryEventType.OrchestratorStarted), "The orchestrator can not execute without an OrchestratorStarted event.");
this.currentUtcDateTime = new Date(decisionStartedEvent.Timestamp);
if (context.df === undefined) {
context.df = new DurableOrchestrationContext_1.DurableOrchestrationContext(state, instanceId, this.currentUtcDateTime, orchestrationBinding.isReplaying, orchestrationBinding.parentInstanceId, orchestrationBinding.longRunningTimerIntervalDuration, orchestrationBinding.maximumShortTimerDuration, orchestrationBinding.defaultHttpAsyncRequestSleepTimeMillseconds, upperSchemaVersion, input, this.taskOrchestrationExecutor);
}
return yield this.taskOrchestrationExecutor.execute(context, state, upperSchemaVersion, this.fn);
});
}
}
exports.Orchestrator = Orchestrator;
//# sourceMappingURL=Orchestrator.js.map
;