Skip to content

Commit

Permalink
Merge pull request #14791 from yrudman/fixed-bug-execute-timeout-for-…
Browse files Browse the repository at this point in the history
…container-image-scanning

Fixed bug: timeout was not triggered for Image Scanning Job after removing Job#agent_class
  • Loading branch information
jrafanie authored Apr 20, 2017
2 parents 8ba23bf + abaf456 commit 532b886
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
4 changes: 3 additions & 1 deletion app/models/job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,9 @@ def self.check_jobs_for_timeout

# Allow jobs to run longer if the MiqQueue task is still active. (Limited to MiqServer for now.)
# TODO: can we add method_name, queue_name, role, instance_id to the exists?
next if MiqQueue.exists?(:state => %w(dequeue ready), :task_id => job.guid)
if job.miq_server_id
next if MiqQueue.exists?(:state => %w(dequeue ready), :task_id => job.guid, :class_name => "MiqServer")
end
job.timeout!
end
rescue Exception
Expand Down
55 changes: 55 additions & 0 deletions spec/models/job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,51 @@
end
end
end

describe "#timeout!" do
it "adds to MiqQueue signal 'signal_abort' for this job " do
@job.timeout!
expect_signal_abort_and_timeout_message
end
end

describe ".check_jobs_for_timeout" do
before(:each) do
@job.update_attributes(:state => "active")
@queue_item = MiqQueue.put(:task_id => @job.guid)
end

context "job timed out" do
it "calls 'job#timeout!' if server was not assigned to job" do
Timecop.travel 5.minutes
Job.check_jobs_for_timeout
expect_signal_abort_and_timeout_message
end

it "calls 'job#timeout!' if server was assigned to job but queue item not in 'ready' or 'dequeue' state" do
@queue_item.update_attributes(:state => MiqQueue::STATE_WARN, :class_name => "MiqServer")
@job.update_attributes(:miq_server_id => @server1.id)
Timecop.travel 5.minutes
Job.check_jobs_for_timeout
expect_signal_abort_and_timeout_message
end

it "does not call 'job#timeout!' if queue state is 'ready' and server was assigned to job" do
@queue_item.update_attributes(:state => MiqQueue::STATE_READY, :class_name => "MiqServer")
@job.update_attributes(:miq_server_id => @server1.id)
Timecop.travel 5.minutes
Job.check_jobs_for_timeout
expect_no_signal_abort
end
end

context "job not timed out" do
it "does not call 'job#timeout!'" do
Job.check_jobs_for_timeout
expect_no_signal_abort
end
end
end
end

context "before_destroy callback" do
Expand Down Expand Up @@ -372,6 +417,16 @@

private

def expect_signal_abort_and_timeout_message
queue_item = MiqQueue.find_by(:instance_id => @job.id, :class_name => "Job", :method_name => "signal_abort")
expect(queue_item.args[0].starts_with?("job timed out after")).to be true
end

def expect_no_signal_abort
queue_item = MiqQueue.find_by(:instance_id => @job.id, :class_name => "Job", :method_name => "signal_abort")
expect(queue_item).to be nil
end

def assert_queue_message
expect(MiqQueue.count).to eq(1)
q = MiqQueue.first
Expand Down

0 comments on commit 532b886

Please sign in to comment.