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.
87 lines
2.3 KiB
87 lines
2.3 KiB
// host-services.js
|
|
// Copyright (C) 2022 DTP Technologies, LLC
|
|
// License: Apache-2.0
|
|
|
|
'use strict';
|
|
|
|
const path = require('path');
|
|
|
|
require('dotenv').config({ path: path.resolve(__dirname, '..', '..', '.env') });
|
|
|
|
const mongoose = require('mongoose');
|
|
|
|
const {
|
|
SiteLog,
|
|
SiteWorker,
|
|
} = require(path.join(__dirname, '..', '..', 'lib', 'site-lib'));
|
|
|
|
const { CronJob } = require('cron');
|
|
|
|
module.rootPath = path.resolve(__dirname, '..', '..');
|
|
module.pkg = require(path.resolve(__dirname, '..', '..', 'package.json'));
|
|
|
|
module.config = {
|
|
environment: process.env.NODE_ENV,
|
|
root: module.rootPath,
|
|
component: { name: 'DTP Reeeper', slug: 'reeeper' },
|
|
};
|
|
|
|
module.config.site = require(path.join(module.rootPath, 'config', 'site'));
|
|
module.config.http = require(path.join(module.rootPath, 'config', 'http'));
|
|
|
|
class ReeeperWorker extends SiteWorker {
|
|
|
|
constructor (dtp) {
|
|
super(dtp, dtp.config.component);
|
|
}
|
|
|
|
async start ( ) {
|
|
const CRON_TIMEZONE = 'America/New_York';
|
|
await super.start();
|
|
|
|
await this.expireCrashedHosts(); // first-run the expirations
|
|
this.expireJob = new CronJob('*/5 * * * * *', this.expireCrashedHosts.bind(this), null, true, CRON_TIMEZONE);
|
|
}
|
|
|
|
async stop ( ) {
|
|
if (this.expireJob) {
|
|
this.log.info('stopping host expire job');
|
|
this.expireJob.stop();
|
|
delete this.expireJob;
|
|
}
|
|
|
|
await super.stop();
|
|
}
|
|
|
|
async expireCrashedHosts ( ) {
|
|
const NetHost = mongoose.model('NetHost');
|
|
try {
|
|
await NetHost
|
|
.find({ status: 'crashed' })
|
|
.select('_id hostname')
|
|
.lean()
|
|
.cursor()
|
|
.eachAsync(async (host) => {
|
|
this.log.info('deactivating crashed host', { hostname: host.hostname });
|
|
await NetHost.updateOne({ _id: host._id }, { $set: { status: 'inactive' } });
|
|
});
|
|
} catch (error) {
|
|
this.log.error('failed to expire crashed hosts', { error });
|
|
}
|
|
}
|
|
}
|
|
|
|
(async ( ) => {
|
|
try {
|
|
module.log = new SiteLog(module, module.config.component);
|
|
|
|
module.worker = new ReeeperWorker(module);
|
|
await module.worker.start();
|
|
|
|
module.log.info(`${module.pkg.name} v${module.pkg.version} ${module.config.component.name} started`);
|
|
} catch (error) {
|
|
module.log.error('failed to start worker', { component: module.config.component, error });
|
|
process.exit(-1);
|
|
}
|
|
|
|
})();
|