Skip to content

Instantly share code, notes, and snippets.

@lyntco
Last active May 10, 2016 05:25
Show Gist options
  • Save lyntco/d9f11eb146b81c6ad0512cb3ce8a0421 to your computer and use it in GitHub Desktop.
Save lyntco/d9f11eb146b81c6ad0512cb3ce8a0421 to your computer and use it in GitHub Desktop.
// var express = require('express');
// var app = express();
// var bodyParser = require('body-parser');
var kue = require('kue')
, queue = kue.createQueue();
// The commented out parts are if you'd like to include queue in your server and manually add jobs in code.
// The code not commented out is if you'd like to use the JSON API (useful for bulk posts).
// app.use(bodyParser.json());
// app.post("/jobs", (req, res, next) => {
// console.log('Recieved the following:', req.body)
// var job = queue.create('email', req.body).removeOnComplete( true ).save( function(err){
// if( err ) console.log( 'Error on job with ID ' + job.id );
// console.log('Added job with ID ' + job.id);
// res.send('Added job with ID ' + job.id)
// });
// });
// app.listen(3000, function () {
// console.log('Listening on port 3000!');
// queue.on('job enqueue', function(id, type){
// console.log( 'Job %s got queued of type %s', id, type );
// }).on('job complete', function(id, result){
// kue.Job.get(id, function(err, job){
// if (err) return;
// job.remove(function(err){
// if (err) throw err;
// console.log('removed completed job #%d', job.id);
// });
// });
// });
// });
kue.app.listen(3000);
You can access the UI at the localhost:`PORT_SPECIFIED`.
Example curl:
curl -H "Content-Type: application/json" -X POST -d '[{
"type": "email",
"data": {
"title": "welcome email for tj",
"to": "tj@learnboost.com",
"template": "welcome-email"
},
"options" : {
"attempts": 5,
"priority": "high"
}
},
{
"type": "email",
"data": {
"title": "followup email for pa",
"to": "papa@learnboost.com",
"template": "followup-email"
},
"options" : {
"delay: 5000,
"attempts": 5,
"priority": "high"
}
},
{
"type": "email",
"data": {
"title": "followup email for la",
"to": "lalala@learnboost.com",
"template": "followup-email"
},
"options" : {
"attempts": 5,
"priority": "high"
}
}]' http://localhost:3000/job
var fs = require('fs');
var kue = require('kue')
, queue = kue.createQueue();
queue.process('email', 20, function(job, done){
email(job.data.to, job.id, done);
});
function email(address, id, done) {
if(!address) {
//done('invalid to address') is possible but discouraged
return done(new Error('invalid to address'));
}
// email send stuff...
console.log(new Date() + ': Send the email out to ' + address);
writeThing(`${id}: ${address}`);
done();
}
function writeThing(txt) {
fs.writeFile("./tmp", `${txt}\n` , {flag: "a+"}, function(err) {
if(err) {
return console.log(err);
}
console.log("The file was saved!");
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment