Improved code layout.

This commit is contained in:
Martin Edenhofer 2016-01-05 09:48:26 +01:00
parent e461fa9ceb
commit d8507d0b57

View file

@ -26,14 +26,14 @@ class Scheduler < ApplicationModel
end end
# read/load jobs and check if it is alredy started # read/load jobs and check if it is alredy started
jobs = Scheduler.where( 'active = ?', true ).order('prio ASC') jobs = Scheduler.where('active = ?', true).order('prio ASC')
jobs.each {|job| jobs.each {|job|
# ignore job is still running # ignore job is still running
next if @@jobs_started[ job.id ] next if @@jobs_started[ job.id ]
# check job.last_run # check job.last_run
next if job.last_run && job.period && job.last_run > ( Time.zone.now - job.period ) next if job.last_run && job.period && job.last_run > (Time.zone.now - job.period)
# run job as own thread # run job as own thread
@@jobs_started[ job.id ] = true @@jobs_started[ job.id ] = true
@ -44,7 +44,7 @@ class Scheduler < ApplicationModel
end end
end end
def self.start_job( job ) def self.start_job(job)
Thread.new { Thread.new {
@ -81,7 +81,7 @@ class Scheduler < ApplicationModel
} }
end end
def self._start_job( job, try_count = 0, try_run_time = Time.zone.now ) def self._start_job(job, try_count = 0, try_run_time = Time.zone.now)
job.last_run = Time.zone.now job.last_run = Time.zone.now
job.pid = Thread.current.object_id job.pid = Thread.current.object_id
job.save job.save
@ -101,14 +101,14 @@ class Scheduler < ApplicationModel
try_count += 1 try_count += 1
# reset error counter if to old # reset error counter if to old
if try_run_time + ( 60 * 5 ) < Time.zone.now if try_run_time + (60 * 5) < Time.zone.now
try_count = 0 try_count = 0
end end
try_run_time = Time.zone.now try_run_time = Time.zone.now
# restart job again # restart job again
if try_run_max > try_count if try_run_max > try_count
_start_job( job, try_count, try_run_time) _start_job(job, try_count, try_run_time)
else else
raise "STOP thread for #{job.method} after #{try_count} tries" raise "STOP thread for #{job.method} after #{try_count} tries"
end end
@ -145,7 +145,7 @@ class Scheduler < ApplicationModel
end end
def self.check( name, time_warning = 10, time_critical = 20 ) def self.check(name, time_warning = 10, time_critical = 20)
time_warning_time = Time.zone.now - time_warning.minutes time_warning_time = Time.zone.now - time_warning.minutes
time_critical_time = Time.zone.now - time_critical.minutes time_critical_time = Time.zone.now - time_critical.minutes
scheduler = Scheduler.find_by( name: name ) scheduler = Scheduler.find_by( name: name )