This repository has been archived by the owner on Jul 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Global poller for updating worker status
Uses a shared poller to fetch and update the status of workers; helps reduce the no. of requests made to BrowserStack. Ref #30
- Loading branch information
Showing
4 changed files
with
159 additions
and
37 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
var Worker = require('./worker') | ||
|
||
/** | ||
* Tracks worker state across runs. | ||
*/ | ||
function WorkerManager () { | ||
this._pollHandle = null | ||
this.workers = {} | ||
this.isPolling = false | ||
} | ||
|
||
WorkerManager.prototype.registerWorker = function registerWorker (workerData) { | ||
if (this.workers[workerData.id]) { | ||
this.unregisterWorker(this.workers[workerData.id]) | ||
} | ||
|
||
var worker = new Worker(workerData) | ||
worker.emit('status', worker.status) | ||
|
||
this.workers[workerData.id] = worker | ||
return worker | ||
} | ||
|
||
WorkerManager.prototype.unregisterWorker = function unregisterWorker (worker) { | ||
worker.emit('delete', worker) | ||
worker.removeAllListeners() | ||
|
||
delete this.workers[worker.id] | ||
return worker | ||
} | ||
|
||
WorkerManager.prototype.updateWorker = function updateWorker (workerData) { | ||
var workers = this.workers | ||
|
||
if (workers[workerData.id]) { | ||
var worker = workers[workerData.id] | ||
var prevStatus = worker.status | ||
|
||
Object.keys(workerData).forEach(function (k) { | ||
worker[k] = workerData[k] | ||
}) | ||
|
||
if (worker.status !== prevStatus) { | ||
worker.emit('status', worker.status) | ||
} | ||
} | ||
} | ||
|
||
WorkerManager.prototype.startPolling = function startPolling (client, pollingTimeout, callback) { | ||
if (this.isPolling) { | ||
return | ||
} | ||
|
||
var self = this | ||
this.isPolling = true | ||
|
||
client.getWorkers(function (err, updatedWorkers) { | ||
if (err) { | ||
self.isPolling = false | ||
return (callback ? callback(err) : null) | ||
} | ||
|
||
var activeWorkers = (updatedWorkers || []).reduce(function (o, worker) { | ||
o[worker.id] = worker | ||
return o | ||
}, {}) | ||
|
||
Object.keys(self.workers).forEach(function (workerId) { | ||
if (activeWorkers[workerId]) { | ||
// process updates | ||
self.updateWorker(activeWorkers[workerId]) | ||
} else { | ||
// process deletions | ||
self.unregisterWorker(self.workers[workerId]) | ||
} | ||
}) | ||
|
||
self._pollHandle = setTimeout(function () { | ||
self.isPolling = false | ||
self.startPolling(client, pollingTimeout, callback) | ||
}, pollingTimeout) | ||
}) | ||
} | ||
|
||
WorkerManager.prototype.stopPolling = function stopPolling () { | ||
if (this._pollHandle) { | ||
clearTimeout(this._pollHandle) | ||
this._pollHandle = null | ||
} | ||
|
||
this.isPolling = false | ||
} | ||
|
||
// expose a single, shared instance of WorkerManager | ||
module.exports = new WorkerManager() |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
var EventEmitter = require('events').EventEmitter | ||
var util = require('util') | ||
|
||
function Worker (data) { | ||
EventEmitter.call(this) | ||
|
||
if (typeof data === 'object' && !Array.isArray(data)) { | ||
var self = this | ||
|
||
Object.keys(data).forEach(function (k) { | ||
self[k] = data[k] | ||
}) | ||
} | ||
} | ||
|
||
util.inherits(Worker, EventEmitter) | ||
|
||
module.exports = Worker |