-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
112 lines (97 loc) · 3.41 KB
/
index.js
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
102
103
104
105
106
107
108
109
110
111
112
const Agenda = require('agenda');
const stopAgenda = require('stop-agenda');
Agenda.prototype.configure = function(config) {
this.config = {
logger: console,
// these are options passed directly to `stop-agenda`
// <https://github.com/ladjs/stop-agenda>
stopAgenda: {
cancelQuery: {
repeatInterval: {
$exists: true,
$ne: null
}
}
},
// these are jobs defined via `config.jobs`
// e.g. `agendaJobDefinitions: [ [name, agendaOptions, fn], ... ]`
agendaJobDefinitions: [],
// these get automatically invoked to `agenda.every`
// e.g. `agenda.every('5 minutes', 'locales')`
// and you define them as [ interval, job name ]
// you need to define them here for graceful handling
// e.g. `agendaRecurringJobs: [ ['5 minutes', 'locales' ], ... ]`
agendaRecurringJobs: [],
// these get automatically invoked when process starts
// e.g. `agenda.now('locales');`
// and you define them as Strings in the array
// e.g. `config.now: ['locales','ping','pong','beep', ... ]`
agendaBootJobs: [],
...config
};
};
Agenda.prototype._start = Agenda.prototype.start;
Agenda.prototype.start = function() {
this.on('ready', () => {
// Pending PR #501
// <https://github.com/agenda/agenda/pull/501>
this._isReady = true;
// output debug info
this.config.logger.info('agenda ready');
// we cancel jobs here so we don't create duplicates
// on every time the server restarts, or mongoose reconnects
// (even though `agenda.every` uses single, just to be safe)
//
// note that the core reason we have this is because
// during development we may remove recurring jobs
// and define new ones, therefore we don't want the old ones to run
this.cancel(this.config.stopAgenda.cancelQuery, async (err, numRemoved) => {
// if there was an error then log it and stop agenda
if (err) {
this.config.logger.error(err);
try {
await stopAgenda(this, this.config.stopAgenda);
this.config.logger.debug(
'stopped agenda due to issue with agenda cancel'
);
} catch (err) {
this.config.logger.error(err);
}
return;
}
this.config.logger.debug(`cancelled ${numRemoved} jobs`);
// Define all of our jobs
this.config.agendaJobDefinitions.forEach(_job => {
this.define(..._job);
});
// Schedule recurring jobs
this.config.agendaRecurringJobs.forEach(every => {
this.every(...every);
});
// Start jobs needed to run now
this.config.agendaBootJobs.forEach(job => {
this.now(job);
});
this._start();
});
});
// Handle events emitted
this.on('start', job =>
this.config.logger.debug(`job "${job.attrs.name}" started`)
);
this.on('complete', job => {
this.config.logger.debug(`job "${job.attrs.name}" completed`);
// NOTE: You may want to manually handle garbage collection here
// <https://github.com/rschmukler/agenda/issues/129#issuecomment-108057837>
// memwatch.gc();
});
this.on('success', job => {
this.config.logger.debug(`job "${job.attrs.name}" succeeded`);
});
this.on('fail', (err, job) => {
err.message = `job "${job.attrs.name}" failed: ${err.message}`;
this.config.logger.error(err, { job });
});
this.on('error', this.config.logger.error);
};
module.exports = Agenda;