From 7829418b905acc4ac81298bd5195a61a23026120 Mon Sep 17 00:00:00 2001 From: Evert Prants Date: Fri, 1 Oct 2021 21:46:42 +0300 Subject: [PATCH] use cron instead of interval --- calendar/plugin.json | 2 +- calendar/plugin.ts | 33 +++++++++++++++++---------------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/calendar/plugin.json b/calendar/plugin.json index 345aab8..0a7a0a9 100644 --- a/calendar/plugin.json +++ b/calendar/plugin.json @@ -4,6 +4,6 @@ "description": "Plugin for iCalendar/VCS room events", "version": "1.1.2", "tags": ["commands", "utility", "calendar"], - "dependencies": ["simplecommands"], + "dependencies": ["simplecommands", "cron"], "npmDependencies": ["node-ical@^0.13.0"] } diff --git a/calendar/plugin.ts b/calendar/plugin.ts index d5f3081..85bb7c2 100644 --- a/calendar/plugin.ts +++ b/calendar/plugin.ts @@ -11,8 +11,6 @@ import { logger } from '@squeebot/core/lib/core'; import { Formatter, IMessage, MessageResolver } from '@squeebot/core/lib/types'; import { fullIDMatcher, readableTime } from '@squeebot/core/lib/common'; -let calendarTimeout: NodeJS.Timeout; - interface CalendarConfiguration { name: string; url: string; @@ -126,9 +124,7 @@ function tellEvent(eventData: Event, msg: IMessage, countdown = true): void { msg.resolve(keys); } -async function fetchCalendars(interval: number): Promise { - clearTimeout(calendarTimeout); - +async function fetchCalendars(): Promise { memcache = []; for (const cfg of loaded) { let result; @@ -194,24 +190,17 @@ async function fetchCalendars(interval: number): Promise { logger.log('[calendar] Fetched %d events from %s.', events, cfg.name); } - - calendarTimeout = setTimeout(() => { - fetchCalendars(interval).catch( - (e) => logger.error('[calendar] fetch error:', e.stack)); - }, interval * 1000); } @Configurable({ calendars: [], - updateInterval: 1800, + updateInterval: '30 * * * *', }) class CalendarPlugin extends Plugin { @EventListener('pluginUnload') public unloadEventHandler(plugin: string | Plugin): void { if (plugin === this.name || plugin === this) { - clearTimeout(calendarTimeout); - this.config.save().then(() => - this.emit('pluginUnloaded', this)); + this.emit('pluginUnloaded', this); } } @@ -223,7 +212,7 @@ class CalendarPlugin extends Plugin { plugin: this.name, execute: async (msg: IMessage, msr: MessageResolver, spec: any, prefix: string, ...simplified: any[]): Promise => { if (simplified[0] && simplified[0] === 'refresh') { - await fetchCalendars(this.config.get('updateInterval', 1800)); + await fetchCalendars(); } let events = eventsFor(msg); @@ -310,9 +299,21 @@ class CalendarPlugin extends Plugin { } } + @DependencyLoad('cron') + public initializeCron(cronPlugin: any): void { + const expression = this.config.get('updateInterval', '30 * * * *'); + cronPlugin.registerTimer( + this, + expression, + () => fetchCalendars().catch( + (error) => logger.error('[calendar] fetch error:', error.stack), + ) + ); + } + initialize(): void { this.loadConfig(); - fetchCalendars(this.config.get('updateInterval', 1800)).catch( + fetchCalendars().catch( (e) => logger.error('[%s] fetch error:', this.name, e.stack)); } }