Class: Scheddy::Task
- Inherits:
-
Object
- Object
- Scheddy::Task
- Defined in:
- lib/scheddy/task.rb
Instance Attribute Summary collapse
-
#cron ⇒ Object
readonly
Returns the value of attribute cron.
-
#delay ⇒ Object
readonly
Returns the value of attribute delay.
-
#interval ⇒ Object
readonly
Returns the value of attribute interval.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
- #next_cycle ⇒ Object readonly
-
#tag ⇒ Object
readonly
Returns the value of attribute tag.
-
#task ⇒ Object
readonly
Returns the value of attribute task.
-
#track_runs ⇒ Object
readonly
Returns the value of attribute track_runs.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #finish_before(grace: 0.1.seconds) ⇒ Object
- #inspect ⇒ Object
- #kill ⇒ Object
- #perform(scheduler, now: false) ⇒ Object
- #reset ⇒ Object
- #running? ⇒ Boolean
- #to_h ⇒ Object
Instance Attribute Details
#cron ⇒ Object (readonly)
Returns the value of attribute cron.
3 4 5 |
# File 'lib/scheddy/task.rb', line 3 def cron @cron end |
#delay ⇒ Object (readonly)
Returns the value of attribute delay.
3 4 5 |
# File 'lib/scheddy/task.rb', line 3 def delay @delay end |
#interval ⇒ Object (readonly)
Returns the value of attribute interval.
3 4 5 |
# File 'lib/scheddy/task.rb', line 3 def interval @interval end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/scheddy/task.rb', line 3 def name @name end |
#next_cycle ⇒ Object
47 48 49 50 |
# File 'lib/scheddy/task.rb', line 47 def next_cycle initial_cycle! if @next_cycle == :initial @next_cycle end |
#tag ⇒ Object (readonly)
Returns the value of attribute tag.
3 4 5 |
# File 'lib/scheddy/task.rb', line 3 def tag @tag end |
#task ⇒ Object (readonly)
Returns the value of attribute task.
3 4 5 |
# File 'lib/scheddy/task.rb', line 3 def task @task end |
#track_runs ⇒ Object
Returns the value of attribute track_runs.
3 4 5 |
# File 'lib/scheddy/task.rb', line 3 def track_runs @track_runs end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
3 4 5 |
# File 'lib/scheddy/task.rb', line 3 def type @type end |
Instance Method Details
#finish_before(grace: 0.1.seconds) ⇒ Object
143 144 145 146 147 148 149 150 |
# File 'lib/scheddy/task.rb', line 143 def finish_before(grace: 0.1.seconds) case type when :interval Time.current + interval - grace when :cron cron.next_time.to_utc_time - grace end end |
#inspect ⇒ Object
77 78 79 80 |
# File 'lib/scheddy/task.rb', line 77 def inspect attrs = to_h.map{|k,v| "#{k}: #{v.inspect}"} %Q{#<#{self.class} #{attrs.join(', ')}>} end |
#kill ⇒ Object
39 40 41 |
# File 'lib/scheddy/task.rb', line 39 def kill thread&.kill end |
#perform(scheduler, now: false) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/scheddy/task.rb', line 8 def perform(scheduler, now: false) return next_cycle if Time.current < next_cycle && !now record_this_run if running? logger.error "Scheddy task '#{name}' already running; skipping this cycle" return next_cycle! end context = Context.new(scheduler, self) self.thread = Thread.new do logger.tagged tag do Rails.application.reloader.wrap do if context.finish_before < Time.current logger.info "Rails dev-mode reloader locked for entire task interval; skipping this run" else task.call(*[context].take(task.arity.abs)) end rescue Exception => e Scheddy.handle_error(e, self) end end ensure self.thread = nil end next_cycle! rescue Exception => e logger.error "Scheddy: error scheduling task '#{name}'; retrying in 5 seconds" Scheddy.handle_error(e, self) return self.next_cycle = 5.seconds.from_now end |
#reset ⇒ Object
52 53 54 55 |
# File 'lib/scheddy/task.rb', line 52 def reset self.next_cycle = :initial @task_history = nil end |
#running? ⇒ Boolean
43 44 45 |
# File 'lib/scheddy/task.rb', line 43 def running? !!thread end |
#to_h ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/scheddy/task.rb', line 58 def to_h attrs = { name: name, next_cycle: next_cycle.utc, type: type, tag: tag, task: task, track_runs: track_runs, } case type when :interval attrs[:initial_delay] = ActiveSupport::Duration.build(delay) unless track_runs && last_run attrs[:interval] = ActiveSupport::Duration.build(interval) when :cron attrs[:cron] = cron.original end attrs.to_a.sort_by!{_1.to_s}.to_h end |