Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

its node child processes all the way down

  • Loading branch information...
commit f5741299762b113339b44b2770c404dc7819c71c 1 parent a2f4806
Max Ogden authored
10 changes.js
View
@@ -2,16 +2,16 @@ var sys = require('sys')
, couchdb = require('./lib/node-couchdb-min/couchdb')
, archiver = require('./feed-archiver');
-exports.listener = function(change) {
- if (!change.doc) return;
+exports.listener = function(doc) {
+ if (!doc) return;
- if (change.doc.feed && change.doc.db) {
+ if (doc.feed && doc.db) {
var feedSaveFunc = function (data) {
for (var item in data.channel.item) {
- archiver.saveItem(data.channel.item[item], new couchdb.Db(change.doc.db), "description");
+ archiver.saveItem(data.channel.item[item], new couchdb.Db(doc.db), "description");
}
}
- archiver.processFeed(change.doc.feed, feedSaveFunc);
+ archiver.processFeed(doc.feed, feedSaveFunc);
}
}
6 main.js → couch-watcher.js
View
@@ -1,3 +1,5 @@
+// adapted from https://github.com/szayat/node.couch.js
+
var request = require('request')
, sys = require('sys')
, events = require('events')
@@ -23,7 +25,7 @@ function createDatabaseListener (uri, db) {
db.onDesignDoc(change.doc);
}
db.ids.forEach(function (id) {
- db.ddocs[id]._changes_process().stdin.write(JSON.stringify(["change", change, uri ])+'\n');
+ db.ddocs[id]._changes_process().stdin.write(JSON.stringify(["change", change.doc ])+'\n');
})
}
, onDesignDoc: function (doc) {
@@ -42,7 +44,7 @@ function createDatabaseListener (uri, db) {
if (doc.changes) {
// start up the process
sys.puts("Starting process for "+doc._id)
- var p = child.spawn(process.execPath, [path.join(__dirname, 'child.js')]);
+ var p = child.spawn(process.execPath, [path.join(__dirname, 'db-watcher.js')]);
p.stderr.on("data", function (chunk) {sys.error("data error: " + chunk.toString())})
p.stdin.write(JSON.stringify(["ddoc", doc])+'\n');
db.ddocs[doc._id]._changes_process = function(){return p};
35 db-watcher.js
View
@@ -0,0 +1,35 @@
+// adapted from https://github.com/szayat/node.couch.js
+
+var stdin = process.openStdin();
+var vm = require('vm');
+var sys = require('sys');
+
+stdin.setEncoding('utf8');
+
+var buffer = ''
+ , docs = {}
+ , listenerDoc
+ ;
+
+var spawnFeedProcess = function( doc ) {
+ sys.puts( "Starting process for " + doc._id )
+ var p = child.spawn( process.execPath, [ path.join(__dirname, 'feed-watcher.js') ] );
+ p.stderr.on( "data", function ( chunk ) { sys.error( "data error: " + chunk.toString() ) } )
+ p.stdin.write(JSON.stringify(["listenerDoc", listenerDoc])+'\n');
+ p.stdin.write(JSON.stringify(["doc", doc])+'\n');
+ docs[ doc._id ]._feed_process = function() { return p };
+}
+
+stdin.on('data', function (chunk) {
+ buffer += chunk.toString();
+ while (buffer.indexOf('\n') !== -1) {
+ line = buffer.slice(0, buffer.indexOf('\n'));
+ buffer = buffer.slice(buffer.indexOf('\n') + 1);
+ var obj = JSON.parse(line);
+ if ((obj[1]) === "ddoc") {
+ listenerDoc = obj[1];
+ } else if (obj[0] === "doc") {
+ spawnFeedProcess(obj[1]);
+ }
+ }
+});
28 child.js → feed-watcher.js
View
@@ -1,4 +1,4 @@
-// from https://github.com/szayat/node.couch.js
+// adapted from https://github.com/szayat/node.couch.js
var stdin = process.openStdin();
var vm = require('vm');
@@ -6,31 +6,43 @@ var sys = require('sys');
stdin.setEncoding('utf8');
-var buffer = '';
-var listener;
+var buffer = ''
+ , feeds = {}
+ , listener
+ ;
var loadModule = function (doc) {
+ // eval the changes parser from the ddoc
var wrapper = "(function (exports, require, module, __filename, __dirname) { "
+ doc.changes
+ "\n});";
-
var module = {exports:{},id:'changes'};
-
var compiledWrapper = vm.runInThisContext(wrapper);
var p = compiledWrapper.apply(doc, [module.exports, require, module]);
return module.exports;
}
+function fetchFeed (doc) {
+ var fetch = function () {
+ var starttime = new Date();
+ listener(doc);
+ var endtime = new Date();
+ setTimeout(fetch, doc.interval ? doc.interval : (((endtime - starttime) * 5) + 300000));
+ }
+
+ fetch();
+}
+
stdin.on('data', function (chunk) {
buffer += chunk.toString();
while (buffer.indexOf('\n') !== -1) {
line = buffer.slice(0, buffer.indexOf('\n'));
buffer = buffer.slice(buffer.indexOf('\n') + 1);
var obj = JSON.parse(line);
- if ((obj[0]) === "ddoc") {
+ if ((obj[0]) === "listenerDoc") {
listener = loadModule(obj[1]).listener;
- } else if (obj[0] === "change") {
- listener(obj[1], obj[2]);
+ } else if (obj[0] === "doc") {
+ fetchFeed(obj[1]);
}
}
});
Please sign in to comment.
Something went wrong with that request. Please try again.