Added generic scheduler.
This commit is contained in:
parent
e96bfdd803
commit
77a4167b72
2 changed files with 95 additions and 0 deletions
49
app/models/scheduler.rb
Normal file
49
app/models/scheduler.rb
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
class Scheduler < ApplicationModel
|
||||||
|
|
||||||
|
def self.run
|
||||||
|
|
||||||
|
Thread.abort_on_exception = true
|
||||||
|
|
||||||
|
# read/load jobs
|
||||||
|
jobs = Scheduler.where( :active => true )
|
||||||
|
jobs.each {|job|
|
||||||
|
self.start_job( job )
|
||||||
|
}
|
||||||
|
while true
|
||||||
|
puts 'Scheduler running...'
|
||||||
|
sleep 60
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.start_job(job)
|
||||||
|
puts "started job thread for '#{job.name}' (#{job.method})..."
|
||||||
|
sleep 1.5
|
||||||
|
|
||||||
|
Thread.new {
|
||||||
|
if job.period
|
||||||
|
while true
|
||||||
|
self._start_job(job)
|
||||||
|
job = Scheduler.where( :id => job.id ).first
|
||||||
|
break if !job
|
||||||
|
break if !job.active
|
||||||
|
break if !job.period
|
||||||
|
sleep job.period
|
||||||
|
end
|
||||||
|
else
|
||||||
|
self._start_job(job)
|
||||||
|
end
|
||||||
|
# raise "Exception from thread"
|
||||||
|
job.pid = ''
|
||||||
|
job.save
|
||||||
|
puts " ...stopped thread for '#{job.method}'"
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
def self._start_job(job)
|
||||||
|
puts "execute #{job.method}..."
|
||||||
|
job.last_run = Time.now
|
||||||
|
job.pid = Thread.current.object_id
|
||||||
|
job.save
|
||||||
|
eval job.method
|
||||||
|
end
|
||||||
|
end
|
46
db/migrate/20130305065226_scheduler_create.rb
Normal file
46
db/migrate/20130305065226_scheduler_create.rb
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
class SchedulerCreate < ActiveRecord::Migration
|
||||||
|
def up
|
||||||
|
create_table :schedulers do |t|
|
||||||
|
t.column :name, :string, :limit => 250, :null => false
|
||||||
|
t.column :method, :string, :limit => 250, :null => false
|
||||||
|
t.column :period, :integer, :null => true
|
||||||
|
t.column :running, :integer, :null => false, :default => false
|
||||||
|
t.column :last_run, :timestamp, :null => true
|
||||||
|
t.column :pid, :string, :limit => 250, :null => true
|
||||||
|
t.column :note, :string, :limit => 250, :null => true
|
||||||
|
t.column :active, :boolean, :null => false, :default => false
|
||||||
|
t.column :updated_by_id, :integer, :null => false
|
||||||
|
t.column :created_by_id, :integer, :null => false
|
||||||
|
t.timestamps
|
||||||
|
end
|
||||||
|
add_index :schedulers, [:name], :unique => true
|
||||||
|
Scheduler.create(
|
||||||
|
:name => 'Check Channels',
|
||||||
|
:method => 'Channel.fetch',
|
||||||
|
:period => 30,
|
||||||
|
:active => true,
|
||||||
|
:updated_by_id => 1,
|
||||||
|
:created_by_id => 1,
|
||||||
|
)
|
||||||
|
Scheduler.create(
|
||||||
|
:name => 'Import OTRS diff load',
|
||||||
|
:method => 'Import::OTRS.diff_loop',
|
||||||
|
:period => 60 * 10,
|
||||||
|
:active => true,
|
||||||
|
:updated_by_id => 1,
|
||||||
|
:created_by_id => 1,
|
||||||
|
)
|
||||||
|
Scheduler.create(
|
||||||
|
:name => 'Generate Session data',
|
||||||
|
:method => 'Session.jobs',
|
||||||
|
:period => 60,
|
||||||
|
:active => true,
|
||||||
|
:updated_by_id => 1,
|
||||||
|
:created_by_id => 1,
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
def down
|
||||||
|
drop_table :schedulers
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue