Created
April 13, 2014 23:11
-
-
Save jtremback/10606044 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var mongoose = require('mongoose'); | |
var Account = mongoose.model('Account'); | |
var Tip = mongoose.model('Tip'); | |
var models = { | |
Tip: Tip, | |
Account: Account | |
}; | |
var queue = []; | |
exports.push = function (model, method, args) { | |
var command = { | |
model: model, | |
method: method, | |
args: args | |
}; | |
queue.push(command); // Add command to back of queue | |
}; | |
var worker = function () { | |
function callback (err) { | |
if (err) { // For bad, unexpected errors, log it and crash. (Expected errors are logged to db inside the method) | |
console.error(err); | |
process.exit(); | |
} | |
worker(); | |
} | |
process.nextTick(function () { | |
if (queue.length) { // If there are commands in the queue | |
var command = queue.shift(); // Take command off front of queue | |
command.args.push(callback); // Add callback | |
models[command.model][command.method].apply(this, command.args); // Run command | |
} | |
setTimeout(function () { | |
worker(); | |
}, 100); // Timeout on rechecking an empty queue (maybe this is uneccesary?) | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment