Class: Zhong::Job
Instance Attribute Summary collapse
-
#at ⇒ Object
readonly
Returns the value of attribute at.
-
#category ⇒ Object
readonly
Returns the value of attribute category.
-
#every ⇒ Object
readonly
Returns the value of attribute every.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#last_ran ⇒ Object
readonly
Returns the value of attribute last_ran.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #clear ⇒ Object
- #desired_at_key ⇒ Object
- #disable ⇒ Object
- #disabled? ⇒ Boolean
- #disabled_key ⇒ Object
- #enable ⇒ Object
-
#initialize(job_name, config = {}, callbacks = {}, &block) ⇒ Job
constructor
A new instance of Job.
- #last_ran_key ⇒ Object
- #lock_key ⇒ Object
- #next_at ⇒ Object
- #refresh_last_ran ⇒ Object
- #run(time = Time.now, error_handler = nil) ⇒ Object
- #run?(time = Time.now) ⇒ Boolean
- #running? ⇒ Boolean
- #to_s ⇒ Object
Constructor Details
#initialize(job_name, config = {}, callbacks = {}, &block) ⇒ Job
Returns a new instance of Job.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/zhong/job.rb', line 8 def initialize(job_name, config = {}, callbacks = {}, &block) @name = job_name @category = config[:category] @logger = config[:logger] @config = config @callbacks = callbacks @at = config[:at] ? At.parse(config[:at], grace: config.fetch(:grace, 15.minutes)) : nil @every = config[:every] ? Every.parse(config[:every]) : nil raise "must specific either `at` or `every` for job: #{self}" unless @at || @every @block = block @if = config[:if] @long_running_timeout = config[:long_running_timeout] @running = false @first_run = true @id = Digest::SHA256.hexdigest(@name) end |
Instance Attribute Details
#at ⇒ Object (readonly)
Returns the value of attribute at.
6 7 8 |
# File 'lib/zhong/job.rb', line 6 def at @at end |
#category ⇒ Object (readonly)
Returns the value of attribute category.
6 7 8 |
# File 'lib/zhong/job.rb', line 6 def category @category end |
#every ⇒ Object (readonly)
Returns the value of attribute every.
6 7 8 |
# File 'lib/zhong/job.rb', line 6 def every @every end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
6 7 8 |
# File 'lib/zhong/job.rb', line 6 def id @id end |
#last_ran ⇒ Object (readonly)
Returns the value of attribute last_ran.
6 7 8 |
# File 'lib/zhong/job.rb', line 6 def last_ran @last_ran end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/zhong/job.rb', line 6 def name @name end |
Instance Method Details
#clear ⇒ Object
123 124 125 |
# File 'lib/zhong/job.rb', line 123 def clear redis.del(last_ran_key) end |
#desired_at_key ⇒ Object
131 132 133 |
# File 'lib/zhong/job.rb', line 131 def desired_at_key "zhong:at:#{self}" end |
#disable ⇒ Object
97 98 99 100 101 |
# File 'lib/zhong/job.rb', line 97 def disable fire_callbacks(:before_disable, self) redis.set(disabled_key, "true") fire_callbacks(:after_disable, self) end |
#disabled? ⇒ Boolean
109 110 111 |
# File 'lib/zhong/job.rb', line 109 def disabled? !redis.get(disabled_key).nil? end |
#disabled_key ⇒ Object
135 136 137 |
# File 'lib/zhong/job.rb', line 135 def disabled_key "zhong:disabled:#{self}" end |
#enable ⇒ Object
103 104 105 106 107 |
# File 'lib/zhong/job.rb', line 103 def enable fire_callbacks(:before_enable, self) redis.del(disabled_key) fire_callbacks(:after_enable, self) end |
#last_ran_key ⇒ Object
127 128 129 |
# File 'lib/zhong/job.rb', line 127 def last_ran_key "zhong:last_ran:#{self}" end |
#lock_key ⇒ Object
139 140 141 |
# File 'lib/zhong/job.rb', line 139 def lock_key "zhong:lock:#{self}" end |
#next_at ⇒ Object
117 118 119 120 121 |
# File 'lib/zhong/job.rb', line 117 def next_at every_time = @every.next_at(@last_ran) if @last_ran && @every at_time = @at.next_at(Time.now) if @at [every_time, at_time, Time.now].compact.max || "now" end |
#refresh_last_ran ⇒ Object
92 93 94 95 |
# File 'lib/zhong/job.rb', line 92 def refresh_last_ran last_ran_val = redis.get(last_ran_key) @last_ran = last_ran_val ? Time.at(last_ran_val.to_i) : nil end |
#run(time = Time.now, error_handler = nil) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/zhong/job.rb', line 39 def run(time = Time.now, error_handler = nil) return unless run?(time) locked = false errored = false ran = false begin redis_lock.lock do locked = true @running = true refresh_last_ran # we need to check again, as another process might have acquired # the lock right before us and obviated our need to do anything break unless run?(time) if disabled? logger.info "not running, disabled: #{self}" break end logger.info "running: #{self}" if @block begin @block.call ran = true rescue => boom logger.error "#{self} failed: #{boom}" error_handler.call(boom, self) if error_handler end end ran!(time) end rescue Suo::LockClientError => boom logger.error "unable to run due to client error: #{boom}" errored = true end @running = false logger.info "unable to acquire exclusive run lock: #{self}" if !locked && !errored ran end |
#run?(time = Time.now) ⇒ Boolean
29 30 31 32 33 34 35 36 37 |
# File 'lib/zhong/job.rb', line 29 def run?(time = Time.now) if @first_run clear_last_ran_if_at_changed if @at refresh_last_ran @first_run = false end run_every?(time) && run_at?(time) && run_if?(time) end |
#running? ⇒ Boolean
88 89 90 |
# File 'lib/zhong/job.rb', line 88 def running? @running end |
#to_s ⇒ Object
113 114 115 |
# File 'lib/zhong/job.rb', line 113 def to_s @to_s ||= [@category, @name].compact.join(".").freeze end |