use cron instead of interval
This commit is contained in:
parent
dfd0337397
commit
7829418b90
@ -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"]
|
||||
}
|
||||
|
@ -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<void> {
|
||||
clearTimeout(calendarTimeout);
|
||||
|
||||
async function fetchCalendars(): Promise<void> {
|
||||
memcache = [];
|
||||
for (const cfg of loaded) {
|
||||
let result;
|
||||
@ -194,24 +190,17 @@ async function fetchCalendars(interval: number): Promise<void> {
|
||||
|
||||
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<boolean> => {
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user