Class: Continuity::Scheduler
- Inherits:
-
Object
- Object
- Continuity::Scheduler
- Defined in:
- lib/continuity/scheduler.rb
Class Method Summary collapse
Instance Method Summary collapse
- #cron(cron_line, &blk) ⇒ Object
- #do_jobs(time_range) ⇒ Object
- #every(period, &blk) ⇒ Object
-
#initialize(backend, frequency = 10) ⇒ Scheduler
constructor
A new instance of Scheduler.
- #join ⇒ Object
- #maybe_schedule(now = Time.now.to_i) ⇒ Object
- #on_schedule(&block) ⇒ Object
- #run(check_frequency = 5) ⇒ Object
Constructor Details
#initialize(backend, frequency = 10) ⇒ Scheduler
Returns a new instance of Scheduler.
7 8 9 10 11 12 13 14 |
# File 'lib/continuity/scheduler.rb', line 7 def initialize(backend, frequency = 10) @frequency = frequency @backend = backend @next_schedule = 0 @on_schedule_cbs = [] @jobs = {} end |
Class Method Details
.new_using_redis(redis, frequency = 10) ⇒ Object
3 4 5 |
# File 'lib/continuity/scheduler.rb', line 3 def self.new_using_redis(redis, frequency = 10) new(RedisBackend.new(redis, frequency)) end |
Instance Method Details
#cron(cron_line, &blk) ⇒ Object
20 21 22 |
# File 'lib/continuity/scheduler.rb', line 20 def cron(cron_line, &blk) @jobs[CronEntry.new(cron_line)] = blk end |
#do_jobs(time_range) ⇒ Object
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/continuity/scheduler.rb', line 62 def do_jobs(time_range) time_range.each do |t| time = Time.at(t) @jobs.each do |cron_entry, blk| if cron_entry.at?(time) blk[time] end end end end |
#every(period, &blk) ⇒ Object
16 17 18 |
# File 'lib/continuity/scheduler.rb', line 16 def every(period, &blk) @jobs[PeriodicEntry.new(period)] = blk end |
#join ⇒ Object
42 43 44 |
# File 'lib/continuity/scheduler.rb', line 42 def join @scheduling_thread.join end |
#maybe_schedule(now = Time.now.to_i) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/continuity/scheduler.rb', line 46 def maybe_schedule(now = Time.now.to_i) return false unless @next_schedule <= now range_scheduled = false scheduled_up_to = @backend.lock_for_scheduling(now) do |previous_time| range_scheduled = (previous_time+1)..now do_jobs(range_scheduled) trigger_cbs(range_scheduled) yield range_scheduled if block_given? end @next_schedule = scheduled_up_to + @frequency return range_scheduled end |
#on_schedule(&block) ⇒ Object
24 25 26 |
# File 'lib/continuity/scheduler.rb', line 24 def on_schedule(&block) @on_schedule_cbs << block end |
#run(check_frequency = 5) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/continuity/scheduler.rb', line 28 def run(check_frequency = 5) @scheduling_thread = Thread.new { loop do begin maybe_schedule sleep check_frequency rescue Object $stderr.print "--Error in Continuity Scheduler--\n" $stderr.print $!.backtrace.join("\n") end end } end |