Skip to content

Commit

Permalink
Initial reaping of stale tests (set for 45 seconds currently)
Browse files Browse the repository at this point in the history
  • Loading branch information
davglass committed Jun 13, 2012
1 parent ead24b3 commit a0c9ac7
Showing 1 changed file with 38 additions and 3 deletions.
41 changes: 38 additions & 3 deletions lib/hub/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@ function Agent(manager, registration) {
this.id = registration.id;
this.ua = registration.ua;

var abortNow;

this.ttl = registration.ttl || Agent.TTL;

if (!this.id) {
throw new Error("ID required.");
} else if (!this.ua) {
throw new Error("UA required.");
abortNow = 'Unknown Browser, probably a reattach after an error. Needs to be aborted';
this.ua = abortNow;
}

this.name = parseUA(this.ua);
Expand All @@ -30,6 +33,7 @@ function Agent(manager, registration) {
this.connected = true;

this.urlQueue = [];
this.currentUrl = null;

EventEmitter2.call(this);

Expand All @@ -44,7 +48,11 @@ function Agent(manager, registration) {

this.setupEvents();
this.connect(registration.socket);


if (abortNow) {
this.abort();
this.emit("agentError", abortNow);
}
}

Agent.TTL = 3600;
Expand Down Expand Up @@ -97,6 +105,7 @@ Agent.prototype.nextURL = function () {
this.waiting = true;
this.emit("complete");
}
this.currentUrl = url;

return url;
};
Expand Down Expand Up @@ -127,6 +136,14 @@ Agent.prototype.unload = function () {
this.emit("disconnect");
};

Agent.prototype.abort = function () {
this.next(); //to next test
this.emit("abort");
this.emit("agentError", {
message: "Agent timed out running test: " + this.currentUrl
});
};

Agent.prototype.ping = function () {
this.connected = true;
this.seen = new Date();
Expand All @@ -141,16 +158,34 @@ Agent.prototype.alive = function () {
return this.connected || !this.expired();
};

function AgentManager(hub) {
function AgentManager(hub, ttl) {
this.hub = hub;
this.agents = {};
EventEmitter2.call(this, {
verbose: true
});

this.ttl = ttl || AgentManager.REAP_TTL;

this._startReap();
}

util.inherits(AgentManager, EventEmitter2);

AgentManager.REAP_TTL = (45 * 1000); //Default reap timeout

AgentManager.prototype._startReap = function () {
this._reap = setInterval(this.reap.bind(this), this.ttl);
};

AgentManager.prototype.reap = function () {
this.getAgents().forEach(function (agent) {
if (agent.expired()) {
agent.abort();
}
});
};

AgentManager.prototype.getAvailableAgents = function () {
return this.getAgents().filter(function (agent) {
return agent.available();
Expand Down

0 comments on commit a0c9ac7

Please sign in to comment.