-
Notifications
You must be signed in to change notification settings - Fork 269
Expand file tree
/
Copy pathindex.js
More file actions
142 lines (117 loc) · 3.41 KB
/
index.js
File metadata and controls
142 lines (117 loc) · 3.41 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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*******************************************************************************
Highcharts Export Server
Copyright (c) 2016-2024, Highsoft
Licenced under the MIT licence.
Additionally a valid Highcharts license is required for use.
See LICENSE file in root for details.
*******************************************************************************/
import { checkAndUpdateCache } from './cache.js';
import {
batchExport,
setAllowCodeExecution,
singleExport,
startExport
} from './chart.js';
import { mapToNewConfig, manualConfig, setOptions } from './config.js';
import {
initLogging,
log,
logWithStack,
setLogLevel,
enableFileLogging
} from './logger.js';
import { initPool, killPool } from './pool.js';
import { shutdownCleanUp } from './resource_release.js';
import server, { startServer } from './server/server.js';
import { printLogo, printUsage } from './utils.js';
/**
* Attaches exit listeners to the process, ensuring proper cleanup of resources
* and termination on exit signals. Handles 'exit', 'SIGINT', 'SIGTERM', and
* 'uncaughtException' events.
*/
const attachProcessExitListeners = () => {
log(3, '[process] Attaching exit listeners to the process.');
// Handler for the 'exit'
process.on('exit', (code) => {
log(4, `Process exited with code ${code}.`);
});
// Handler for the 'SIGINT'
process.on('SIGINT', async (name, code) => {
log(4, `The ${name} event with code: ${code}.`);
await shutdownCleanUp(0);
});
// Handler for the 'SIGTERM'
process.on('SIGTERM', async (name, code) => {
log(4, `The ${name} event with code: ${code}.`);
await shutdownCleanUp(0);
});
// Handler for the 'SIGHUP'
process.on('SIGHUP', async (name, code) => {
log(4, `The ${name} event with code: ${code}.`);
await shutdownCleanUp(0);
});
// Handler for the 'uncaughtException'
process.on('uncaughtException', async (error, name) => {
logWithStack(1, error, `The ${name} error.`);
await shutdownCleanUp(1);
});
};
/**
* Initializes the export process. Tasks such as configuring logging, checking
* cache and sources, and initializing the pool of resources happen during
* this stage. Function that is required to be called before trying to export charts or setting a server. The `options` is an object that contains all options.
*
* @param {Object} options - All export options.
*
* @returns {Promise<Object>} Promise resolving to the updated export options.
*/
const initExport = async (options) => {
// Set the allowCodeExecution per export module scope
setAllowCodeExecution(
options.customLogic && options.customLogic.allowCodeExecution
);
// Init the logging
initLogging(options.logging);
// Attach process' exit listeners
if (options.other.listenToProcessExits) {
attachProcessExitListeners();
}
// Check if cache needs to be updated
await checkAndUpdateCache(options);
// Init the pool
await initPool({
pool: options.pool || {
minWorkers: 1,
maxWorkers: 1
},
puppeteerArgs: options.puppeteer.args || []
});
// Return updated options
return options;
};
export default {
// Server
server,
startServer,
// Exporting
initExport,
singleExport,
batchExport,
startExport,
// Pool
initPool,
killPool,
// Other
setOptions,
shutdownCleanUp,
// Logs
log,
logWithStack,
setLogLevel,
enableFileLogging,
// Utils
mapToNewConfig,
manualConfig,
printLogo,
printUsage
};