-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathProcessStarter.ts
59 lines (51 loc) · 1.63 KB
/
ProcessStarter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import child_process from 'child_process';
import CancellationToken from 'cancellationtoken';
import Log from './Log';
import { ProcessConfiguration } from './shared/BackOfficeStatus';
import * as SystemPromise from './SystemPromise';
import Sleep from './Sleep';
const logger = Log.logger(__filename);
// Ensure that a process is running.
// Restart as required.
// Configuration expect:
// autorun
// path
// env
export default class ProcessStarter {
private readonly exe: string;
private readonly configuration: ProcessConfiguration;
constructor(exe:string, configuration: ProcessConfiguration) {
this.exe = exe;
this.configuration = configuration;
if (this.configuration.autorun) {
this.lifeCycle(CancellationToken.CONTINUE);
}
}
private startExe() {
logger.info('Starting', {exe: this.exe});
var env = process.env;
env = Object.assign({}, env);
env = Object.assign(env, this.configuration.env);
var exe = this.exe;
if (this.configuration.path !== null) {
exe = this.configuration.path + "/" + exe;
}
var child = child_process.spawn(exe, [], {
env: env,
detached: true,
stdio: "ignore",
});
child.on('error', (err)=> {
logger.warn('Error', {exe: this.exe}, err);
});
}
private lifeCycle=async (ct:CancellationToken)=>{
while(true) {
const exists = await SystemPromise.PidOf(ct, this.exe);
if (!exists) {
this.startExe();
}
await Sleep(ct, 2000);
}
}
}