smartnginx/ts/smartnginx.classes.nginxprocess.ts

70 lines
1.7 KiB
TypeScript
Raw Normal View History

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';
/**
* 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({
executor: 'bash'
});
2019-01-09 11:15:28 +00:00
constructor(nginxRefArg: SmartNginx) {
this.smartNginxRef = nginxRefArg;
}
2016-07-24 23:54:36 +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;
}
this.started = true;
2019-01-01 21:34:36 +00:00
plugins.smartlog.defaultLogger.log('info', 'started Nginx!');
}
2016-07-24 23:54:36 +00:00
/**
* reload config
*/
2019-01-09 11:15:28 +00:00
public async reloadConfig() {
if (!this.started) {
this.start();
} else {
2019-01-09 11:15:28 +00:00
await this.smartshellInstance.exec('nginx -s reload');
}
2019-01-09 11:15:28 +00:00
this.smartNginxRef.logger.log('info', 'NginxProcess has loaded the new config!');
}
2016-07-24 23:54:36 +00:00
/**
* stop the nginx instance
*/
2019-01-09 11:15:28 +00:00
public async stop() {
if (this.nginxChildProcess) {
this.smartshellInstance.exec('nginx -s quit');
this.started = false;
2019-01-09 11:15:28 +00:00
this.smartNginxRef.logger.log('info', 'stopped Nginx!');
} else {
2019-01-09 11:15:28 +00:00
this.smartNginxRef.logger.log('info', 'nginx already stopped!');
}
}
2016-07-24 23:54:36 +00:00
/**
* checks if nginx is in path
*/
2019-01-09 11:15:28 +00:00
public check(): boolean {
return;
}
2016-07-24 23:54:36 +00:00
}