-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathexecDaemon.ts
More file actions
50 lines (46 loc) · 1.53 KB
/
execDaemon.ts
File metadata and controls
50 lines (46 loc) · 1.53 KB
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
import path from 'path';
import { spawn } from 'child_process';
import { useLocal, getCommonDaemonEnv } from './libs';
import { isCiCdEnvironment } from '@serverless-devs/utils';
import fs from 'fs-extra';
import { get } from 'lodash';
const TTL = 10 * 60 * 1000;
interface IConfig {
[key: string]: any;
}
interface IConfigWithTTL extends IConfig {
lockPath: string;
}
function readJsonFile(filePath: string) {
if (fs.existsSync(filePath)) {
const data = fs.readFileSync(filePath, 'utf8');
return JSON.parse(data);
}
}
export function execDaemon(filename: string, config?: IConfig) {
const filePath = path.join(__dirname, 'daemon', filename);
if (!fs.existsSync(filePath)) return;
const core_use_daemon = get(process, 'env.core_use_daemon', 'true');
if (core_use_daemon === 'true') {
const subprocess = spawn(process.execPath, [filePath], {
detached: true,
stdio: 'ignore',
env: { ...getCommonDaemonEnv(), ...config },
});
return subprocess.unref();
}
spawn(process.execPath, [filePath], {
stdio: 'inherit',
env: { ...getCommonDaemonEnv(), ...config },
});
}
export function execDaemonWithTTL(filename: string, config: IConfigWithTTL) {
if (useLocal()) return;
if (isCiCdEnvironment()) return;
const { lockPath } = config;
const lockFileInfo = readJsonFile(lockPath);
const now = Date.now();
if (now - lockFileInfo?.currentTimestamp < TTL) return;
fs.writeFileSync(lockPath, JSON.stringify({ ...lockFileInfo, currentTimestamp: now }, null, 2));
execDaemon(filename, config);
}