-
Notifications
You must be signed in to change notification settings - Fork 1
/
updatecheck-worker.js
63 lines (53 loc) · 2.18 KB
/
updatecheck-worker.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
const { parentPort } = require('worker_threads');
const { loadSettings, checkUpdates, getBotInfo } = require('./botAPIv2');
const schedule = require('node-schedule');
const { createLogger, format, transports } = require('winston');
const logger = createLogger({
level: getBotInfo('logger_level'),
format: format.simple(),
transports: [new transports.Console()]
});
async function automatedUpdateCheck() {
try {
logger.debug('Starting automated update check');
const servers = await loadSettings('global', 'servers');
logger.debug(`Loaded ${servers.length} servers from settings`);
if (servers && Array.isArray(servers)) {
for (const server of servers) {
logger.debug(`Checking server ${server}`);
const updateChecking = await loadSettings(server, 'autocheckupdates');
if (!updateChecking) {
logger.debug(`Update checking is disabled for server ${server}`);
continue;
}
const version = await checkUpdates(server);
if (version) {
logger.debug(`Update found for server ${server}: ${version}`);
parentPort.postMessage({ serverId: server, version });
} else {
logger.debug(`No update found for server ${server}`);
}
}
} else {
logger.error('No servers found or servers is not an array.');
parentPort.postMessage({ error: 'No servers found or servers is not an array.' });
}
} catch (error) {
logger.error(`Error occurred while checking for updates: ${error.message}`);
parentPort.postMessage({ error: error.message });
}
}
parentPort.on('message', (message) => {
if (message === 'stop') {
logger.debug('Stopping worker');
job.cancel();
parentPort.close();
process.exit();
}else if (message === 'start') {
logger.info('worker started');
}
});
const job = schedule.scheduleJob('0 * * * *', () => {
logger.debug('Starting automated update check');
automatedUpdateCheck();
});