UNPKG

@remote.it/core

Version:

Core remote.it JavasScript/TypeScript library

114 lines 4.87 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var ConfigFile_1 = require("./ConfigFile"); var FileWatcher_1 = require("./FileWatcher"); var ServiceProcessManager_1 = require("./ServiceProcessManager"); var SChannelListener_1 = require("./SChannelListener"); var TargetProcessManager_1 = require("./TargetProcessManager"); var UDPEventBroadcaster_1 = require("./UDPEventBroadcaster"); var Logger_1 = require("./Logger"); var SERVICE_EVENTS = [ 'adding', 'added', 'starting', 'started', 'restarting', 'restarted', 'stopping', 'stopped', ]; var TARGET_EVENTS = [ 'adding', 'added', 'starting', 'started', 'restarting', 'restarted', 'stopping', 'stopped', ]; var ProcessSupervisor = /** @class */ (function () { function ProcessSupervisor(configPath, broadcastPort) { if (broadcastPort === void 0) { broadcastPort = 55000; } this.configPath = configPath; this.broadcastPort = broadcastPort; this.broadcaster = this.configureUDPEventBroadcaster(); Logger_1.Logger.info('watching file:', { configPath: configPath }); this.config = new ConfigFile_1.ConfigFile(configPath); this.target = this.manageTargetProcess(); this.services = this.manageServiceProcesses(); this.watchConfigFile(); this.listenToSChannel(); Logger_1.Logger.info('process supervisor started'); } ProcessSupervisor.prototype.configureUDPEventBroadcaster = function () { var broadcaster = new UDPEventBroadcaster_1.UDPEventBroadcaster(this.broadcastPort); broadcaster.broadcast('started:' + this.broadcastPort); return broadcaster; }; ProcessSupervisor.prototype.manageTargetProcess = function () { var _this = this; var target = new TargetProcessManager_1.TargetProcessManager(); TARGET_EVENTS.map(function (e) { return target.on(e, function (uid) { return _this.broadcaster.broadcast("target/" + e + "/" + uid); }); }); target.update(this.config.target); return target; }; ProcessSupervisor.prototype.manageServiceProcesses = function () { var _this = this; var services = new ServiceProcessManager_1.ServiceProcessManager(); SERVICE_EVENTS.map(function (e) { return services.on(e, function (uid) { return _this.broadcaster.broadcast("service/" + e + "/" + uid); }); }); services.update(this.config.services); return services; }; ProcessSupervisor.prototype.watchConfigFile = function () { var _this = this; var watcher = new FileWatcher_1.FileWatcher(this.configPath); watcher.on('ready', function (path) { return Logger_1.Logger.info('watcher ready', { path: path }); }); watcher.on('created', function (path) { return Logger_1.Logger.info('file created', { path: path }); }); watcher.on('updated', function () { _this.services.update(_this.config.services); _this.target.update(_this.config.target); // initiators.update(config.initiators) }); watcher.on('removed', function (path) { return Logger_1.Logger.info('file removed', { path: path }); }); watcher.on('error', function (error) { return Logger_1.Logger.error('watcher error', { error: error }); }); }; ProcessSupervisor.prototype.listenToSChannel = function () { var _this = this; var schannel = new SChannelListener_1.SChannelListener(); schannel.on('ready', function () { Logger_1.Logger.info('schannel listener ready'); _this.broadcastStatus(); }); schannel.on('error', function (error) { return Logger_1.Logger.info('schannel listener error:', { error: error }); }); schannel.on('message', function (message) { Logger_1.Logger.info('schannel listener message:', { message: message }); if (message === 'status') _this.broadcastStatus(); }); schannel.on('closed', function () { return Logger_1.Logger.info('schannel listener closed'); }); schannel.listen(); }; ProcessSupervisor.prototype.broadcastStatus = function () { this.broadcaster.broadcast('status:' + JSON.stringify({ target: this.target.processes.map(function (p) { return ({ pid: p.process.pid, uid: p.config.uid, }); }), services: this.services.processes.map(function (p) { return ({ pid: p.process.pid, uid: p.config.uid, }); }), })); }; return ProcessSupervisor; }()); exports.ProcessSupervisor = ProcessSupervisor; //# sourceMappingURL=ProcessSupervisor.js.map