deepify
Version:
DEEP Development Tools
151 lines (113 loc) • 3.44 kB
JavaScript
/**
* Created by CCristi on 5/16/16.
*/
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.BinaryLauncher = undefined;
var _AbstractLauncher = require('./AbstractLauncher');
var _Exec = require('../../Helpers/Exec');
var _MissingElasticsearchBinaryException = require('../Exception/MissingElasticsearchBinaryException');
var _FailedToLauchElasticsearchException = require('../Exception/FailedToLauchElasticsearchException');
var _ServerAlreadyRunningException = require('../Exception/ServerAlreadyRunningException');
var _path = require('path');
var _path2 = _interopRequireDefault(_path);
var _fs = require('fs');
var _fs2 = _interopRequireDefault(_fs);
var _lockfile = require('lockfile');
var _lockfile2 = _interopRequireDefault(_lockfile);
var _process = require('process');
var _process2 = _interopRequireDefault(_process);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
class BinaryLauncher extends _AbstractLauncher.AbstractLauncher {
/**
* @param {String} binaryPath
*/
constructor(binaryPath) {
super();
this._binaryPath = binaryPath;
this._pid = null;
}
/**
* @returns {BinaryLauncher}
* @private
*/
_launch() {
this._assureBinary();
this._lock();
let pidFile = this._pidFile;
let launchCmd = new _Exec.Exec(this._binaryPath, '--daemonize', `--pidfile=${pidFile}`, `--network.host=${this.hostname}`, `--http.port=${this.port}`);
for (let setting in this.settings) {
if (!this.settings.hasOwnProperty(setting)) {
continue;
}
let settingVal = this.settings[setting];
launchCmd.addArg(`--${setting}=${settingVal}`);
}
launchCmd.runSync();
if (launchCmd.failed) {
throw new _FailedToLauchElasticsearchException.FailedToLauchElasticsearchException(this, launchCmd.error);
}
this._pid = _fs2.default.readFileSync(pidFile).toString();
return this;
}
/**
* @returns {Boolean}
* @private
*/
_stop() {
if (this._pid) {
this._unlock();
return _process2.default.kill(this._pid, 'SIGTERM');
}
return false;
}
/**
* @private
*/
_assureBinary() {
if (!(_fs2.default.existsSync(this._binaryPath) && _fs2.default.lstatSync(this._binaryPath).isFile())) {
throw new _MissingElasticsearchBinaryException.MissingElasticsearchBinaryException(this._binaryPath);
}
}
/**
* @private
*/
_lock() {
let pidFile = this._pidFile;
try {
_lockfile2.default.lockSync(pidFile);
} catch (e) {
if (_fs2.default.existsSync(pidFile)) {
let pid = _fs2.default.readFileSync(pidFile).toString();
if (pid) {
try {
_process2.default.kill(pid, 0);
} catch (e) {
if (e.code === 'ESRCH') {
// process not found
_fs2.default.unlinkSync(pidFile);
this._lock();
return;
}
}
}
}
throw new _ServerAlreadyRunningException.ServerAlreadyRunningException(this, e);
}
}
/**
* @private
*/
_unlock() {
_lockfile2.default.unlockSync(this._pidFile);
}
/**
* @returns {String}
*/
get _pidFile() {
return _path2.default.join(_path2.default.dirname(this._binaryPath), `_es.${this.hostname}.${this.port}.pid`);
}
}
exports.BinaryLauncher = BinaryLauncher;