2018-08-10 21:10:48 +00:00
|
|
|
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';
|
2016-07-24 23:54:36 +00:00
|
|
|
|
2019-01-09 11:15:28 +00:00
|
|
|
import { ChildProcess } from 'child_process';
|
|
|
|
|
2016-08-02 13:32:06 +00:00
|
|
|
/**
|
|
|
|
* manages a nginxprocess for an NginxConfig
|
|
|
|
*/
|
2016-07-24 23:54:36 +00:00
|
|
|
export class NginxProcess {
|
2019-01-09 11:15:28 +00:00
|
|
|
public started: boolean = false;
|
|
|
|
public smartNginxRef: SmartNginx;
|
|
|
|
private nginxChildProcess: ChildProcess;
|
|
|
|
private smartshellInstance = new Smartshell({
|
2018-08-10 21:10:48 +00:00
|
|
|
executor: 'bash'
|
|
|
|
});
|
2019-01-09 11:15:28 +00:00
|
|
|
|
|
|
|
constructor(nginxRefArg: SmartNginx) {
|
|
|
|
this.smartNginxRef = nginxRefArg;
|
2018-08-10 21:10:48 +00:00
|
|
|
}
|
2016-07-24 23:54:36 +00:00
|
|
|
|
2018-08-10 21:10:48 +00:00
|
|
|
/**
|
|
|
|
* start nginx
|
|
|
|
*/
|
2019-01-09 11:15:28 +00:00
|
|
|
public async start() {
|
|
|
|
if (!this.nginxChildProcess) {
|
|
|
|
this.nginxChildProcess = (await this.smartshellInstance.execStreaming(
|
|
|
|
`nginx -c ${paths.nginxConfFile}`
|
|
|
|
)).childProcess;
|
2018-08-10 21:10:48 +00:00
|
|
|
}
|
|
|
|
this.started = true;
|
2019-01-01 21:34:36 +00:00
|
|
|
plugins.smartlog.defaultLogger.log('info', 'started Nginx!');
|
2018-08-10 21:10:48 +00:00
|
|
|
}
|
2016-07-24 23:54:36 +00:00
|
|
|
|
2018-08-10 21:10:48 +00:00
|
|
|
/**
|
|
|
|
* reload config
|
|
|
|
*/
|
2019-01-09 11:15:28 +00:00
|
|
|
public async reloadConfig() {
|
|
|
|
if (!this.started) {
|
2018-08-10 21:10:48 +00:00
|
|
|
this.start();
|
|
|
|
} else {
|
2019-01-09 11:15:28 +00:00
|
|
|
await this.smartshellInstance.exec('nginx -s reload');
|
2018-08-10 21:10:48 +00:00
|
|
|
}
|
2019-01-09 11:15:28 +00:00
|
|
|
this.smartNginxRef.logger.log('info', 'NginxProcess has loaded the new config!');
|
2018-08-10 21:10:48 +00:00
|
|
|
}
|
2016-07-24 23:54:36 +00:00
|
|
|
|
2018-08-10 21:10:48 +00:00
|
|
|
/**
|
|
|
|
* stop the nginx instance
|
|
|
|
*/
|
2019-01-09 11:15:28 +00:00
|
|
|
public async stop() {
|
|
|
|
if (this.nginxChildProcess) {
|
2018-08-10 21:10:48 +00:00
|
|
|
this.smartshellInstance.exec('nginx -s quit');
|
|
|
|
this.started = false;
|
2019-01-09 11:15:28 +00:00
|
|
|
this.smartNginxRef.logger.log('info', 'stopped Nginx!');
|
2018-08-10 21:10:48 +00:00
|
|
|
} else {
|
2019-01-09 11:15:28 +00:00
|
|
|
this.smartNginxRef.logger.log('info', 'nginx already stopped!');
|
2018-08-10 21:10:48 +00:00
|
|
|
}
|
|
|
|
}
|
2016-07-24 23:54:36 +00:00
|
|
|
|
2018-08-10 21:10:48 +00:00
|
|
|
/**
|
|
|
|
* checks if nginx is in path
|
|
|
|
*/
|
2019-01-09 11:15:28 +00:00
|
|
|
public check(): boolean {
|
2018-08-10 21:10:48 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-07-24 23:54:36 +00:00
|
|
|
}
|