UNPKG

rhamt-client

Version:
158 lines (157 loc) 6.76 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 cp = require("child_process"); const os = require("os"); const EventBus = require("vertx3-eventbus-client"); const rhamt_core_1 = require("rhamt-core"); const SERVER_STARTED_REGEX = /.*rhamt server listening on (.*)/; //const DURATION_REGEX = /RHAMT execution took (.*)/; const SERVER_START_TIMEOUT_MS = 20000; class RhamtClient { constructor() { this.isServerRunnig = false; } start(runtimeConfiguration) { return __awaiter(this, void 0, void 0, function* () { this.runtimeConfiguration = runtimeConfiguration; return this.startRhamtServer(); }); } cancel() { return new Promise((resolve, reject) => { if (this.bus) { this.bus.send('rhamt.server', { 'stop': true }, (error, message) => { console.log('message after stopping: ' + JSON.stringify(message)); console.log('error after stopping: ' + JSON.stringify(error)); if (!error) { //this.runConfiguration!.monitor.setCancelled(); resolve(); } else { reject('error occurred while trying to stop analysis: ' + JSON.stringify(error)); } }); } }); } analyze(config) { // TODO: Best way to make sure we no longer receive events // from a previous analysis that's running. // TODO: Send a stop event to server, then on callback we start. return new Promise((resolve, reject) => { if (!this.isRunning()) { reject('rhamt-client not running.'); } else { let input = []; config.input.forEach(entry => { input.push({ 'location': entry }); }); const load = { 'input': input, 'output': config.output, 'start': true }; this.bus.send('rhamt.server', load, (error, message) => { console.log('message after sending: ' + JSON.stringify(message)); console.log('error after sending: ' + JSON.stringify(error)); if (!error) { this.runConfiguration = config; resolve(); } else { reject('rhamt server error: ' + JSON.stringify(error)); } }); console.log('rhamt-client sent the analyze request.'); } }); } isRunning() { return this.isServerRunnig; } terminate() { this.isServerRunnig = false; console.log('attemting to terminate server process...'); if (this.serverProcess && !this.serverProcess.killed) { console.log('terminating server process...'); process.kill(this.serverProcess.pid); console.log('rhamt-client server terminated'); return true; } else { console.log('rhamt-client server is not running'); } return false; } onRhamtExit() { this.isServerRunnig = false; if (this.bus) { this.bus.close(); this.bus = undefined; } if (this.runtimeConfiguration) { this.runtimeConfiguration.onStopped.emit(new rhamt_core_1.RhamtEvent()); } } startRhamtServer() { return new Promise((resolve, reject) => { this.isServerRunnig = true; let started = false; let connected = false; this.serverProcess = this.spawn(); this.serverProcess.once('error', this.onRhamtExit.bind(this)); this.serverProcess.on('exit', this.onRhamtExit.bind(this)); const outputListener = (data) => { const line = data.toString(); console.log('attempting to match server output string: ' + line); const match = SERVER_STARTED_REGEX.exec(line); if (match) { console.log('rhamt-client server started.'); console.log('setting up sockets.'); started = true; if (this.serverProcess) { this.serverProcess.stdout.removeListener('data', outputListener); } this.bus = new EventBus(`http://localhost:${this.runtimeConfiguration.port}/eventbus`); this.bus.onopen = () => { console.log('rhamt-client sockets connected.'); console.log('attemtping to setup progress monitor...'); this.bus.registerHandler('rhamt.client', {}, this.handleMessage.bind(this)); connected = true; this.isServerRunnig = true; this.runtimeConfiguration.onStarted.emit(new rhamt_core_1.RhamtEvent()); resolve(); }; console.log('rhamt-client finished trying to setup sockets.'); } }; this.serverProcess.stdout.addListener('data', outputListener); setTimeout(() => { if (!started && !connected) { console.log('rhamt-client server startup timeout.'); this.terminate(); reject(); } }, SERVER_START_TIMEOUT_MS); }); } spawn() { console.log('rhamt-client using ' + JSON.stringify(this.runtimeConfiguration)); return cp.spawn(this.runtimeConfiguration.rhamtCli, ["--startServer", String(this.runtimeConfiguration.port)], { cwd: os.homedir() }); } handleMessage(err, msg) { if (this.runConfiguration) { this.runConfiguration.handleMessage(err, msg); } } } exports.RhamtClient = RhamtClient;