You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
80 lines
2.0 KiB
80 lines
2.0 KiB
// host-services.js
|
|
// Copyright (C) 2021 Digital Telepresence, LLC
|
|
// License: Apache-2.0
|
|
|
|
'use strict';
|
|
|
|
const path = require('path');
|
|
|
|
require('dotenv').config({ path: path.resolve(__dirname, '..', '..', '.env') });
|
|
|
|
const mongoose = require('mongoose');
|
|
|
|
const {
|
|
SitePlatform,
|
|
SiteAsync,
|
|
SiteLog,
|
|
SiteError,
|
|
} = require(path.join(__dirname, '..', '..', 'lib', 'site-lib'));
|
|
|
|
const { CronJob } = require('cron');
|
|
|
|
const CRON_TIMEZONE = 'America/New_York';
|
|
|
|
module.pkg = require(path.resolve(__dirname, '..', '..', 'package.json'));
|
|
module.config = {
|
|
componentName: 'reeeper',
|
|
root: path.resolve(__dirname, '..', '..'),
|
|
};
|
|
|
|
module.log = new SiteLog(module, module.config.componentName);
|
|
|
|
module.expireCrashedHosts = async ( ) => {
|
|
const NetHost = mongoose.model('NetHost');
|
|
try {
|
|
await NetHost
|
|
.find({ status: 'crashed' })
|
|
.select('_id hostname')
|
|
.lean()
|
|
.cursor()
|
|
.eachAsync(async (host) => {
|
|
module.log.info('deactivating crashed host', { hostname: host.hostname });
|
|
await NetHost.updateOne({ _id: host._id }, { $set: { status: 'inactive' } });
|
|
});
|
|
} catch (error) {
|
|
module.log.error('failed to expire crashed hosts', { error });
|
|
}
|
|
};
|
|
|
|
(async ( ) => {
|
|
try {
|
|
process.once('SIGINT', async ( ) => {
|
|
module.log.info('SIGINT received');
|
|
module.log.info('requesting shutdown...');
|
|
|
|
const exitCode = await SitePlatform.shutdown();
|
|
process.nextTick(( ) => {
|
|
process.exit(exitCode);
|
|
});
|
|
});
|
|
|
|
/*
|
|
* Site Platform startup
|
|
*/
|
|
await SitePlatform.startPlatform(module);
|
|
|
|
await module.expireCrashedHosts(); // first-run the expirations
|
|
|
|
module.expireJob = new CronJob(
|
|
'*/5 * * * * *',
|
|
module.expireCrashedHosts,
|
|
null, true, CRON_TIMEZONE,
|
|
);
|
|
|
|
module.log.info(`${module.pkg.name} v${module.pkg.version} ${module.config.componentName} started`);
|
|
} catch (error) {
|
|
module.log.error('failed to start Host Cache worker', { error });
|
|
process.exit(-1);
|
|
}
|
|
|
|
})();
|