NotificationLatch/src/reminderscheduler.js

58 lines
1.4 KiB
JavaScript

const { Duration } = require('luxon');
class ReminderScheduler
{
static create = container => new this(container.Config, container.Logger, container.NotificationFacade);
constructor(config, logger, notificationFacade)
{
this.reminders = config.reminders;
this.logger = logger;
this.notificationFacade = notificationFacade;
this.timerInterval = 60000;
}
start()
{
if (!this.reminders.enabled)
{
this.logger.info('Reminders are disabled');
return;
}
this.interval = Duration.fromObject(this.reminders.interval).shiftTo('seconds').seconds;
if (this.interval <= 0)
{
this.logger.warn(`Invalid reminder interval: ${this.interval} seconds, reminders will NOT be sent`);
return;
}
this.logger.info(`Checking for reminders every minute, interval is ${this.interval} seconds`);
setTimeout(this._onTimer.bind(this), this.timerInterval);
}
async _onTimer()
{
try
{
this.logger.verbose('Checking for reminders');
await this.notificationFacade.sendReminders(this.interval, this.reminders.title, this.reminders.message, this.reminders.sound);
}
catch (err)
{
this.logger.error(`Error while sending reminders: ${err}`);
this.logger.verbose(err.stack);
}
finally
{
setTimeout(this._onTimer.bind(this), this.timerInterval);
}
}
}
module.exports = ReminderScheduler;