smartnginx/ts/smartnginx.classes.nginxprocess.ts

68 lines
1.6 KiB
TypeScript
Raw Normal View History

2023-07-26 14:05:53 +00:00
import * as plugins from './smartnginx.plugins.js';
import * as paths from './smartnginx.paths.js';
import { SmartNginx } from './smartnginx.classes.smartnginx.js';
import { NginxHost } from './smartnginx.classes.nginxhost.js';
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;
2023-07-26 14:05:53 +00:00
private smartshellInstance = new plugins.smartshell.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) {
2023-07-26 14:05:53 +00:00
this.nginxChildProcess = (
await this.smartshellInstance.execStreaming(`nginx -c ${paths.nginxConfFile}`)
).childProcess;
}
this.started = true;
2023-07-26 14:05:53 +00:00
console.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
}