Skip to content

Instantly share code, notes, and snippets.

@dturton
Created January 21, 2018 13:31
Show Gist options
  • Select an option

  • Save dturton/7356f81718080841f14dfe14aa4052ff to your computer and use it in GitHub Desktop.

Select an option

Save dturton/7356f81718080841f14dfe14aa4052ff to your computer and use it in GitHub Desktop.
node.js kue and express
import kue from 'kue'
import express from 'express'
const app = express();
const jobs = kue.createQueue()
app.use('/api', kue.app);
app.get('/job', (req, res) => {
let job = jobs.create( 'email', {
title: 'emailing '
})
job.on('complete', function(){
res.send("Job complete");
}).on('failed', function(){
res.send("Job failed");
}).on('progress', function(progress){
console.log('job #' + job.id + ' ' + progress + '% complete');
})
job.save((err) => {
if( !err ) res.json(job.id);
});
})
jobs.process('email', function(job, done){
setTimeout(() => {
done()
}, 5000);
});
app.listen(8080, function() {
console.log("Listening on http://localhost:8080");
});
@bbarr
Copy link
Copy Markdown

bbarr commented Jan 23, 2018

You want the consumer and the producer to be on different processes.

I haven't checked the code below, and it is 1AM, so... take with a grain of salt, @dturton ! :)

// server.js
import kue from 'kue'
import express from 'express'

const app = express();

const jobs = kue.createQueue()

app.post('/job', (req, res) => {
  const job = jobs.create( 'email', {
      title: 'emailing '
  })
  res.send(job.id)
})

app.get('/job/:id', (req, res) => {
  kue.Job.get(id, (e, job) => {
    res.send(job.progress === 100)
  })
})
// worker.js
import kue from 'kue'

const jobs = kue.createQueue()

jobs.process('email', function(job, done){
  setTimeout(() => {
    job.progress(1, 1) // just 1 and 1 cause we are only processing 1 email at the moment
    done()
  }, 5000);
});

@bbarr
Copy link
Copy Markdown

bbarr commented Jan 23, 2018

node server.js
node worker.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment