hot-sauce

1.0.7 • Public • Published

Hot Sauce

Couchbase Backed Job Scheduling Management API

Build Status Coverage Status npm

Installation

npm install hot-sauce

Testing

npm run test

Code Coverage

Code Coverage provided by Istanbul with hooks for coveralls. To see coverage report run

npm run cover

Usage - built-in start/stop methods

This module can be incorporated into an existing application with applicable start/stop commands

var HotSauce = require('hot-sauce');
 
var config = {
    port: 3000,
    couchbase: {
        cluster: ['http://couchbase.host:8091'],
        bucket: {
            name: 'bucket_name',
            password: 'p@$$w0rd'
        }
    },
    logger: {
        console: {enabled: true, options: {level: 'debug'}},
        file: {enabled: false, options: {level: 'debug', filename: "hot-sauce.log"}}
    }
};
 
var hotSauce = new HotSauce(config);
 
hotSauce.start();

Usage - subapp in existing app

This module can be incorporated into an existing application as a sub-app

var HotSauce = require('hot-sauce');
 
var config = {
    port: 3000,
    couchbase: {
        cluster: ['http://couchbase.host:8091'],
        bucket: {
            name: 'bucket_name',
            password: 'p@$$w0rd'
        }
    },
    logger: {
        console: {enabled: true, options: {level: 'debug'}},
        file: {enabled: false, options: {level: 'debug', filename: "hot-sauce.log"}}
    }
};
 
var hotSauce = new HotSauce(config);
 
var app = express();
//require an apiKey on all routes
app.all('*', function(req, res, next) {
    if(req.query.apiKey) {
        next();
    } else {
        res.status(400).json({error: 'apiKey is required'});
    }
});
 
//mount the hot sauce app at /api/
app.use('/api', hotSauce.app);
 
//start server
var server = http.createServer(app);
server.listen(config.port);

Configuration

{
    port: 3000, //port for server to listen on
    couchbase: { 
        cluster: ['http://couchbase.host:8091'],  //array of cochbase cluster nodes
        bucket: {
            name: 'bucket_name',  //bucket to use for storage
            password: 'p@$$w0rd'
        }
    },
    logger: { 
        console: {enabled: true, options: {level: 'debug'}},
        file: {enabled: false, options: {level: 'debug', filename: "hot-sauce.log"}}
    }
};

Couchbase Configuration

The application requires the existence of 2 views in the bucket

GetAllJobs

function (doc, meta) {
  emit(meta.id, doc);
}

GetJobIfAvailable

function (doc, meta) {
  if(doc.isActive === null || doc.isActive === undefined || doc.isActive === true){
    if(doc.locking && doc.locking.locked != true){
      if(doc.schedule && doc.schedule.future_instances.length > 0){
      //only find unlocked jobs
        var instances = doc.schedule.future_instances;
        for(var i=0; i<instances.length; i++){
          var key = dateToArray(instances[i]);
      emit(key, doc.code);
        }
      } 
    }
  }
}

GetJobsMaintenance

function (doc, meta) {
  var key = [];
  key.push(doc.locking ? doc.locking.locked : false);
  key.push(doc.id);
  emit(key, doc);
}

Routes

  • GET /jobs Index of all jobs stored in couchbase ** Example Response
{
    "id": "52",
    "name": "job",
    "code": "code_52",
    "description": "desc",
    "jobData": {},
    "isActive": true,
    "schedule": {
        "expirationThreshold": 20000,
        "cron": "*/12 52 * * * *",
        "future_instances": [
            "2015-08-03T19:52:00.000Z",
            "2015-08-03T19:52:12.000Z",
            "2015-08-03T19:52:24.000Z",
            "2015-08-03T19:52:36.000Z",
            "2015-08-03T19:52:48.000Z"
        ]
    },
    "locking": {},
    "lastModified": "2015-08-03T19:06:55.502Z"
}
  • GET /jobs/:id Detail on specific job

  • GET /jobs/available?codes=code1,code2,code3&caller=someone Given input codes, get & lock an available job by someone. Margin of error for the query is 5 seconds i.e it is feasible to get a job scheduled to run upto 5 seconds in the future.

  • GET /jobs/:id/unlock?caller=someone
    Unlocks a locked job

  • POST /jobs Create/Upload a job ** Example POST request

{
    "id": "52",
    "name": "job",
    "isActive": false,
    "code": "every52nd",
    "description": "desc",
    "jobData": {},
    "schedule": {
        "expirationThreshold": 20000,
        "cron": "*/12 52 * * * *"
    },
    "locking": {}
}

Package Sidebar

Install

npm i hot-sauce

Weekly Downloads

15

Version

1.0.7

License

MIT

Last publish

Collaborators

  • binaryalchemist
  • enigmatic00
  • jdonnell
  • johndietz