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

View File

@ -3,21 +3,29 @@ import createServiceLogTransformer from "./createServiceLogTransformer"
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 = {
...process.env,
lb_service: {
id: service.id,
index: service.index,
},
lb_service_id: service.id,
lb_service_path: service.path,
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,
silent: true,
cwd: cwd,
env: instanceEnv,
killSignal: "SIGTERM",
})
instance.logs = {