Skip to content
This repository was archived by the owner on Mar 22, 2018. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions config/default.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
rasterizer:
command: phantomjs # phantomjs executable
options:
- '--ssl-protocol=any' # Better support for HTTPS urls!
port: 3001 # internal service port. No need to allow inbound or outbound access to this port
path: '/tmp/' # where the screenshot files are stored
viewport: '1024x600' # browser window size. Height frows according to the content
Expand Down
8 changes: 6 additions & 2 deletions lib/rasterizerService.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ RasterizerService.prototype.rasterizerExitHandler = function (code) {
};

RasterizerService.prototype.startService = function() {
var rasterizer = spawn(this.config.command, ['scripts/rasterizer.js', this.config.path, this.config.port, this.config.viewport]);

var options = this.config.options || [];
var args = options.concat(['scripts/rasterizer.js', this.config.path, this.config.port, this.config.viewport]);
console.log('RasterizerService starting with ' + JSON.stringify(args));
var rasterizer = spawn(this.config.command,args);
rasterizer.stderr.on('data', function (data) {
console.log('phantomjs error: ' + data);
});
Expand Down Expand Up @@ -100,4 +104,4 @@ RasterizerService.prototype.getPath = function() {
return this.config.path;
}

module.exports = RasterizerService;
module.exports = RasterizerService;
7 changes: 4 additions & 3 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,10 @@ module.exports = function(app, useCors) {
var callRasterizer = function(rasterizerOptions, callback) {
request.get(rasterizerOptions, function(error, response, body) {
if (error || response.statusCode != 200) {
console.log('Error while requesting the rasterizer: %s', error.message);
var effectiveError = error ? error : new Error('Bad rasterizer response. StatusCode=['+response.statusCode+']. Body=['+body+']');
console.log('callRasterizer error:', effectiveError.message);
rasterizerService.restartService();
return callback(new Error(body));
return callback(effectiveError);
}
callback(null);
});
Expand Down Expand Up @@ -113,4 +114,4 @@ module.exports = function(app, useCors) {
});
}

};
};
31 changes: 27 additions & 4 deletions scripts/rasterizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,21 +86,44 @@ service = server.listen(port, function(request, response) {
response.write('Error while parsing headers: ' + err.message);
return response.close();
}

// See http://newspaint.wordpress.com/2013/04/25/getting-to-the-bottom-of-why-a-phantomjs-page-load-fails/
page.onResourceError = function(resourceError) {
page.reason = resourceError.errorString;
page.reason_url = resourceError.url;
};

var pageOpenCallbackFired = false;
page.open(url, function(status) {
pageOpenCallbackFired = true;
if (status == 'success') {
window.setTimeout(function () {
page.render(path);
response.statusCode = 200;
response.write('Success: Screenshot saved to ' + path + "\n");
page.release();
response.close();
}, delay);
} else {
response.write('Error: Url returned status ' + status + "\n");
var message = 'PhantomJS could not open the WebPage ['+url+'] because: ' + page.reason;
console.log(message);
response.statusCode = 502;
response.write(message);
page.release();
response.close();
}
});
// must start the response now, or phantom closes the connection
response.statusCode = 200;
response.write('');

// This has been delayed a bit on purpose because once you write the http status code,
// you can't change it in case of error :(
// So we only start writing after 500ms so that the page.open has a time window
// to eventually trigger an error with the appropriate status code
window.setTimeout(function () {
// must start the response now, or phantom closes the connection
if ( pageOpenCallbackFired ) {
response.statusCode = 200;
response.write('');
}
},500);

});