Skip to content

Commit

Permalink
Merge pull request #14342 from yrudman/refactor-logging-for-job
Browse files Browse the repository at this point in the history
Refactor/DRY logging for job
  • Loading branch information
chessbyte authored Mar 15, 2017
2 parents b9c1057 + 70076e1 commit e02e529
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
10 changes: 7 additions & 3 deletions app/models/job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def self.create_job(process_type, options = {})
job.initialize_attributes
job.save
job.create_miq_task(job.attributes_for_task)
$log.info "Job created: guid: [#{job.guid}], userid: [#{job.userid}], name: [#{job.name}], target class: [#{job.target_class}], target id: [#{job.target_id}], process type: [#{job.type}], agent class: [#{job.agent_class}], agent id: [#{job.agent_id}], zone: [#{job.zone}]"
$log.info "Job created: #{job.attributes_log}"
job.signal(:initializing)
job
end
Expand All @@ -55,11 +55,11 @@ def initialize_attributes

def check_active_on_destroy
if self.is_active?
_log.warn "Job is active, delete not allowed - guid: [#{guid}], userid: [#{self.userid}], name: [#{self.name}], target class: [#{target_class}], target id: [#{target_id}], process type: [#{type}], agent class: [#{agent_class}], agent id: [#{agent_id}], zone: [#{zone}]"
_log.warn "Job is active, delete not allowed - #{attributes_log}"
throw :abort
end

_log.info "Job deleted: guid: [#{guid}], userid: [#{self.userid}], name: [#{self.name}], target class: [#{target_class}], target id: [#{target_id}], process type: [#{type}], agent class: [#{agent_class}], agent id: [#{agent_id}], zone: [#{zone}]"
_log.info "Job deleted: #{attributes_log}"
true
end

Expand Down Expand Up @@ -251,4 +251,8 @@ def attributes_for_task
:zone => zone,
:started_on => started_on}
end

def attributes_log
"guid: [#{guid}], userid: [#{self.userid}], name: [#{self.name}], target class: [#{target_class}], target id: [#{target_id}], process type: [#{type}], agent class: [#{agent_class}], agent id: [#{agent_id}], zone: [#{zone}]"
end
end # class Job
2 changes: 1 addition & 1 deletion app/models/job_proxy_dispatcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def queue_signal(job, options)

def start_job_on_proxy(job, proxy)
assign_proxy_to_job(proxy, job)
_log.info "Job [#{job.guid}] update: userid: [#{job.userid}], name: [#{job.name}], target class: [#{job.target_class}], target id: [#{job.target_id}], process type: [#{job.type}], agent class: [#{job.agent_class}], agent id: [#{job.agent_id}]"
_log.info "Job #{job.attributes_log}"
job_options = {:args => ["start"], :zone => MiqServer.my_zone}
job_options.merge!(:server_guid => proxy.guid, :role => "smartproxy") if proxy.kind_of?(MiqServer)
@active_vm_scans_by_zone[MiqServer.my_zone] += 1
Expand Down
8 changes: 8 additions & 0 deletions spec/models/job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,13 @@
end
end

describe "#attributes_log" do
it "returns attributes for logging" do
job = Job.create_job("VmScan", :name => "Hello, World!")
expect(job.attributes_log).to include("VmScan", "Hello, World!", job.guid)
end
end

context "belongs_to task" do
before(:each) do
@job = Job.create_job("VmScan", :name => "Hello, World!")
Expand Down Expand Up @@ -320,6 +327,7 @@
)
end
end

context "after_update_commit callback calls" do
describe "#update_linked_task" do
it "executes when 'after_update_commit' callbacke triggered" do
Expand Down

0 comments on commit e02e529

Please sign in to comment.