@chix/flow
Version:
358 lines • 16.1 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 (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
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;
return g = { next: verb(0), "throw": verb(1), "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 (_) 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 __values = (this && this.__values) || function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
Object.defineProperty(exports, "__esModule", { value: true });
var _debug = require("debug");
var events_1 = require("events");
var uuid_1 = require("uuid");
var events_2 = require("../events");
var port_1 = require("../port");
var debug = _debug('chix:pm');
var onExit = [];
if (typeof process !== 'undefined' && process.on) {
process.on('exit', function onExitHandlerProcessManager() {
onExit.forEach(function (instance) {
return instance.onExit.bind(instance)();
});
});
}
var noop = function (_processes, _that) { };
var ProcessManager = (function (_super) {
__extends(ProcessManager, _super);
function ProcessManager(options) {
if (options === void 0) { options = {}; }
var _this = _super.call(this) || this;
_this.processes = new Map();
_this.onExitHandler = noop;
_this.processErrorHandler = function (event) {
console.log('PM Error', event);
_this.emit(events_2.PMEvents.ERROR, event);
};
_this.onProcessStartHandler = function (event) {
debug('Process started ' + event.node.identifier);
_this.emit(events_2.PMEvents.START_PROCESS, event.node);
};
_this.onProcessStopHandler = function (event) {
debug('Process stopped ' + event.node.identifier);
_this.emit(events_2.PMEvents.STOP_PROCESS, event.node);
};
_this.onProcessStatusHandler = function (event) {
debug('Changed status ' + event.node.identifier + ' to ' + event.status);
_this.emit(events_2.PMEvents.PROCESS_STATUS, event);
};
if (options.onExit) {
_this.onExitHandler = options.onExit;
}
onExit.push(_this);
return _this;
}
ProcessManager.prototype.onExit = function () {
var processes = Array.from(this.processes.values()).reverse();
this.onExitHandler(processes, this);
};
ProcessManager.prototype.hasMainGraph = function () {
return this.getMainGraphs().length > 0;
};
ProcessManager.prototype.getMainGraph = function () {
var main = this.getMainGraphs().pop();
if (!main) {
throw Error('Could not find main actor');
}
return main;
};
ProcessManager.prototype.getMainGraphs = function () {
var e_1, _a;
var main = [];
try {
for (var _b = __values(this.processes.values()), _c = _b.next(); !_c.done; _c = _b.next()) {
var process_1 = _c.value;
if (process_1.type === 'flow' && !process_1.hasParent()) {
main.push(process_1);
}
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_1) throw e_1.error; }
}
return main;
};
ProcessManager.prototype.register = function (node) {
if (node.pid) {
throw new Error('Refusing to add node with existing process id');
}
var pid = uuid_1.v4();
node.setPid(pid);
this.processes.set(pid, node);
node.on(events_2.NodeEvents.START.name, this.onProcessStartHandler);
node.on(events_2.NodeEvents.STOP.name, this.onProcessStopHandler);
node.on(events_2.NodeEvents.STATUS_UPDATE.name, this.onProcessStatusHandler);
node.on(events_2.NodeEvents.ERROR.name, this.processErrorHandler);
debug('Registered ' + node.identifier);
this.emit(events_2.PMEvents.ADD_PROCESS, node);
};
ProcessManager.prototype.changePid = function (from, to) {
if (this.processes.has(from)) {
this.processes.set(to, this.processes.get(from));
this.processes.delete(from);
}
else {
throw Error('Process id not found');
}
this.emit(events_2.PMEvents.CHANGE_PID, {
from: from,
to: to,
});
};
ProcessManager.prototype.getProcess = function (pid) {
if (this.processes.has(pid)) {
return this.processes.get(pid);
}
throw Error('Process id not found');
};
ProcessManager.prototype.getProcesses = function () {
return Array.from(this.processes.values());
};
ProcessManager.prototype.start = function (node) {
var pid = typeof node === 'object' ? node.pid : node;
var process = this.getProcess(pid);
if (process.type === 'flow') {
process.start();
}
else {
process.release();
}
};
ProcessManager.prototype.stop = function (node) {
return __awaiter(this, void 0, void 0, function () {
var pid, process;
return __generator(this, function (_a) {
pid = typeof node === 'object' ? node.pid : node;
process = this.getProcess(pid);
if (process.type === 'flow') {
return [2, process.stop()];
}
return [2, process.hold()];
});
});
};
ProcessManager.prototype.unregister = function (node) {
return __awaiter(this, void 0, void 0, function () {
var process;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!node.pid) {
throw new Error('Process id not found');
}
process = this.getProcess(node.pid);
if (!(process.type === 'flow')) return [3, 2];
return [4, this.stop(node)];
case 1:
_a.sent();
return [3, 4];
case 2: return [4, node.shutdown()];
case 3:
_a.sent();
_a.label = 4;
case 4:
node.removeListener(events_2.NodeEvents.START.name, this.onProcessStartHandler);
node.removeListener(events_2.NodeEvents.STOP.name, this.onProcessStopHandler);
node.removeListener(events_2.NodeEvents.STATUS_UPDATE.name, this.onProcessStatusHandler);
node.removeListener(events_2.NodeEvents.ERROR.name, this.processErrorHandler);
this.processes.delete(node.pid);
delete node.pid;
this.emit(events_2.PMEvents.REMOVE_PROCESS, node);
debug('Unregistered ' + node.identifier);
return [2, node];
}
});
});
};
ProcessManager.prototype.get = function (pid) {
return this.processes.get(pid);
};
ProcessManager.prototype.getById = function (id, actor) {
return this.findBy('id', id, actor);
};
ProcessManager.prototype.findBy = function (prop, value, actor) {
var e_2, _a;
var found;
try {
for (var _b = __values(this.processes.values()), _c = _b.next(); !_c.done; _c = _b.next()) {
var process_2 = _c.value;
if (process_2[prop] === value && (!actor || actor.hasNode(process_2.id))) {
if (found) {
console.log(this.processes);
throw Error('conflict: multiple ' + prop + 's matching ' + value);
}
found = process_2;
}
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_2) throw e_2.error; }
}
return found;
};
ProcessManager.prototype.getStalledPorts = function () {
var e_3, _a, e_4, _b, e_5, _c;
var nodes = Array.from(this.getMainGraph().nodes.values()).filter(function (node) { return node.runCount === 0 && node.isStartable; });
var ports = [];
try {
for (var nodes_1 = __values(nodes), nodes_1_1 = nodes_1.next(); !nodes_1_1.done; nodes_1_1 = nodes_1.next()) {
var node = nodes_1_1.value;
var inputPorts = Array.from(Object.values(node.ports.input));
try {
for (var inputPorts_1 = (e_4 = void 0, __values(inputPorts)), inputPorts_1_1 = inputPorts_1.next(); !inputPorts_1_1.done; inputPorts_1_1 = inputPorts_1.next()) {
var port = inputPorts_1_1.value;
var status_1 = port.isFilled();
if (status_1 === port_1.Port.EMPTY) {
var waiting = void 0;
if (port.hasConnections()) {
var connections = port.getConnections();
try {
for (var connections_1 = (e_5 = void 0, __values(connections)), connections_1_1 = connections_1.next(); !connections_1_1.done; connections_1_1 = connections_1.next()) {
var connection = connections_1_1.value;
var source = this.get(connection.source.pid);
if (source.runCount === 0) {
waiting = true;
break;
}
}
}
catch (e_5_1) { e_5 = { error: e_5_1 }; }
finally {
try {
if (connections_1_1 && !connections_1_1.done && (_c = connections_1.return)) _c.call(connections_1);
}
finally { if (e_5) throw e_5.error; }
}
}
if (!waiting) {
var _d = port.parent, identifier = _d.identifier, ns = _d.ns, name_1 = _d.name;
ports.push({
node: identifier,
ns: ns,
name: name_1,
status: node.getStatus(),
port: port.name,
required: port.required,
context: port.hasContext(),
persist: port.hasPersist(),
default: port.default,
});
}
}
}
}
catch (e_4_1) { e_4 = { error: e_4_1 }; }
finally {
try {
if (inputPorts_1_1 && !inputPorts_1_1.done && (_b = inputPorts_1.return)) _b.call(inputPorts_1);
}
finally { if (e_4) throw e_4.error; }
}
}
}
catch (e_3_1) { e_3 = { error: e_3_1 }; }
finally {
try {
if (nodes_1_1 && !nodes_1_1.done && (_a = nodes_1.return)) _a.call(nodes_1);
}
finally { if (e_3) throw e_3.error; }
}
return ports;
};
ProcessManager.prototype.filterByStatus = function (status) {
return this.filterBy('status', status);
};
ProcessManager.prototype.filterBy = function (prop, value) {
var e_6, _a;
var filtered = [];
try {
for (var _b = __values(this.processes.values()), _c = _b.next(); !_c.done; _c = _b.next()) {
var process_3 = _c.value;
if (process_3[prop] === value) {
filtered.push(process_3);
}
}
}
catch (e_6_1) { e_6 = { error: e_6_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_6) throw e_6.error; }
}
return filtered;
};
return ProcessManager;
}(events_1.EventEmitter));
exports.ProcessManager = ProcessManager;
//# sourceMappingURL=manager.js.map