Skip to content

Commit

Permalink
Set up onReconnect after initial sub on the connection to log-reader.
Browse files Browse the repository at this point in the history
If we set it up before `subscribeAndWait` returns, then we'll end up
with two subscriptions; we don't have the log-reader sub yet, so we
can't stop it when `onReconnect` runs the first time, so we end up with
a redundant subscription. This means that if a real reconnect happens
later, we'll stop the sub that we set up inside `onReconnect`, but not
the initial sub, so we've leaked a sub and end up with duplicate
messages after reconnect.
  • Loading branch information
Emily Stark committed Feb 27, 2014
1 parent 9d5782b commit bc4524b
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions tools/deploy-galaxy.js
Expand Up @@ -457,7 +457,21 @@ exports.logs = function (options) {
throw new Error("Can't listen to messages on the logs collection");

var logsSubscription = null;
// In case of reconnect recover the state so user sees only new logs
try {
logsSubscription =
logReader.subscribeAndWait("logsForApp", options.app,
{ streaming: options.streaming });
} catch (e) {
return handleError(e, galaxy, {
"no-such-app": "No such app: " + options.app
});
}

// In case of reconnect recover the state so user sees only new logs.
// Only set up the onReconnect handler after the subscribe and wait
// has returned; if we set it up before, then we'll end up with two
// subscriptions, because the onReconnect handler will run for the
// first time before the subscribeAndWait returns.
logReader.connection.onReconnect = function () {
logsSubscription && logsSubscription.stop();
var opts = { streaming: options.streaming };
Expand All @@ -476,16 +490,6 @@ exports.logs = function (options) {
);
};

try {
logsSubscription =
logReader.subscribeAndWait("logsForApp", options.app,
{ streaming: options.streaming });
} catch (e) {
return handleError(e, galaxy, {
"no-such-app": "No such app: " + options.app
});
}

return options.streaming ? null : 0;
} finally {
// If not streaming, close the connection to log-reader so that
Expand Down

0 comments on commit bc4524b

Please sign in to comment.