Use Net::HTTP::Persistent for requests to Poseidon
This commit is contained in:
1
Gemfile
1
Gemfile
@ -20,6 +20,7 @@ gem 'json_schemer'
|
|||||||
gem 'js-routes'
|
gem 'js-routes'
|
||||||
gem 'kramdown'
|
gem 'kramdown'
|
||||||
gem 'mimemagic'
|
gem 'mimemagic'
|
||||||
|
gem 'net-http-persistent'
|
||||||
gem 'nokogiri'
|
gem 'nokogiri'
|
||||||
gem 'pagedown-bootstrap-rails'
|
gem 'pagedown-bootstrap-rails'
|
||||||
gem 'pg'
|
gem 'pg'
|
||||||
|
@ -140,6 +140,7 @@ GEM
|
|||||||
chronic (0.10.2)
|
chronic (0.10.2)
|
||||||
coderay (1.1.3)
|
coderay (1.1.3)
|
||||||
concurrent-ruby (1.1.9)
|
concurrent-ruby (1.1.9)
|
||||||
|
connection_pool (2.2.5)
|
||||||
crack (0.4.5)
|
crack (0.4.5)
|
||||||
rexml
|
rexml
|
||||||
crass (1.0.6)
|
crass (1.0.6)
|
||||||
@ -279,6 +280,8 @@ GEM
|
|||||||
multi_xml (0.6.0)
|
multi_xml (0.6.0)
|
||||||
multipart-post (2.1.1)
|
multipart-post (2.1.1)
|
||||||
nested_form (0.3.2)
|
nested_form (0.3.2)
|
||||||
|
net-http-persistent (4.0.0)
|
||||||
|
connection_pool (~> 2.2)
|
||||||
netrc (0.11.0)
|
netrc (0.11.0)
|
||||||
newrelic_rpm (8.1.0)
|
newrelic_rpm (8.1.0)
|
||||||
nio4r (2.5.8)
|
nio4r (2.5.8)
|
||||||
@ -574,6 +577,7 @@ DEPENDENCIES
|
|||||||
listen
|
listen
|
||||||
mimemagic
|
mimemagic
|
||||||
mnemosyne-ruby
|
mnemosyne-ruby
|
||||||
|
net-http-persistent
|
||||||
newrelic_rpm
|
newrelic_rpm
|
||||||
nokogiri
|
nokogiri
|
||||||
nyan-cat-formatter
|
nyan-cat-formatter
|
||||||
@ -620,4 +624,4 @@ DEPENDENCIES
|
|||||||
whenever
|
whenever
|
||||||
|
|
||||||
BUNDLED WITH
|
BUNDLED WITH
|
||||||
2.2.23
|
2.2.29
|
||||||
|
@ -21,8 +21,7 @@ class Runner::Strategy::Poseidon < Runner::Strategy
|
|||||||
|
|
||||||
def self.sync_environment(environment)
|
def self.sync_environment(environment)
|
||||||
url = "#{config[:url]}/execution-environments/#{environment.id}"
|
url = "#{config[:url]}/execution-environments/#{environment.id}"
|
||||||
connection = Faraday.new nil, ssl: {ca_file: config[:ca_file]}
|
response = http_connection.put url, environment.to_json
|
||||||
response = connection.put url, environment.to_json, headers
|
|
||||||
return true if [201, 204].include? response.status
|
return true if [201, 204].include? response.status
|
||||||
|
|
||||||
Rails.logger.warn("Could not create execution environment in Poseidon, got response: #{response.as_json}")
|
Rails.logger.warn("Could not create execution environment in Poseidon, got response: #{response.as_json}")
|
||||||
@ -39,8 +38,7 @@ class Runner::Strategy::Poseidon < Runner::Strategy
|
|||||||
inactivityTimeout: config[:unused_runner_expiration_time].seconds,
|
inactivityTimeout: config[:unused_runner_expiration_time].seconds,
|
||||||
}
|
}
|
||||||
Rails.logger.debug { "#{Time.zone.now.getutc.inspect}: Requesting new runner at #{url}" }
|
Rails.logger.debug { "#{Time.zone.now.getutc.inspect}: Requesting new runner at #{url}" }
|
||||||
connection = Faraday.new nil, ssl: {ca_file: config[:ca_file]}
|
response = http_connection.post url, body.to_json
|
||||||
response = connection.post url, body.to_json, headers
|
|
||||||
|
|
||||||
case response.status
|
case response.status
|
||||||
when 200
|
when 200
|
||||||
@ -60,8 +58,7 @@ class Runner::Strategy::Poseidon < Runner::Strategy
|
|||||||
|
|
||||||
def destroy_at_management
|
def destroy_at_management
|
||||||
Rails.logger.debug { "#{Time.zone.now.getutc.inspect}: Destroying runner at #{runner_url}" }
|
Rails.logger.debug { "#{Time.zone.now.getutc.inspect}: Destroying runner at #{runner_url}" }
|
||||||
connection = Faraday.new nil, ssl: {ca_file: self.class.config[:ca_file]}
|
response = self.class.http_connection.delete runner_url
|
||||||
response = connection.delete runner_url, nil, self.class.headers
|
|
||||||
self.class.handle_error response unless response.status == 204
|
self.class.handle_error response unless response.status == 204
|
||||||
rescue Faraday::Error => e
|
rescue Faraday::Error => e
|
||||||
raise Runner::Error::FaradayError.new("Request to Poseidon failed: #{e.inspect}")
|
raise Runner::Error::FaradayError.new("Request to Poseidon failed: #{e.inspect}")
|
||||||
@ -83,8 +80,7 @@ class Runner::Strategy::Poseidon < Runner::Strategy
|
|||||||
# First, clean the workspace and second, copy all files to their location.
|
# First, clean the workspace and second, copy all files to their location.
|
||||||
# This ensures that no artefacts from a previous submission remain in the workspace.
|
# This ensures that no artefacts from a previous submission remain in the workspace.
|
||||||
body = {copy: copy, delete: ['./']}
|
body = {copy: copy, delete: ['./']}
|
||||||
connection = Faraday.new nil, ssl: {ca_file: self.class.config[:ca_file]}
|
response = self.class.http_connection.patch url, body.to_json
|
||||||
response = connection.patch url, body.to_json, self.class.headers
|
|
||||||
return if response.status == 204
|
return if response.status == 204
|
||||||
|
|
||||||
Runner.destroy(@allocation_id) if response.status == 400
|
Runner.destroy(@allocation_id) if response.status == 400
|
||||||
@ -154,6 +150,12 @@ class Runner::Strategy::Poseidon < Runner::Strategy
|
|||||||
@headers ||= {'Content-Type' => 'application/json', 'Poseidon-Token' => config[:token]}
|
@headers ||= {'Content-Type' => 'application/json', 'Poseidon-Token' => config[:token]}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.http_connection
|
||||||
|
@http_connection ||= Faraday.new(ssl: {ca_file: config[:ca_file]}, headers: headers) do |faraday|
|
||||||
|
faraday.adapter :net_http_persistent
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def self.parse(response)
|
def self.parse(response)
|
||||||
JSON.parse(response.body).deep_symbolize_keys
|
JSON.parse(response.body).deep_symbolize_keys
|
||||||
rescue JSON::ParserError => e
|
rescue JSON::ParserError => e
|
||||||
@ -167,8 +169,7 @@ class Runner::Strategy::Poseidon < Runner::Strategy
|
|||||||
url = "#{runner_url}/execute"
|
url = "#{runner_url}/execute"
|
||||||
body = {command: command, timeLimit: @execution_environment.permitted_execution_time}
|
body = {command: command, timeLimit: @execution_environment.permitted_execution_time}
|
||||||
Rails.logger.debug { "#{Time.zone.now.getutc.inspect}: Preparing command execution at #{url}: #{command}" }
|
Rails.logger.debug { "#{Time.zone.now.getutc.inspect}: Preparing command execution at #{url}: #{command}" }
|
||||||
connection = Faraday.new nil, ssl: {ca_file: self.class.config[:ca_file]}
|
response = self.class.http_connection.post url, body.to_json
|
||||||
response = connection.post url, body.to_json, self.class.headers
|
|
||||||
|
|
||||||
case response.status
|
case response.status
|
||||||
when 200
|
when 200
|
||||||
|
@ -109,7 +109,7 @@ describe Runner::Strategy::Poseidon do
|
|||||||
|
|
||||||
it 'raises an error' do
|
it 'raises an error' do
|
||||||
faraday_connection = instance_double 'Faraday::Connection'
|
faraday_connection = instance_double 'Faraday::Connection'
|
||||||
allow(Faraday).to receive(:new).and_return(faraday_connection)
|
allow(described_class).to receive(:http_connection).and_return(faraday_connection)
|
||||||
%i[post patch delete].each {|message| allow(faraday_connection).to receive(message).and_raise(Faraday::TimeoutError) }
|
%i[post patch delete].each {|message| allow(faraday_connection).to receive(message).and_raise(Faraday::TimeoutError) }
|
||||||
expect { action.call }.to raise_error(Runner::Error::FaradayError)
|
expect { action.call }.to raise_error(Runner::Error::FaradayError)
|
||||||
end
|
end
|
||||||
@ -122,20 +122,19 @@ describe Runner::Strategy::Poseidon do
|
|||||||
|
|
||||||
it 'makes the correct request to Poseidon' do
|
it 'makes the correct request to Poseidon' do
|
||||||
faraday_connection = instance_double 'Faraday::Connection'
|
faraday_connection = instance_double 'Faraday::Connection'
|
||||||
allow(Faraday).to receive(:new).and_return(faraday_connection)
|
allow(described_class).to receive(:http_connection).and_return(faraday_connection)
|
||||||
allow(faraday_connection).to receive(:put).and_return(Faraday::Response.new(status: 201))
|
allow(faraday_connection).to receive(:put).and_return(Faraday::Response.new(status: 201))
|
||||||
action.call
|
action.call
|
||||||
expect(faraday_connection).to have_received(:put) do |url, body, headers|
|
expect(faraday_connection).to have_received(:put) do |url, body|
|
||||||
expect(url).to match(%r{execution-environments/#{execution_environment.id}\z})
|
expect(url).to match(%r{execution-environments/#{execution_environment.id}\z})
|
||||||
expect(body).to eq(execution_environment.to_json)
|
expect(body).to eq(execution_environment.to_json)
|
||||||
expect(headers).to include({'Content-Type' => 'application/json'})
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
shared_examples 'returns true when the api request was successful' do |status|
|
shared_examples 'returns true when the api request was successful' do |status|
|
||||||
it "returns true on status #{status}" do
|
it "returns true on status #{status}" do
|
||||||
faraday_connection = instance_double 'Faraday::Connection'
|
faraday_connection = instance_double 'Faraday::Connection'
|
||||||
allow(Faraday).to receive(:new).and_return(faraday_connection)
|
allow(described_class).to receive(:http_connection).and_return(faraday_connection)
|
||||||
allow(faraday_connection).to receive(:put).and_return(Faraday::Response.new(status: status))
|
allow(faraday_connection).to receive(:put).and_return(Faraday::Response.new(status: status))
|
||||||
expect(action.call).to be_truthy
|
expect(action.call).to be_truthy
|
||||||
end
|
end
|
||||||
@ -144,7 +143,7 @@ describe Runner::Strategy::Poseidon do
|
|||||||
shared_examples 'returns false when the api request failed' do |status|
|
shared_examples 'returns false when the api request failed' do |status|
|
||||||
it "returns false on status #{status}" do
|
it "returns false on status #{status}" do
|
||||||
faraday_connection = instance_double 'Faraday::Connection'
|
faraday_connection = instance_double 'Faraday::Connection'
|
||||||
allow(Faraday).to receive(:new).and_return(faraday_connection)
|
allow(described_class).to receive(:http_connection).and_return(faraday_connection)
|
||||||
allow(faraday_connection).to receive(:put).and_return(Faraday::Response.new(status: status))
|
allow(faraday_connection).to receive(:put).and_return(Faraday::Response.new(status: status))
|
||||||
expect(action.call).to be_falsey
|
expect(action.call).to be_falsey
|
||||||
end
|
end
|
||||||
@ -160,7 +159,7 @@ describe Runner::Strategy::Poseidon do
|
|||||||
|
|
||||||
it 'returns false if Faraday raises an error' do
|
it 'returns false if Faraday raises an error' do
|
||||||
faraday_connection = instance_double 'Faraday::Connection'
|
faraday_connection = instance_double 'Faraday::Connection'
|
||||||
allow(Faraday).to receive(:new).and_return(faraday_connection)
|
allow(described_class).to receive(:http_connection).and_return(faraday_connection)
|
||||||
allow(faraday_connection).to receive(:put).and_raise(Faraday::TimeoutError)
|
allow(faraday_connection).to receive(:put).and_raise(Faraday::TimeoutError)
|
||||||
expect(action.call).to be_falsey
|
expect(action.call).to be_falsey
|
||||||
end
|
end
|
||||||
|
Reference in New Issue
Block a user