-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
74 lines (62 loc) · 1.83 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
'use strict'
const kue = require('kue');
const EventEmitter = require('events').EventEmitter;
const debug = require('debug')('loopback:component:kue-push');
const pushHandler = require('./lib/push-handler');
module.exports = function (app, options) {
debug('Setting up component');
const defaultOptions = {
namespace: 'push',
redis: {
host: '127.0.0.1',
port: 6379,
},
}
options = Object.assign({}, defaultOptions, options);
let push = app[options.namespace] = new EventEmitter();
debug('Create push notifications queue');
var pushQueue = kue.createQueue({
redis: {
host: options.redis.host,
port: options.redis.port,
}
});
pushQueue.on('error', function (err) {
debug('Queue error: ', err.Error);
});
pushQueue.process('push', function (job, done) {
debug('Push from queue is about to be processed');
sendPush(job.data, function (err, json) {
if (err) {
var error = new Error('Error sending push');
error.job = job;
error.transportError = err;
push.emit('error', error);
debug('Error sending push with job id: %d', job.id);
} else {
push.emit('success', job);
debug('Push with job id "%d" was successfully sent.', job.id);
}
done();
})
});
let sendPush = pushHandler(options.pushSettings);
push.send = function(data, callback) {
const notification = 'deimudder';
let job = pushQueue.create('push', notification).save(function (error) {
if (error) {
debug('Error adding job to the queue.');
callback({
status: error.message,
jobId: job.id,
}, null);
} else {
debug('Job has been added to the queue.');
callback(null, {
status: 'added to queue',
jobId: job.id,
});
}
});
}
}