From aefa4cc4e7550258a656b0fb767644e48376dc8c Mon Sep 17 00:00:00 2001 From: eug-vs Date: Wed, 2 Dec 2020 03:11:21 +0300 Subject: refactor: rename job -> pollingJob and add scope --- lib/scheduler.ts | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/lib/scheduler.ts b/lib/scheduler.ts index 064daa8..d6b28e9 100644 --- a/lib/scheduler.ts +++ b/lib/scheduler.ts @@ -10,46 +10,46 @@ const defaultPollingInterval = '*/10 * * * * *'; class Scheduler { - job: cron.CronJob; - jobs: cron.CronJob[]; - Model: EventModel; - handlers: Record; + private jobs: cron.CronJob[]; + private pollingJob: cron.CronJob; + private handlers: Record; + public Model: EventModel; constructor(model: EventModel, pollingInterval = defaultPollingInterval) { this.Model = model; this.jobs = []; this.handlers = {}; - this.job = new CronJob(pollingInterval, () => this.updateJobs()); + this.pollingJob = new CronJob(pollingInterval, () => this.updateJobs()); this.startPolling(); } - registerHandler(name: string, handler: Handler) { + public registerHandler(name: string, handler: Handler) { this.handlers[name] = handler; } - startPolling() { - this.job.start(); + public startPolling() { + this.pollingJob.start(); } - stopPolling() { - this.job.stop(); + public stopPolling() { + this.pollingJob.stop(); } - startAllJobs() { + private startAllJobs() { this.jobs.forEach(job => job.start()); } - stopAllJobs() { + private stopAllJobs() { this.jobs.forEach(job => job.stop()); } - async rescheduleMissedEvents() { + private async rescheduleMissedEvents() { const missedEvents = await this.Model.findMissedEvents(); return Bluebird.map(missedEvents, event => event.save()); } - async updateJobs() { + private async updateJobs() { // Reschedule missed events before we stop jobs to avoid // accidentally stopping the job that has not triggered yet // (if event schedule resonates with updateJobs schedule) @@ -67,7 +67,7 @@ class Scheduler { this.startAllJobs(); } - async run(id: string) { + private async run(id: string) { const event = await this.Model.findById(id); if (!event) return console.log('WARNING: locked event does not exist'); -- cgit v1.2.3