aboutsummaryrefslogtreecommitdiff
path: root/lib/event.model.ts
blob: 7768ffc01303210febae36a29bc2a5c3fcef9fdd (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import { model, Schema, Model } from 'mongoose';
import cron from 'cron';
import createEventSchema, { EventDocument } from './event.schema';
import { LogDocument } from './log.schema';
import LogModel from './log.model';

export interface Event<Context> extends EventDocument<Context> {
  log(message: string): void;
  start(): void;
  complete(): void;
  fail(error: Error | string): void;
  computeNextRunAt(): Date;
  getLogs(): Promise<LogDocument[]>;
}

export interface EventModel<Context> extends Model<Event<Context>> {
  findNextEvents(): Event<Context>[];
  findMissedEvents(): Event<Context>[];
}

const CronJob = cron.CronJob;

const createEventModel = <Context>(name: string, contextSchema: Schema): EventModel<Context> => {
  const schema = createEventSchema(contextSchema);

  // Schema methods
  schema.method('log', function(message: string) {
    const timestamp = new Date().toLocaleString('en');
    console.log(`[${timestamp}] ${this.type}: ${message}`);
    return LogModel.create({ eventId: this._id, message });
  });

  schema.method('start', function() {
    this.log('Event started')
    this.lastRunAt = new Date();
    this.status = 'running';
    return this.save();
  });

  schema.method('complete', function() {
    this.log('Event complete')
    this.status = 'complete';
    return this.save();
  });

  schema.method('fail', function(error: Error) {
    this.log(error);
    this.log('Event failed');
    this.error = error;
    this.status = 'failed';
    return this.save();
  });

  schema.method('computeNextRunAt', function() {
    const job = new CronJob(this.schedule);
    const nextRunAt = job.nextDates();
    return nextRunAt.toDate();
  });

  schema.method('getLogs', function() {
    return LogModel.find({ eventId: this._id });
  });

  // Statics
  schema.static('findMissedEvents', async function () {
    return this.find({
      nextRunAt: {
        // TODO: skip single-fire events
        $lt: new Date()
      },
    });
  });

  schema.static('findNextEvents', function(limit = 10) {
    return this.find(
      {
        nextRunAt: {
          $gt: new Date()
        },
      },
      null,
      {
        sort: {
          nextRunAt: 1
        },
        limit
      }
    )
  });

  // Hooks
  schema.pre<Event<Context>>('save', async function() {
    this.nextRunAt = this.computeNextRunAt();
  });

  return model<Event<Context>, EventModel<Context>>(name, schema);
};


export default createEventModel;