summaryrefslogtreecommitdiff
path: root/src/services/events/event.model.js
blob: 9ecacb7ce603f8ee9e3e46665ebaac8618d52f3b (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
const cron = require('cron');
const { model } = require('mongoose');
const schema = require('./event.schema.js');
const LogModel = require('./log.model.js');

const CronJob = cron.CronJob;

schema.methods.log = function(message) {
  const dateOpts = { timeStyle: 'medium', dateStyle: 'short' };
  const timestamp = new Date().toLocaleString('en', dateOpts);
  console.log(`[${timestamp}] ${this.name}: ${message}`);

  return LogModel.create({
    eventId: this._id,
    message
  });
};

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

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

schema.methods.fail = function(error) {
  this.log(error);
  this.error = error;
  this.status = 'failed';
  return this.save();
};

schema.methods.run = async function(handler) {
  try {
    this.start();
    await handler(this);
    return this.complete();
  } catch (error) {
    this.fail(error);
  }
};

schema.methods.computeNextRunAt = function() {
  const job = new CronJob(this.schedule);
  const nextRunAt = job.nextDates();
  return new Date(nextRunAt);
};

schema.pre('save', function(next) {
  this.nextRunAt = this.computeNextRunAt();
  next();
});

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

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

const Model = model('Event', schema);


module.exports = Model;