-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
72 lines (61 loc) · 1.86 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
/**
* jobqueue hook
*
* @description :: A hook definition. Extends Sails by adding shadow routes, implicit actions, and/or initialization logic.
* @docs :: https://sailsjs.com/docs/concepts/extending-sails/hooks
*/
var kue = require("kue");
var Job = kue.Job;
var redis = require("redis");
module.exports = function jobqueue(sails) {
return {
initialize: async function () {
// var hook = this;
var eventsToWaitFor = [];
if (sails.hooks.orm) {
eventsToWaitFor.push('hook:orm:loaded');
}
if (sails.hooks.pubsub) {
eventsToWaitFor.push('hook:pubsub:loaded');
}
sails.after(eventsToWaitFor, function () {
initJobQueue();
sails.log.info(" 🍺 Logistic Infotech's sails-hook-job-queue loaded 🍺 ");
});
}
};
function initJobQueue() {
// Create job queue on Jobs service
var processors = Jobs._processors;
var redis_url = sails.config.redis_url ? sails.config.redis_url : 'redis://127.0.0.1:6379';
Jobs = kue.createQueue({
redis: redis_url
});
Jobs._processors = processors;
startWorker();
}
function startWorker() {
logJobs();
startProcessors();
};
function startProcessors() {
for (var identity in Jobs._processors) {
Jobs.process(identity, Jobs._processors[identity]);
}
};
function logJobs() {
Jobs.on("job complete", function (id) {
Job.get(id, function (err, job) {
if (err) return;
sails.log.info("🌿 Job '" + job.type + "' (ID: " + id + ") completed successfully." + job.data);
});
}).on("job failed", function (id) {
Job.get(id, function (err, job) {
if (err) return;
sails.log(job._error);
sails.log("\n");
sails.log.warn("🤯 Job '" + job.type + "' (ID: " + id + ") failed. Error: " + job._error);
});
});
};
};