Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow specification of timeout and number of threads in config file #358

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.0.0-p247
2.0.0-p247
9 changes: 5 additions & 4 deletions lib/wraith/javascript/_phantom__common.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module.exports = function (config) {
selector = systemArgs[4],
globalBeforeCaptureJS = systemArgs[5],
pathBeforeCaptureJS = systemArgs[6],
timeoutMs = parseInt(systemArgs[7]),
dimensionsProcessed = 0,
currentDimensions;

Expand Down Expand Up @@ -74,7 +75,7 @@ module.exports = function (config) {
function resizeAndCaptureImage() {
console.log('Snapping ' + url + ' at: ' + currentDimensions.viewportWidth + 'x' + currentDimensions.viewportHeight);
resize();
setTimeout(captureImage, 1000); // give page time to re-render properly
setTimeout(captureImage, timeoutMs); // give page time to re-render properly
}

function captureImage() {
Expand All @@ -90,7 +91,7 @@ module.exports = function (config) {
if (helper.takingMultipleScreenshots(dimensions) && dimensionsProcessed < dimensions.length) {
currentDimensions = dimensions[dimensionsProcessed];
image_name = helper.replaceImageNameWithDimensions(image_name, currentDimensions);
setTimeout(resizeAndCaptureImage, 1000);
setTimeout(resizeAndCaptureImage, timoutMs);
}
else {
// prevent CI from failing from 'Unsafe JavaScript attempt to access frame with URL about:blank from frame with URL' errors
Expand All @@ -109,12 +110,12 @@ module.exports = function (config) {
// rendering, just in case the page kicks off another request
if (current_requests < 1) {
clearTimeout(final_timeout);
last_request_timeout = setTimeout(runSetupJavaScriptThenCaptureImage, 1000);
last_request_timeout = setTimeout(runSetupJavaScriptThenCaptureImage, timeoutMs);
}

// Sometimes, straggling requests never make it back, in which
// case, timeout after 5 seconds and render the page anyway
final_timeout = setTimeout(runSetupJavaScriptThenCaptureImage, 5000);
}

}
}
4 changes: 2 additions & 2 deletions lib/wraith/save_images.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def prepare_widths_for_cli(width)

def capture_page_image(browser, url, width, file_name, selector, global_before_capture, path_before_capture)

command = "#{browser} #{wraith.phantomjs_options} '#{wraith.snap_file}' '#{url}' \"#{width}\" '#{file_name}' '#{selector}' '#{global_before_capture}' '#{path_before_capture}'"
command = "#{browser} #{wraith.phantomjs_options} '#{wraith.snap_file}' '#{url}' \"#{width}\" '#{file_name}' '#{selector}' '#{global_before_capture}' '#{path_before_capture}' #{wraith.timeout_ms}"

# @TODO - uncomment the following line when we add a verbose mode
# puts command
Expand All @@ -80,7 +80,7 @@ def run_command(command)
end

def parallel_task(jobs)
Parallel.each(jobs, :in_threads => 8) do |_label, _path, width, url, filename, selector, global_before_capture, path_before_capture|
Parallel.each(jobs, :in_threads => wraith.num_threads) do |_label, _path, width, url, filename, selector, global_before_capture, path_before_capture|
begin
attempt_image_capture(width, url, filename, selector, global_before_capture, path_before_capture, 5)
rescue => e
Expand Down
8 changes: 8 additions & 0 deletions lib/wraith/wraith.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ def before_capture
@config["before_capture"] ? convert_to_absolute(@config["before_capture"]) : "false"
end

def num_threads
@config['num_threads'] ? @config['num_threads'] : 8
end

def timeout_ms
@config['timeout_ms'] ? @config['timeout_ms'] : 1000
end

def widths
@config["screen_widths"]
end
Expand Down
21 changes: 21 additions & 0 deletions spec/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,27 @@
it "contains shot options" do
expect(wraith.config).to include "directory" => "shots"
end

it 'returns default values for num_threads' do
expect(wraith.num_threads).to eq 8
end

it 'returns default values for timeout_ms' do
expect(wraith.timeout_ms).to eq 1000
end

context 'non-standard config values' do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, for clarity I'd refer to these as 'overridden' values rather than 'non-standard' (which suggests Strings instead of ints, or non-ASCII characters or something)

let(:config) { YAML.load "browser: phantomjs\nnum_threads: 2\ntimeout_ms: 4000"}
let(:non_standard_wraith) { Wraith::Wraith.new( config, true) }

it 'returns non standard values for num_threads if specified in config' do
expect(non_standard_wraith.num_threads).to eq 2
end

it 'returns non_standard values for timeout_ms if specified in config' do
expect(non_standard_wraith.timeout_ms).to eq 4000
end
end
end

describe "When creating a wraith worker" do
Expand Down