| 41 | /** Manages a single process. Wrapper around node's ChildProcess. */ |
| 42 | // eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging |
| 43 | export class ManagedChildProcess extends EventEmitter { |
| 44 | // @TODO Add timeouts for restarting and killing the process (it should give up after some time, like 10 seconds) maybe? |
| 45 | |
| 46 | public id: string; |
| 47 | public info: INamedBackProcessInfo | IBackProcessInfo; |
| 48 | /** Process that this is wrapping/managing. */ |
| 49 | private process?: ChildProcess; |
| 50 | /** If the process is currently being restarted. */ |
| 51 | private _isRestarting = false; |
| 52 | /** Display name of the service. */ |
| 53 | public readonly name: string; |
| 54 | /** The current working directory of the process. */ |
| 55 | private readonly cwd: string; |
| 56 | /** If the process is detached (it is not spawned as a child process of this program). */ |
| 57 | private readonly detached: boolean; |
| 58 | /** If the process should be restarted if it exits unexpectedly. */ |
| 59 | private autoRestart: boolean; |
| 60 | /** Number of times the process has auto restarted. Used to prevent infinite loops. */ |
| 61 | private autoRestartCount: number; |
| 62 | /** Whether to run in a shell */ |
| 63 | private readonly shell: boolean; |
| 64 | /** Launch with these Environmental Variables */ |
| 65 | private readonly env?: NodeJS.ProcessEnv; |
| 66 | /** A timestamp of when the process was started. */ |
| 67 | private startTime = 0; |
| 68 | /** State of the process. */ |
| 69 | private state: ProcessState = ProcessState.STOPPED; |
| 70 | |
| 71 | constructor(id: string, name: string, cwd: string, opts: ProcessOpts, info: INamedBackProcessInfo | IBackProcessInfo) { |
| 72 | super(); |
| 73 | const { detached, autoRestart, noshell, env } = opts; |
| 74 | this.id = id; |
| 75 | this.name = name; |
| 76 | this.cwd = cwd; |
| 77 | this.detached = !!detached; |
| 78 | this.autoRestart = !!autoRestart; |
| 79 | this.autoRestartCount = 0; |
| 80 | this.info = info; |
| 81 | this.shell = !noshell; |
| 82 | this.env = env; |
| 83 | } |
| 84 | |
| 85 | /** Get the process ID (or -1 if the process is not running). */ |
| 86 | public getPid(): number { |
| 87 | return this.process ? this.process.pid : -1; |
| 88 | } |
| 89 | |
| 90 | /** Get the state of the process. */ |
| 91 | public getState(): ProcessState { |
| 92 | return this.state; |
| 93 | } |
| 94 | |
| 95 | /** Get the time timestamp of when the process was started. */ |
| 96 | public getStartTime(): number { |
| 97 | return this.startTime; |
| 98 | } |
| 99 | |
| 100 | /** |
nothing calls this directly
no test coverage detected