use cron instead of interval

This commit is contained in:
Evert Prants 2021-10-01 21:46:42 +03:00
parent dfd0337397
commit 7829418b90
Signed by: evert
GPG Key ID: 1688DA83D222D0B5
2 changed files with 18 additions and 17 deletions

View File

@ -4,6 +4,6 @@
"description": "Plugin for iCalendar/VCS room events", "description": "Plugin for iCalendar/VCS room events",
"version": "1.1.2", "version": "1.1.2",
"tags": ["commands", "utility", "calendar"], "tags": ["commands", "utility", "calendar"],
"dependencies": ["simplecommands"], "dependencies": ["simplecommands", "cron"],
"npmDependencies": ["node-ical@^0.13.0"] "npmDependencies": ["node-ical@^0.13.0"]
} }

View File

@ -11,8 +11,6 @@ import { logger } from '@squeebot/core/lib/core';
import { Formatter, IMessage, MessageResolver } from '@squeebot/core/lib/types'; import { Formatter, IMessage, MessageResolver } from '@squeebot/core/lib/types';
import { fullIDMatcher, readableTime } from '@squeebot/core/lib/common'; import { fullIDMatcher, readableTime } from '@squeebot/core/lib/common';
let calendarTimeout: NodeJS.Timeout;
interface CalendarConfiguration { interface CalendarConfiguration {
name: string; name: string;
url: string; url: string;
@ -126,9 +124,7 @@ function tellEvent(eventData: Event, msg: IMessage, countdown = true): void {
msg.resolve(keys); msg.resolve(keys);
} }
async function fetchCalendars(interval: number): Promise<void> { async function fetchCalendars(): Promise<void> {
clearTimeout(calendarTimeout);
memcache = []; memcache = [];
for (const cfg of loaded) { for (const cfg of loaded) {
let result; let result;
@ -194,24 +190,17 @@ async function fetchCalendars(interval: number): Promise<void> {
logger.log('[calendar] Fetched %d events from %s.', events, cfg.name); 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({ @Configurable({
calendars: [], calendars: [],
updateInterval: 1800, updateInterval: '30 * * * *',
}) })
class CalendarPlugin extends Plugin { class CalendarPlugin extends Plugin {
@EventListener('pluginUnload') @EventListener('pluginUnload')
public unloadEventHandler(plugin: string | Plugin): void { public unloadEventHandler(plugin: string | Plugin): void {
if (plugin === this.name || plugin === this) { if (plugin === this.name || plugin === this) {
clearTimeout(calendarTimeout); this.emit('pluginUnloaded', this);
this.config.save().then(() =>
this.emit('pluginUnloaded', this));
} }
} }
@ -223,7 +212,7 @@ class CalendarPlugin extends Plugin {
plugin: this.name, plugin: this.name,
execute: async (msg: IMessage, msr: MessageResolver, spec: any, prefix: string, ...simplified: any[]): Promise<boolean> => { execute: async (msg: IMessage, msr: MessageResolver, spec: any, prefix: string, ...simplified: any[]): Promise<boolean> => {
if (simplified[0] && simplified[0] === 'refresh') { if (simplified[0] && simplified[0] === 'refresh') {
await fetchCalendars(this.config.get('updateInterval', 1800)); await fetchCalendars();
} }
let events = eventsFor(msg); 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 { initialize(): void {
this.loadConfig(); this.loadConfig();
fetchCalendars(this.config.get('updateInterval', 1800)).catch( fetchCalendars().catch(
(e) => logger.error('[%s] fetch error:', this.name, e.stack)); (e) => logger.error('[%s] fetch error:', this.name, e.stack));
} }
} }