Skip to content

Commit 53fc73f

Browse files
committed
added a job for sending http requests
1 parent f514ab1 commit 53fc73f

File tree

6 files changed

+56
-4
lines changed

6 files changed

+56
-4
lines changed

TODO.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@
1616
| controllers/Users.js | 297 | Test that any deleted data is backed up
1717
| routes/index.js | 214 | Implement API Generator
1818
| routes/initialize.js | 10 | Test initialize route
19-
| services/encryption/index.js | 40 | Generate checksum here
2019
| services/queue/clock.js | 11 | work on a clock functionality so kue can support scheduled jobs
2120
| services/queue/jobs.js | 80 | Test saveToTrash job
2221
| services/queue/jobs.js | 93 | Test Webhook Event
2322
| services/queue/jobs.js | 137 | Test Secure Webhooks
24-
| services/queue/jobs.js | 144 | Test Unsecure Webhooks
23+
| services/queue/jobs.js | 144 | Test Unsecure Webhooks
24+
| services/queue/jobs.js | 169 | Test sendHTTPRequest Job
25+
| services/encryption/index.js | 40 | Generate checksum here

routes/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ router.use(expressValidator());
177177
if(config.noFrontendCaching === 'yes'){
178178
router.use(helmet.noCache());
179179
}else{
180-
router.use(router._APICache);
180+
router.use(router._APICache);
181181
}
182182

183183
router.get('/', function (req, res) {

routes/users.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"use strict";

services/queue/jobs.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,50 @@ jobs.sendWebhook = function(data, done){
162162
});
163163
};
164164

165+
// Send HTTP Request
166+
// This is for jobs that can be configured from an admin dashboard. So an admin an configure the system to all an api at a particular time daily.
167+
// This can be used within the code too, to do some jobs.
168+
// Supports POST or GET
169+
// ToDo: Test sendHTTPRequest Job
170+
jobs.sendHTTPRequest = function(data, done){
171+
log.info('Sending HTTP' +data.method+' request to '+data.url+' with data => '+data.data+' and headers => '+data.headers);
172+
// Expected data
173+
// {
174+
// url: 'http://string.com',
175+
// method: 'POST', // or any http method
176+
// headers: {
177+
// 'User-Agent': 'Request-Promise'
178+
// },
179+
// data: {
180+
// someData: 'this',
181+
// someOtherData: 'and this'
182+
// }
183+
// }
184+
//
185+
186+
var options = {
187+
method: data.method,
188+
uri: data.url,
189+
body: data.data,
190+
headers: data.headers,
191+
json: true // Automatically parses the JSON string in the response
192+
};
193+
194+
if(data.method === 'GET'){
195+
options.qs = data.data;
196+
}else if(data.method === 'POST'){
197+
options.body = data.data;
198+
}else{
199+
options.qs = data.data;
200+
options.body = data.data;
201+
}
202+
request(options)
203+
.then(function(resp){
204+
done(false, resp);
205+
})
206+
.catch(function(err){
207+
done(new Error(err.message));
208+
});
209+
};
210+
165211
module.exports = jobs;

services/queue/workers.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,8 @@ queue.process('sendWebhook', 2, function(job,done){
2323
jobs.sendWebhook(job.data, done);
2424
});
2525

26+
queue.process('sendHTTPRequest', 2, function(job,done){
27+
jobs.sendHTTPRequest(job.data, done);
28+
});
29+
2630
module.exports = queue;

test/services/queue.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ describe('#Queue service', function(){
4747
it('should load processes', function() {
4848
var process = require('../../services/queue/workers');
4949
// We have configured queue to create 2 workers per job making a total of 6 workers for 3 jobs that we currently have
50-
process.workers.length.should.equal(10);
50+
process.workers.length.should.equal(12);
5151
});
5252

5353

0 commit comments

Comments
 (0)