smartnginx/ts/smartnginx.classes.nginxprocess.ts

83 lines
2.1 KiB
TypeScript

import * as plugins from './smartnginx.plugins';
import * as paths from './smartnginx.paths';
import { SmartNginx } from './smartnginx.classes.smartnginx';
import { NginxHost } from './smartnginx.classes.nginxhost';
import { Smartshell } from '@pushrocks/smartshell';
/**
* manages a nginxprocess for an NginxConfig
*/
export class NginxProcess {
started: boolean = false;
nginxConfig: SmartNginx;
nginxChildProcess: plugins.childProcess.ChildProcess;
smartshellInstance = new Smartshell({
executor: 'bash'
});
constructor(nginxConfigArg) {
this.nginxConfig = nginxConfigArg;
}
/**
* start nginx
*/
start() {
let done = plugins.smartpromise.defer();
if (typeof this.nginxChildProcess == 'undefined') {
this.nginxChildProcess = plugins.childProcess.exec(
`nginx -c ${paths.nginxConfFile}`,
function(error, stdout, stderr) {
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
if (error !== null) {
console.log(`exec error: ${error}`);
}
}
);
}
this.started = true;
plugins.smartlog.defaultLogger.info('started Nginx!');
done.resolve();
return done.promise;
}
/**
* reload config
*/
reloadConfig() {
let done = plugins.smartpromise.defer();
if (this.started == false) {
this.start();
} else {
this.smartshellInstance.exec('nginx -s reload');
}
plugins.smartlog.defaultLogger.info('NginxProcess has loaded the new config!');
done.resolve();
return done.promise;
}
/**
* stop the nginx instance
*/
stop() {
let done = plugins.smartpromise.defer();
if (typeof this.nginxChildProcess != 'undefined') {
this.smartshellInstance.exec('nginx -s quit');
this.started = false;
plugins.smartlog.defaultLogger.info('stopped Nginx!');
} else {
plugins.smartlog.defaultLogger.info('nginx already stopped!');
}
done.resolve();
return done.promise;
}
/**
* checks if nginx is in path
*/
check(): boolean {
return;
}
}