forked from y-scope/clp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
launcher.js
111 lines (97 loc) · 3.21 KB
/
launcher.js
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/**
* Production launcher for CLP WebUI, which redirects Meteor server stderr to rotated error logs
* files in a specified directory for error monitoring.
*
* To avoid duplicated installations of dependencies, use the same `node_modules` for the server
* by setting envvar NODE_PATH="./programs/server/npm/node_modules", assuming this script is
* placed under the same directory where the bundled `main.js` is located.
*
* This is not intended for development use. For development, please refer to README.md in the
* component root for launching a development server with Meteor-specific error messages print
* to the console.
*
* ENVIRONMENT VARIABLES:
* - NODE_PATH: path to node_modules including "winston" and "winston-daily-rotate-file"
* - WEBUI_LOGS_DIR: path to error logs directory
* SCRIPT USAGE:
* - usage: node /path/to/launcher.js /path/to/main.js
*/
const {spawn} = require("child_process");
const winston = require("winston");
require("winston-daily-rotate-file");
const DEFAULT_LOGS_DIR = ".";
const MAX_LOGS_FILE_SIZE = "100m";
const MAX_LOGS_RETENTION_DAYS = "30d";
/**
* Creates a logger using winston module.
*
* @param {string} logsDir directory where the log files will be saved.
* @return {object} the logger object
*/
const getLogger = (logsDir) => {
return winston.createLogger({
format: winston.format.combine(
winston.format.timestamp(),
winston.format.printf((info) => {
return JSON.stringify({
timestamp: info.timestamp,
level: info.level,
label: info.label,
message: info.message,
});
}),
),
transports: [
new winston.transports.DailyRotateFile({
datePattern: "YYYY-MM-DD-HH",
dirname: logsDir,
filename: "webui_error-%DATE%.log",
maxFiles: MAX_LOGS_RETENTION_DAYS,
maxSize: MAX_LOGS_FILE_SIZE,
}),
],
});
};
/**
* Runs a script with logging support.
*
* @param {string} logsDir path where the logs will be stored
* @param {string} scriptPath path of the script to be executed
*/
const runScript = (logsDir, scriptPath) => {
const logger = getLogger(logsDir);
const script = spawn(process.argv0, [scriptPath]);
script.stderr.on("data", (data) => {
logger.error(data.toString());
});
script.on("close", (code) => {
console.log(`Child process exited with code ${code}`);
});
};
/**
* Parses the command line arguments and retrieves the values for the
* WEBUI_LOGS_DIR and scriptPath variables.
*
* @return {object} containing the values for WEBUI_LOGS_DIR and scriptPath
*/
const parseArgs = () => {
const WEBUI_LOGS_DIR = process.env.WEBUI_LOGS_DIR || DEFAULT_LOGS_DIR;
// eslint-disable-next-line prefer-destructuring
const scriptPath = process.argv[2];
return {
WEBUI_LOGS_DIR,
scriptPath,
};
};
/**
* The main function of the program.
*
* This function is the entry point of the program.
*
* @return {void}
*/
const main = () => {
const args = parseArgs();
runScript(args.WEBUI_LOGS_DIR, args.scriptPath);
};
main();