Skip to content

Commit

Permalink
Merge pull request #10396 from moolitayer/fix_scan_timeout
Browse files Browse the repository at this point in the history
Fix container scanning timeout handling.
(cherry picked from commit 1a367ea)
  • Loading branch information
roliveri authored and chessbyte committed Aug 23, 2016
1 parent 7e2084a commit 6235a83
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 9 deletions.
14 changes: 9 additions & 5 deletions app/models/job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,18 @@ def timeout!
end
end

def target_entity
target_class.constantize.find_by_id(target_id)
end

def self.check_jobs_for_timeout
$log.debug "Checking for timed out jobs"
begin
in_my_region
.where("state != 'finished' and (state != 'waiting_to_start' or dispatch_status = 'active')")
.where("zone is null or zone = ?", MiqServer.my_zone)
.each do |job|
next unless job.updated_on < job.current_job_timeout(timeout_adjustment(job)).seconds.ago
next unless job.updated_on < job.current_job_timeout(job.timeout_adjustment).seconds.ago

# Allow jobs to run longer if the MiqQueue task is still active. (Limited to MiqServer for now.)
if job.agent_class == "MiqServer"
Expand All @@ -185,11 +189,11 @@ def self.check_jobs_for_timeout
end
end

def self.timeout_adjustment(job)
def timeout_adjustment
timeout_adjustment = 1
vm = VmOrTemplate.find(job.target_id)
if vm.kind_of?(ManageIQ::Providers::Microsoft::InfraManager::Vm) ||
vm.kind_of?(ManageIQ::Providers::Microsoft::InfraManager::Template)
target = target_entity
if target.kind_of?(ManageIQ::Providers::Microsoft::InfraManager::Vm) ||
target.kind_of?(ManageIQ::Providers::Microsoft::InfraManager::Template)
timeout_adjustment = 4
end
timeout_adjustment
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,6 @@ def queue_callback(state, msg, _)

private

def target_entity
target_class.constantize.find_by_id(target_id)
end

def ext_management_system
@ext_management_system ||= ExtManagementSystem.find(options[:ems_id])
end
Expand Down
57 changes: 57 additions & 0 deletions spec/models/job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,35 @@
end
end

context "where job is for a VM that disappeared" do
before(:each) do
@job.update_attributes(:state => "scanning", :dispatch_status => "active", :zone => nil)

@vm.destroy

Timecop.travel 5.minutes

Job.check_jobs_for_timeout

@msg = MiqQueue.get(:role => "smartstate", :zone => @zone.name)
status, message, result = @msg.deliver
@msg.delivered(status, message, result)

@job.reload
end

after(:each) do
Timecop.return
end

it "should be timed out after 5 minutes" do
$log.info("@job: #{@job.inspect}")
expect(@job.state).to eq("finished")
expect(@job.status).to eq("error")
expect(@job.message.starts_with?("job timed out after")).to be_truthy
end
end

context "where 2 VMs in 2 Zones have an EVM Snapshot" do
before(:each) do
scan_type = nil
Expand Down Expand Up @@ -220,6 +249,34 @@
# end
end
end

context "where scan jobs exist for both vms and container images" do
before(:each) do
@ems_k8s = FactoryGirl.create(
:ems_kubernetes, :hostname => "test.com", :zone => @zone, :port => 8443,
:authentications => [AuthToken.new(:name => "test", :type => 'AuthToken', :auth_key => "a secret")]
)
@image = FactoryGirl.create(
:container_image, :ext_management_system => @ems_k8s, :name => 'test',
:image_ref => "docker://3629a651e6c11d7435937bdf41da11cf87863c03f2587fa788cf5cbfe8a11b9a"
)
@image_scan_job = @image.scan
end

context "#target_entity" do
it "returns the job target" do
expect(@job.target_entity).to eq(@vm)
expect(@image_scan_job.target_entity).to eq(@image)
end
end

context "#timeout_adjustment" do
it "returns the correct adjusment" do
expect(@job.timeout_adjustment).to eq(1)
expect(@image_scan_job.timeout_adjustment).to eq(1)
end
end
end
end

private
Expand Down

0 comments on commit 6235a83

Please sign in to comment.