Skip to content

Commit

Permalink
added a job for sending http requests
Browse files Browse the repository at this point in the history
  • Loading branch information
iolufemi committed Aug 15, 2017
1 parent f514ab1 commit 53fc73f
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 4 deletions.
5 changes: 3 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
| controllers/Users.js | 297 | Test that any deleted data is backed up
| routes/index.js | 214 | Implement API Generator
| routes/initialize.js | 10 | Test initialize route
| services/encryption/index.js | 40 | Generate checksum here
| services/queue/clock.js | 11 | work on a clock functionality so kue can support scheduled jobs
| services/queue/jobs.js | 80 | Test saveToTrash job
| services/queue/jobs.js | 93 | Test Webhook Event
| services/queue/jobs.js | 137 | Test Secure Webhooks
| services/queue/jobs.js | 144 | Test Unsecure Webhooks
| services/queue/jobs.js | 144 | Test Unsecure Webhooks
| services/queue/jobs.js | 169 | Test sendHTTPRequest Job
| services/encryption/index.js | 40 | Generate checksum here
2 changes: 1 addition & 1 deletion routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ router.use(expressValidator());
if(config.noFrontendCaching === 'yes'){
router.use(helmet.noCache());
}else{
router.use(router._APICache);
router.use(router._APICache);
}

router.get('/', function (req, res) {
Expand Down
1 change: 1 addition & 0 deletions routes/users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"use strict";
46 changes: 46 additions & 0 deletions services/queue/jobs.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,50 @@ jobs.sendWebhook = function(data, done){
});
};

// Send HTTP Request
// 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.
// This can be used within the code too, to do some jobs.
// Supports POST or GET
// ToDo: Test sendHTTPRequest Job
jobs.sendHTTPRequest = function(data, done){
log.info('Sending HTTP' +data.method+' request to '+data.url+' with data => '+data.data+' and headers => '+data.headers);
// Expected data
// {
// url: 'http://string.com',
// method: 'POST', // or any http method
// headers: {
// 'User-Agent': 'Request-Promise'
// },
// data: {
// someData: 'this',
// someOtherData: 'and this'
// }
// }
//

var options = {
method: data.method,
uri: data.url,
body: data.data,
headers: data.headers,
json: true // Automatically parses the JSON string in the response
};

if(data.method === 'GET'){
options.qs = data.data;
}else if(data.method === 'POST'){
options.body = data.data;
}else{
options.qs = data.data;
options.body = data.data;
}
request(options)
.then(function(resp){
done(false, resp);
})
.catch(function(err){
done(new Error(err.message));
});
};

module.exports = jobs;
4 changes: 4 additions & 0 deletions services/queue/workers.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@ queue.process('sendWebhook', 2, function(job,done){
jobs.sendWebhook(job.data, done);
});

queue.process('sendHTTPRequest', 2, function(job,done){
jobs.sendHTTPRequest(job.data, done);
});

module.exports = queue;
2 changes: 1 addition & 1 deletion test/services/queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe('#Queue service', function(){
it('should load processes', function() {
var process = require('../../services/queue/workers');
// We have configured queue to create 2 workers per job making a total of 6 workers for 3 jobs that we currently have
process.workers.length.should.equal(10);
process.workers.length.should.equal(12);
});


Expand Down

0 comments on commit 53fc73f

Please sign in to comment.