Files
meteor/tools/fiber-helpers.js
Emily Stark 5c3b769e44 Clean up galaxy command setup and ssh tunnel cleanup.
Uses cleanup.js to register an exit handler that cleans up the ssh tunnel,
instead of the try/finally. The galaxyCommand wrapper takes care of finding a
galaxy, opening a tunnel, and cleaning up the tunnel when the command
finishes. Commands that want to keep the tunnel open can just use
prepareForGalaxy and not galaxyCommand.

galaxyCommand required refactoring argument parsing a bit; now each command has
an optional argumentParser step that runs before the actual command, which
allows us to wrap the command function knowing that the arguments have already
been parsed (e.g. site is always in argv._[1]).

Also reverts c185b2be because that fix is no longer necessary.
2013-07-15 22:31:07 -07:00

40 lines
1.3 KiB
JavaScript

// node (v8) defaults to only recording 10 lines of stack trace.
// this becomes especially bad when using fibers, since you get deeper
// stack traces that would have been split up between different callbacks
//
// this only affects the `meteor` executable, not server code on
// meteor apps.
Error.stackTraceLimit = Infinity; // http://code.google.com/p/v8/wiki/JavaScriptStackTraceApi
var _ = require("underscore");
var Fiber = require("fibers");
var Future = require("fibers/future");
// runs a function within a fiber. we wrap the entry point into
// meteor.js into a fiber but if you use callbacks that call
// synchronous code you need to wrap those as well.
//
// NOTE: It's probably better to not use callbacks. Instead you can
// use Futures to generate synchronous equivalents.
exports.inFiber = function(func) {
return function(/*arguments*/) {
var self = this;
var args = arguments;
new Fiber(function () {
func.apply(self, args);
}).run();
};
};
exports.parallelEach = function (collection, callback, context) {
var futures = _.map(collection, function () {
var args = _.toArray(arguments);
return function () {
return callback.apply(context, args);
}.future()();
});
Future.wait(futures);
// Throw if any threw.
_.each(futures, function (f) { f.get(); });
};