trabajo-afectivo/app/models/scheduler.rb

89 lines
2.6 KiB
Ruby
Raw Normal View History

# Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/
2013-03-05 08:38:58 +00:00
class Scheduler < ApplicationModel
2013-05-07 20:45:00 +00:00
def self.run( worker, worker_count )
2013-03-05 08:38:58 +00:00
Thread.abort_on_exception = true
2013-05-07 20:45:00 +00:00
jobs_started = {}
2013-03-05 08:38:58 +00:00
while true
2013-05-07 20:45:00 +00:00
puts "Scheduler running (worker #{worker} of #{worker_count})..."
# read/load jobs and check if it is alredy started
jobs = Scheduler.where( 'active = ? AND prio = ?', true, worker )
jobs.each {|job|
next if jobs_started[ job.id ]
jobs_started[ job.id ] = true
self.start_job( job, worker, worker_count )
}
sleep 45
2013-03-05 08:38:58 +00:00
end
end
2013-05-07 20:45:00 +00:00
def self.start_job( job, worker, worker_count )
2013-03-05 08:38:58 +00:00
puts "started job thread for '#{job.name}' (#{job.method})..."
2013-05-07 20:45:00 +00:00
sleep 4
2013-03-05 08:38:58 +00:00
Thread.new {
if job.period
while true
2013-05-07 20:45:00 +00:00
self._start_job( job, worker, worker_count )
2013-03-05 08:38:58 +00:00
job = Scheduler.where( :id => job.id ).first
2013-05-07 20:45:00 +00:00
# exit is job got deleted
2013-03-05 08:38:58 +00:00
break if !job
2013-05-07 20:45:00 +00:00
# exit if job is not active anymore
2013-03-05 08:38:58 +00:00
break if !job.active
2013-05-07 20:45:00 +00:00
# exit if there is no loop period defined
2013-03-05 08:38:58 +00:00
break if !job.period
2013-05-07 20:45:00 +00:00
# wait until next run
2013-03-05 08:38:58 +00:00
sleep job.period
end
else
2013-05-07 20:45:00 +00:00
self._start_job( job, worker, worker_count )
2013-03-05 08:38:58 +00:00
end
# raise "Exception from thread"
2013-03-05 08:38:58 +00:00
job.pid = ''
job.save
puts " ...stopped thread for '#{job.method}'"
}
end
2013-05-07 20:45:00 +00:00
def self._start_job( job, worker, worker_count )
2013-05-07 21:34:58 +00:00
puts "execute #{job.method} (worker #{worker} of #{worker_count})..."
2013-03-05 08:38:58 +00:00
job.last_run = Time.now
job.pid = Thread.current.object_id
job.save
2013-05-07 20:45:00 +00:00
eval job.method()
2013-03-05 08:38:58 +00:00
end
2013-08-04 21:56:02 +00:00
def self.check( name, time_warning = 10, time_critical = 20 )
time_warning_time = Time.now - time_warning.minutes
time_critical_time = Time.now - time_critical.minutes
scheduler = Scheduler.where( :name => name ).first
if !scheduler
puts "CRITICAL - no such scheduler jobs '#{name}'"
return true
end
#puts "S " + scheduler.inspect
if !scheduler.last_run
puts "CRITICAL - scheduler jobs never started '#{name}'"
exit 2
end
if scheduler.last_run < time_critical_time
puts "CRITICAL - scheduler jobs was not running in last '#{time_critical.to_s}' minutes - last run at '#{scheduler.last_run.to_s}' '#{name}'"
exit 2
end
if scheduler.last_run < time_warning_time
puts "CRITICAL - scheduler jobs was not running in last '#{time_warning.to_s}' minutes - last run at '#{scheduler.last_run.to_s}' '#{name}'"
exit 2
end
puts "ok - scheduler jobs was running at '#{scheduler.last_run.to_s}' '#{name}'"
exit 0
end
end