// reeeper/cron/expire-crashed-hosts.js // Copyright (C) 2022 DTP Technologies, LLC // License: Apache-2.0 'use strict'; const path = require('path'); const mongoose = require('mongoose'); const NetHost = mongoose.model('NetHost'); const { CronJob } = require('cron'); const { SiteWorkerProcess } = require(path.join(__dirname, '..', '..', '..', '..', 'lib', 'site-lib')); /** * DTP Core Chat sticker processor can receive requests to ingest and delete * stickers to be executed as background jobs in a queue. This processor * attaches to the `media` queue and registers processors for `sticker-ingest` * and `sticker-delete`. */ class CrashedHostsCron extends SiteWorkerProcess { static get COMPONENT ( ) { return { name: 'crashedHostsCron', slug: 'crashed-hosts-cron', }; } constructor (worker) { super(worker, CrashedHostsCron.COMPONENT); } async start ( ) { await super.start(); await this.expireCrashedHosts(); // first-run the expirations this.job = new CronJob( '*/5 * * * * *', this.expireCrashedHosts.bind(this), null, true, process.env.DTP_CRON_TIMEZONE || 'America/New_York', ); } async stop ( ) { if (this.job) { this.log.info('stopping host expire job'); this.job.stop(); delete this.job; } await super.stop(); } async expireCrashedHosts ( ) { 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 }); } } } module.exports = CrashedHostsCron;