UNPKG

lidex-mongo

Version:
186 lines (185 loc) 6.22 kB
"use strict"; 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.MongoPersistence = void 0; const mongodb_1 = require("mongodb"); class MongoPersistence { constructor(url) { const client = new mongodb_1.MongoClient(url); const db = client.db(); this.workflows = db.collection("workflows"); } init() { return __awaiter(this, void 0, void 0, function* () { yield this.workflows.createIndex({ id: 1 }, { unique: true }); yield this.workflows.createIndex({ status: 1 }); yield this.workflows.createIndex({ status: 1, timeoutAt: 1 }); }); } insert(workflowId, handler, input) { return __awaiter(this, void 0, void 0, function* () { try { yield this.workflows.insertOne({ id: workflowId, handler, input, status: "idle", }); return true; } catch (error) { const e = error; // Workflow already started, ignore. if (e.name === "MongoServerError" && e.code === 11000) { return false; } throw error; } }); } claim(now, timeoutAt) { return __awaiter(this, void 0, void 0, function* () { const workflow = yield this.workflows.findOneAndUpdate({ $or: [ { status: "idle", }, { status: { $in: ["running", "failed"] }, timeoutAt: { $lt: now }, }, ], }, { $set: { status: "running", timeoutAt, }, }, { projection: { _id: 0, id: 1, }, }); return workflow === null || workflow === void 0 ? void 0 : workflow.id; }); } findOutput(workflowId, stepId) { return __awaiter(this, void 0, void 0, function* () { const workflow = yield this.workflows.findOne({ id: workflowId, }, { projection: { _id: 0, [`steps.${stepId}`]: 1, }, }); if (workflow && workflow.steps) { return workflow.steps[stepId]; } return undefined; }); } findWakeUpAt(workflowId, napId) { return __awaiter(this, void 0, void 0, function* () { const workflow = yield this.workflows.findOne({ id: workflowId, }, { projection: { _id: 0, [`naps.${napId}`]: 1, }, }); if (workflow && workflow.naps) { return workflow.naps[napId]; } return undefined; }); } findRunData(workflowId) { return __awaiter(this, void 0, void 0, function* () { const workflow = yield this.workflows.findOne({ id: workflowId, }, { projection: { _id: 0, handler: 1, input: 1, failures: 1, }, }); if (workflow) { return workflow; } return undefined; }); } setAsFinished(workflowId) { return __awaiter(this, void 0, void 0, function* () { yield this.workflows.updateOne({ id: workflowId, }, { $set: { status: "finished" }, }); }); } findStatus(workflowId) { return __awaiter(this, void 0, void 0, function* () { const workflow = yield this.workflows.findOne({ id: workflowId, }, { projection: { _id: 0, status: 1, }, }); return workflow === null || workflow === void 0 ? void 0 : workflow.status; }); } updateStatus(workflowId, status, timeoutAt, failures, lastError) { return __awaiter(this, void 0, void 0, function* () { yield this.workflows.updateOne({ id: workflowId, }, { $set: { status, timeoutAt, failures, lastError, }, }); }); } updateOutput(workflowId, stepId, output, timeoutAt) { return __awaiter(this, void 0, void 0, function* () { yield this.workflows.updateOne({ id: workflowId, }, { $set: { [`steps.${stepId}`]: output, timeoutAt, }, }); }); } updateWakeUpAt(workflowId, napId, wakeUpAt, timeoutAt) { return __awaiter(this, void 0, void 0, function* () { yield this.workflows.updateOne({ id: workflowId, }, { $set: { [`naps.${napId}`]: wakeUpAt, timeoutAt, }, }); }); } } exports.MongoPersistence = MongoPersistence;