Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

rm files never used, add try catch for JSON.parse, rm comments never …

…used
  • Loading branch information...
commit 505b13f8f8af8fdc8f1701192d59d2cc4aac026b 1 parent 2040a98
@iwater iwater authored
View
1  config.json
@@ -1 +0,0 @@
-{}
View
12 lib/monitor.js
@@ -1,12 +0,0 @@
-var util = require("util");
-var events = require("events");
-
-function Monitor() {
- events.EventEmitter.call(this);
-}
-
-util.inherits(Monitor, events.EventEmitter);
-
-exports.createMonitor = function(){
- return new Monitor();
-};
View
30 lib/queue.js
@@ -1,12 +1,24 @@
var request = require('request');
-var tryPop = function( url, queue, cb ){
+var tryPop = function( url, cb ){
request(url, function (error, response, body) {
- if (!error && response.statusCode == 200) {
- cb( null, JSON.parse(body) );
- }
- if (!error && response.statusCode == 404) {
- cb( 'empty' );
+ if ( error ) {
+ cb( error );
+ } else {
+ switch( response.statusCode ) {
+ case 200:
+ try {
+ cb( null, JSON.parse(body) );
+ } catch (e) {
+ cb( 'not json response' );
+ }
+ break;
+ case 404:
+ cb( 'empty' );
+ break;
+ default:
+ cb( 'unknown' );
+ }
}
})
};
@@ -17,7 +29,7 @@ var Queue = function(base, queue){
};
Queue.prototype.dequeue = function(cb){
- tryPop( this.base + '/' + this.queue, this.queue, cb );
+ tryPop( this.base + '/' + this.queue, cb );
};
Queue.prototype.enqueue = function(uri, cb){
@@ -26,7 +38,9 @@ Queue.prototype.enqueue = function(uri, cb){
headers:{'Content-Type': 'application/x-www-form-urlencoded'},
body : 'uri=' + uri
}, function (error, response, body) {
- console.log(body);
+ if( error ) {
+ console.log( [uri, error] );
+ }
});
};
View
10 lib/redis.js
@@ -15,8 +15,16 @@ Queue.prototype.enqueue = function(queue, task){
};
Queue.prototype.dequeue = function(queue, cb){
+ var self = this;
this.client.rpop(queue, function (err, obj){
- cb( JSON.parse( obj ) );
+ try {
+ cb( JSON.parse( obj ) );
+ } catch (e){
+ console.log( e );
+ process.nextTick(function () {
+ self.dequeue( queue, cb );
+ });
+ }
});
};
View
20 queuer-server.js
@@ -1,27 +1,11 @@
var express = require('express');
var _ = require('underscore');
-var mon = require('./lib/monitor');
var argv = require('optimist').argv;
var Queue = require('./lib/redis');
+var events = require('events');
var de = require('devent').createDEvent('queuer');
var startup = new Date();
-/*
-var hook = new Hook( {
- name: 'hook-server',
- 'hook-host': '0.0.0.0'
-});
-
-hook.on('hook::ready', function(){
- hook.on('*::task-finished', function( task ){
- monitor.emit('task-' + task.uri, 'finished');
- });
- hook.on('*::task-error', function( task ){
- monitor.emit('task-' + task.uri, 'error');
- });
-});
-
-hook.connect();*/
de.on('task-finished', function( task ){
//console.log(['finish', task]);
@@ -35,7 +19,7 @@ de.on('task-error', function( task ){
var queues = new Queue( argv.redis_port, argv.redis_host );
-var monitor = mon.createMonitor();
+var monitor = new events.EventEmitter();
var app = express.createServer();
app.use(express.bodyParser());
Please sign in to comment.
Something went wrong with that request. Please try again.