This repository has been archived by the owner on Jul 21, 2022. It is now read-only.
/
phantomManager.js
158 lines (127 loc) · 4.44 KB
/
phantomManager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*!
* Copyright(c) 2014 Jan Blaha
*
* PhantomManager is responsible of managing pool of phantomjs worker processes
* and distributing pdf rendering tasks to them.
*/
var events = require('events'),
util = require('util'),
find = require('lodash.find'),
numCPUs = require('os').cpus().length,
PhantomWorker = require("./phantomWorker.js");
var PhantomManager = module.exports = function (options) {
this._phantomInstances = [];
this.options = options || {};
this.options.workerEnv = this.options.workerEnv || {};
this.options.numberOfWorkers = this.options.numberOfWorkers || numCPUs;
this.options.timeout = this.options.timeout || 180000;
this.options.host = this.options.host || "127.0.0.1";
this.options.hostEnvVarName = this.options.hostEnvVarName || "PHANTOM_WORKER_HOST"
this.options.portEnvVarName = this.options.portEnvVarName || "PHANTOM_WORKER_PORT";
this._timeouts = [];
this.tasksQueue = [];
};
util.inherits(PhantomManager, events.EventEmitter);
PhantomManager.prototype.start = function (cb) {
var self = this;
process.once("exit", function () {
self.kill();
});
var started = 0;
var workerErrors = [];
var couldNotStartWorkersErr;
var workerMainErrorMsg = '';
for (var i = 0; i < self.options.numberOfWorkers; i++) {
self._phantomInstances.push(new PhantomWorker({
pathToPhantomScript: self.options.pathToPhantomScript,
hostEnvVarName: self.options.hostEnvVarName,
portEnvVarName: self.options.portEnvVarName,
host: self.options.host,
portLeftBoundary: self.options.portLeftBoundary,
portRightBoundary: self.options.portRightBoundary,
phantomPath: self.options.phantomPath,
env: self.options.workerEnv
}));
self._phantomInstances[i].start(function(err) {
if (err) {
if (err.mainReason && workerMainErrorMsg === '') {
workerMainErrorMsg = err.mainReason;
}
workerErrors.push(err);
}
started++;
if (started === self.options.numberOfWorkers) {
if (workerErrors.length) {
couldNotStartWorkersErr = new Error("phantom manager could not start all workers.." + workerMainErrorMsg);
couldNotStartWorkersErr.workerErrors = workerErrors;
return cb(couldNotStartWorkersErr);
}
cb(null);
}
});
}
};
PhantomManager.prototype.kill = function() {
this._timeouts.forEach(function(t) {
clearTimeout(t);
});
this._phantomInstances.forEach(function (i) {
i.kill();
});
};
PhantomManager.prototype.execute = function (options, cb) {
var self = this;
var freePhantomInstance = find(this._phantomInstances, {
isBusy: false
});
if (freePhantomInstance) {
this._executeInWorker(freePhantomInstance, options, cb);
return;
}
this.tasksQueue.push({options: options, cb: cb});
};
PhantomManager.prototype._executeInWorker = function (worker, options, cb) {
var self = this;
var isDone = false;
var callbackWasCalled = false;
var timeout = setTimeout(function () {
self._timeouts.splice(self._timeouts.indexOf(timeout), 1);
if (isDone)
return;
isDone = true;
self.emit("timeout", worker);
worker.recycle(function () {
if (!callbackWasCalled) {
var error = new Error();
error.weak = true;
error.message = "Timeout";
cb(error);
}
self.tryFlushQueue();
});
}, options.timeout || this.options.timeout);
this._timeouts.push(timeout);
worker.execute(options, function (err, result) {
if (isDone)
return;
if (err) {
callbackWasCalled = true;
return cb(err);
}
callbackWasCalled = true;
isDone = true;
self.tryFlushQueue();
cb(null, result);
});
};
PhantomManager.prototype.tryFlushQueue = function () {
if (this.tasksQueue.length === 0)
return;
var freePhantomInstance = find(this._phantomInstances, {
isBusy: false
});
if (!freePhantomInstance)
return;
var task = this.tasksQueue.shift();
this._executeInWorker(freePhantomInstance, task.options, task.cb);
};