aboutsummaryrefslogtreecommitdiff
path: root/lib/scheduler.ts
blob: 1d52c35027dd936bf895cd6862d31057055c0ca3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import cron from 'cron';
import Bluebird from 'bluebird';
import { EventModel } from './event.model';

const CronJob = cron.CronJob;

const defaultPollingInterval = '*/10 * * * * *';


class Scheduler {
  job: cron.CronJob;
  jobs: cron.CronJob[];
  Model: EventModel<any>;

  constructor(model: EventModel<any>, pollingInterval = defaultPollingInterval) {
    this.Model = model;
    this.jobs = [];

    this.job = new CronJob(pollingInterval, () => this.updateJobs());
    this.startPolling();
  }

  startPolling() {
    this.job.start();
  }
  
  stopPolling() {
    this.job.stop();
  }

  startAllJobs() {
    this.jobs.forEach(job => job.start());
  }

  stopAllJobs() {
    this.jobs.forEach(job => job.stop());
  }

  async rescheduleMissedEvents() {
    const missedEvents = await this.Model.findMissedEvents();
    return Bluebird.map(missedEvents, event => event.save());
  }

  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)
    await this.rescheduleMissedEvents();

    this.stopAllJobs();

    const events = await this.Model.findNextEvents();
    if (!events.length) console.log('WARNING: no upcoming events');
    this.jobs = events.map(event => new CronJob(
      event.schedule,
      () => this.run(event.id)
    ));

    this.startAllJobs();
  }

  async run(id: string) {
    const event = await this.Model.findById(id);
    // TODO: handle the case when event is deleted
    if (!event) return;

    try {
      event.start();
      // TODO: put actual handler here
      await new Promise(res => setTimeout(res, 5000));
      return event.complete();
    } catch (error) {
      event.fail(error);
    }
  }
}


export default Scheduler;