lidex-mongo
Version:
MongoDB persistence for Lidex
206 lines (205 loc) • 6.75 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.makeMongoPersistence = makeMongoPersistence;
const mongodb_1 = require("mongodb");
function makeMongoPersistence(url) {
const client = new mongodb_1.MongoClient(url);
const db = client.db();
const workflows = db.collection("workflows");
function init() {
return __awaiter(this, void 0, void 0, function* () {
yield workflows.createIndex({ id: 1 }, { unique: true });
yield workflows.createIndex({ status: 1 });
yield workflows.createIndex({ status: 1, timeoutAt: 1 });
});
}
function insert(workflowId, handler, input) {
return __awaiter(this, void 0, void 0, function* () {
try {
yield 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;
}
});
}
function claim(now, timeoutAt) {
return __awaiter(this, void 0, void 0, function* () {
const workflow = yield 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;
});
}
function findOutput(workflowId, stepId) {
return __awaiter(this, void 0, void 0, function* () {
const workflow = yield workflows.findOne({
id: workflowId,
}, {
projection: {
_id: 0,
[`steps.${stepId}`]: 1,
},
});
if (workflow && workflow.steps) {
return workflow.steps[stepId];
}
return undefined;
});
}
function findWakeUpAt(workflowId, napId) {
return __awaiter(this, void 0, void 0, function* () {
const workflow = yield workflows.findOne({
id: workflowId,
}, {
projection: {
_id: 0,
[`naps.${napId}`]: 1,
},
});
if (workflow && workflow.naps) {
return workflow.naps[napId];
}
return undefined;
});
}
function findRunData(workflowId) {
return __awaiter(this, void 0, void 0, function* () {
const workflow = yield workflows.findOne({
id: workflowId,
}, {
projection: {
_id: 0,
handler: 1,
input: 1,
failures: 1,
},
});
if (workflow) {
return {
handler: workflow.handler,
input: workflow.input,
failures: workflow.failures,
};
}
return undefined;
});
}
function setAsFinished(workflowId) {
return __awaiter(this, void 0, void 0, function* () {
yield workflows.updateOne({
id: workflowId,
}, {
$set: { status: "finished" },
});
});
}
function findStatus(workflowId) {
return __awaiter(this, void 0, void 0, function* () {
const workflow = yield workflows.findOne({
id: workflowId,
}, {
projection: {
_id: 0,
status: 1,
},
});
return workflow === null || workflow === void 0 ? void 0 : workflow.status;
});
}
function updateStatus(workflowId, status, timeoutAt, failures, lastError) {
return __awaiter(this, void 0, void 0, function* () {
yield workflows.updateOne({
id: workflowId,
}, {
$set: {
status,
timeoutAt,
failures,
lastError,
},
});
});
}
function updateOutput(workflowId, stepId, output, timeoutAt) {
return __awaiter(this, void 0, void 0, function* () {
yield workflows.updateOne({
id: workflowId,
}, {
$set: {
[`steps.${stepId}`]: output,
timeoutAt,
},
});
});
}
function updateWakeUpAt(workflowId, napId, wakeUpAt, timeoutAt) {
return __awaiter(this, void 0, void 0, function* () {
yield workflows.updateOne({
id: workflowId,
}, {
$set: {
[`naps.${napId}`]: wakeUpAt,
timeoutAt,
},
});
});
}
function terminate() {
return __awaiter(this, void 0, void 0, function* () {
yield client.close();
});
}
return {
init,
insert,
claim,
findOutput,
findWakeUpAt,
findRunData,
setAsFinished,
findStatus,
updateStatus,
updateOutput,
updateWakeUpAt,
terminate,
};
}
;