Refactor service spawn and shutdown mechanism

This commit is contained in:
SrGooglo 2025-04-24 06:09:21 +00:00
parent 3753c8eae0
commit 4c78660c4c
2 changed files with 22 additions and 18 deletions

View File

@ -55,7 +55,8 @@ export default class Service {
this.instance = await spawnService({ this.instance = await spawnService({
id: this.id, id: this.id,
service: this.path, service: this,
path: this.path,
cwd: this.cwd, cwd: this.cwd,
onClose: this.handleClose.bind(this), onClose: this.handleClose.bind(this),
onError: this.handleError.bind(this), onError: this.handleError.bind(this),
@ -140,8 +141,7 @@ export default class Service {
// Kill the current process if is running // Kill the current process if is running
if (this.instance.exitCode === null) { if (this.instance.exitCode === null) {
console.log(`[${this.id}] Killing current process...`) await this.instance.kill()
await this.instance.kill("SIGKILL")
} }
// Start a new process // Start a new process
@ -153,17 +153,13 @@ export default class Service {
/** /**
* Stop the service * Stop the service
*/ */
async stop() { stop() {
console.log(`[${this.id}] Stopping service...`) console.log(`[${this.id}] Stopping service...`)
if (this.fileWatcher) { this.instance.kill()
await this.fileWatcher.close()
this.fileWatcher = null
}
if (this.instance) { if (this.fileWatcher) {
await this.instance.kill("SIGKILL") this.fileWatcher.close()
this.instance = null
} }
} }

View File

@ -3,21 +3,29 @@ import createServiceLogTransformer from "./createServiceLogTransformer"
import Vars from "../vars" import Vars from "../vars"
export default async ({ id, service, cwd, onClose, onError, onIPCData }) => { export default async ({
id,
service,
path,
cwd,
onClose,
onError,
onIPCData,
}) => {
const instanceEnv = { const instanceEnv = {
...process.env, ...process.env,
lb_service: { lb_service_id: service.id,
id: service.id, lb_service_path: service.path,
index: service.index, lb_service_version: service.version,
}, lb_service_cwd: service.cwd,
lb_service: true,
} }
let instance = ChildProcess.fork(Vars.bootloaderBin, [service], { let instance = ChildProcess.fork(Vars.bootloaderBin, [path], {
detached: false, detached: false,
silent: true, silent: true,
cwd: cwd, cwd: cwd,
env: instanceEnv, env: instanceEnv,
killSignal: "SIGTERM",
}) })
instance.logs = { instance.logs = {