UNPKG

raas-server

Version:
96 lines 3.9 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { 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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const raas_core_1 = require("raas-core"); const runtimeConnector_1 = require("./runtimeConnector"); class RuntimeController { constructor(handle, server, notificationService) { this.onShutdown = new raas_core_1.rhamtEvents.TypedEvent(); this.handle = handle; this.server = server; this.notificationService = notificationService; } init() { const shutdown = this.shutdown.bind(this); this.server.on('exit', shutdown); this.server.once('error', shutdown); } analyze(config) { this.analysis = { id: raas_core_1.RhamtModelService.generateUniqueId(), state: raas_core_1.Protocol.ServerState.UNKNOWN, analysisConfiguration: config }; this.doAnalyze(config, this.analysis).then(() => { this.analysis.state = raas_core_1.Protocol.ServerState.ANALYZING; this.notificationService.onAnalysisStarted(this.analysis); }).catch((e) => { console.log(`messenger error: name (${e.name}) msg (${e.message}) stack (${e.stack}). shutting down server.`); this.shutdown(); }); } doAnalyze(config, analysis) { return new Promise((resolve, reject) => { this.connect(config, analysis).then(this.prepPayload.bind(this)).then(result => { result.stream.send('rhamt.server', result.payload, (error, message) => { if (!error) { resolve(); } else { result.stream.close(); reject(); } }); }); }); } connect(config, analysis) { return __awaiter(this, void 0, void 0, function* () { return runtimeConnector_1.RuntimeConnector.connect(this.handle.runConfiguration).then(stream => { const handler = (error, msg) => this.notificationService.onMessage({ analysis, error, msg }); stream.registerHandler('rhamt.client', {}, handler.bind(this)); return { stream, analysis, config }; }); }); } prepPayload(data) { const input = new Array(); data.config.input.forEach(entry => { input.push({ location: entry }); }); const sources = new Array(); const targets = new Array(); data.config.options.forEach(option => { if (option.name === 'source') { sources.push({ source: option.value }); } else if (option.name === 'target') { targets.push({ target: option.value }); } }); const payload = { input, output: data.config.output, start: true, sources, targets, options: data.config.options }; return { stream: data.stream, payload }; } shutdown() { if (this.server.pid && !this.server.killed) { this.server.kill(); } this.onShutdown.emit(undefined); } } exports.RuntimeController = RuntimeController; //# sourceMappingURL=runtimeController.js.map