vivocha/koda

Name: koda

Owner: Vivocha

Description: Message Queue for Node.js and MongoDB

Created: 2014-03-12 10:22:19.0

Updated: 2017-05-03 19:20:44.0

Pushed: 2017-05-17 23:28:19.0

Homepage: null

Size: 11

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

koda

Message Queue for Node.js and MongoDB

Producer

k = require('koda')
mongodb = require('mongodb')

odb.MongoClient.connect('mongodb://localhost/test', function(err, db) {
 (err || !db) {
console.error('failed to connect to db', err, db);
process.exit(1);


r koda = k.create(db);
da.on('ready', function() {
console.log('koda ready');
koda.enqueue('job_a', { blabla: true }, function(err, data) {
  console.log('enqueue job_a callback', err, data);
});
koda.enqueue('job_b', { oops: true }, { expires: 5000 /*milliseconds*/, ignoreResult: true }, function(err, data) {
  console.log('enqueue job_b callback', err, data);
});
;

da.on('error', function(err) {
console.error('coda init failed', err, err.stack);
process.exit(1);
;


nction print_queue_stats() {
koda.stats(function (err, result) {
  console.log(err, result);
  setTimeout(print_queue_stats, 5000) ;
})


int_queue_stats()


Worker

k = require('koda')
mongodb = require('mongodb')

odb.MongoClient.connect('mongodb://localhost/test', function(err, db) {
 (err || !db) {
console.error('failed to connect to db', err, db);
process.exit(1);


r koda = k.create(db);

da.on('error', function (err) {
console.error('queue has problems, check database', err)
process.exit(1)


da.on('ready', function () {

// "requeues" jobs that were in assigned state in case of an unexpected restart
koda.requeueStale(['job_a', 'job_b'], function (err, result) {

  function next_job_a() {

    // blocks while new job doesn't arrive
    koda.next('job_a',function(event, finish) {
      console.log('got job A event', event);
      finish(null); // marks jobs as completed (null) or failed (!null)
      next_job_a(); 
    }); 
  }

  function next_job_b() {

    // blocks while new job doesn't arrive
    koda.next('job_b',function(event, finish) {
      console.log('got job B event', event);
      finish(null);
      next_job_b(); 
    }); 
  }

  next_job_a();
  next_job_b();

})





This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.