Class: Zhong::Scheduler
- Inherits:
-
Object
- Object
- Zhong::Scheduler
- Defined in:
- lib/zhong/scheduler.rb
Constant Summary collapse
- DEFAULT_CONFIG =
{ timeout: 0.5, grace: 15.minutes, long_running_timeout: 5.minutes, tz: nil }.freeze
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#jobs ⇒ Object
readonly
Returns the value of attribute jobs.
-
#redis ⇒ Object
readonly
Returns the value of attribute redis.
Instance Method Summary collapse
- #category(name) {|_self| ... } ⇒ Object
- #clear ⇒ Object
- #error_handler(&block) ⇒ Object
- #every(period, name, opts = {}, &block) ⇒ Object
- #find_by_name(job_name) ⇒ Object
-
#initialize(config = {}) ⇒ Scheduler
constructor
A new instance of Scheduler.
- #on(event, &block) ⇒ Object
- #redis_time ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(config = {}) ⇒ Scheduler
Returns a new instance of Scheduler.
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/zhong/scheduler.rb', line 12 def initialize(config = {}) @jobs = {} @callbacks = {} @config = DEFAULT_CONFIG.merge(config) @logger = @config[:logger] @redis = @config[:redis] @tz = @config[:tz] @category = nil @error_handler = nil @running = false end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
3 4 5 |
# File 'lib/zhong/scheduler.rb', line 3 def config @config end |
#jobs ⇒ Object (readonly)
Returns the value of attribute jobs.
3 4 5 |
# File 'lib/zhong/scheduler.rb', line 3 def jobs @jobs end |
#redis ⇒ Object (readonly)
Returns the value of attribute redis.
3 4 5 |
# File 'lib/zhong/scheduler.rb', line 3 def redis @redis end |
Instance Method Details
#category(name) {|_self| ... } ⇒ Object
33 34 35 36 37 38 39 40 41 |
# File 'lib/zhong/scheduler.rb', line 33 def category(name) raise "cannot nest categories: #{name} would be nested in #{@category} (#{caller.first})" if @category @category = name.to_s yield(self) @category = nil end |
#clear ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/zhong/scheduler.rb', line 25 def clear raise "unable to clear while running; run Zhong.stop first" if @running @jobs = {} @callbacks = {} @category = nil end |
#error_handler(&block) ⇒ Object
56 57 58 59 |
# File 'lib/zhong/scheduler.rb', line 56 def error_handler(&block) @error_handler = block if block_given? @error_handler end |
#every(period, name, opts = {}, &block) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/zhong/scheduler.rb', line 43 def every(period, name, opts = {}, &block) raise "must specify a period for #{name} (#{caller.first})" unless period job = Job.new(name, opts.merge(@config).merge(every: period, category: @category), &block) if jobs.key?(job.id) @logger.error "duplicate job #{job}, skipping" return end @jobs[job.id] = job end |
#find_by_name(job_name) ⇒ Object
109 110 111 |
# File 'lib/zhong/scheduler.rb', line 109 def find_by_name(job_name) @jobs[Digest::SHA256.hexdigest(job_name)] end |
#on(event, &block) ⇒ Object
61 62 63 64 |
# File 'lib/zhong/scheduler.rb', line 61 def on(event, &block) raise "unknown callback #{event}" unless [:before_tick, :after_tick, :before_run, :after_run].include?(event.to_sym) (@callbacks[event.to_sym] ||= []) << block end |
#redis_time ⇒ Object
113 114 115 116 117 |
# File 'lib/zhong/scheduler.rb', line 113 def redis_time s, ms = @redis.time # returns [seconds since epoch, microseconds] now = Time.at(s + ms / (10**6)) @tz ? now.in_time_zone(@tz) : now end |
#start ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/zhong/scheduler.rb', line 66 def start @logger.info "starting at #{redis_time}" @stop = false trap_signals raise "already running" if @running loop do @running = true if fire_callbacks(:before_tick) now = redis_time jobs_to_run(now).each do |_, job| break if @stop run_job(job, now) end break if @stop fire_callbacks(:after_tick) heartbeat(now) break if @stop sleep_until_next_second end break if @stop end @running = false Thread.new { @logger.info "stopped" }.join end |
#stop ⇒ Object
104 105 106 107 |
# File 'lib/zhong/scheduler.rb', line 104 def stop Thread.new { @logger.error "stopping" } if @running # thread necessary due to trap context @stop = true end |