Permalink
Browse files

Initial reaping of stale tests (set for 45 seconds currently)

  • Loading branch information...
1 parent ead24b3 commit a0c9ac7b76593e366fb92f8af3f7bb7a4d7fd111 @davglass committed Jun 13, 2012
Showing with 38 additions and 3 deletions.
  1. +38 −3 lib/hub/agent.js
View
@@ -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);
@@ -30,6 +33,7 @@ function Agent(manager, registration) {
this.connected = true;
this.urlQueue = [];
+ this.currentUrl = null;
EventEmitter2.call(this);
@@ -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;
@@ -97,6 +105,7 @@ Agent.prototype.nextURL = function () {
this.waiting = true;
this.emit("complete");
}
+ this.currentUrl = url;
return url;
};
@@ -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();
@@ -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();

0 comments on commit a0c9ac7

Please sign in to comment.