Class: Philiprehberger::Scheduler::Job
- Inherits:
-
Object
- Object
- Philiprehberger::Scheduler::Job
- Defined in:
- lib/philiprehberger/scheduler/job.rb
Instance Attribute Summary collapse
-
#callable ⇒ Object
readonly
Returns the value of attribute callable.
-
#condition ⇒ Object
readonly
Returns the value of attribute condition.
-
#cron ⇒ Object
readonly
Returns the value of attribute cron.
-
#depends_on ⇒ Object
readonly
Returns the value of attribute depends_on.
-
#input_from ⇒ Object
readonly
Returns the value of attribute input_from.
-
#interval ⇒ Object
readonly
Returns the value of attribute interval.
-
#last_error ⇒ Object
Returns the value of attribute last_error.
-
#last_result ⇒ Object
Returns the value of attribute last_result.
-
#last_run ⇒ Object
Returns the value of attribute last_run.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#paused ⇒ Object
Returns the value of attribute paused.
-
#run_at ⇒ Object
readonly
Returns the value of attribute run_at.
-
#running ⇒ Object
Returns the value of attribute running.
-
#timezone ⇒ Object
readonly
Returns the value of attribute timezone.
Instance Method Summary collapse
- #cron? ⇒ Boolean
- #due?(now) ⇒ Boolean
- #execute(input = nil) ⇒ Object
-
#initialize(callable:, interval: nil, cron: nil, run_at: nil, **options) ⇒ Job
constructor
A new instance of Job.
- #interval? ⇒ Boolean
- #overlap? ⇒ Boolean
- #paused? ⇒ Boolean
- #restore_state(state) ⇒ Object
- #run_at? ⇒ Boolean
- #to_state ⇒ Object
Constructor Details
#initialize(callable:, interval: nil, cron: nil, run_at: nil, **options) ⇒ Job
Returns a new instance of Job.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/philiprehberger/scheduler/job.rb', line 10 def initialize(callable:, interval: nil, cron: nil, run_at: nil, **) @callable = callable @interval = interval @cron = cron @run_at = run_at @name = .delete(:name) @depends_on = .delete(:depends_on) @input_from = .delete(:input_from) @condition = .delete(:if) @timezone = .delete(:timezone) @options = { overlap: true }.merge() @last_run = nil @running = false @last_result = nil @last_error = nil @paused = false end |
Instance Attribute Details
#callable ⇒ Object (readonly)
Returns the value of attribute callable.
6 7 8 |
# File 'lib/philiprehberger/scheduler/job.rb', line 6 def callable @callable end |
#condition ⇒ Object (readonly)
Returns the value of attribute condition.
6 7 8 |
# File 'lib/philiprehberger/scheduler/job.rb', line 6 def condition @condition end |
#cron ⇒ Object (readonly)
Returns the value of attribute cron.
6 7 8 |
# File 'lib/philiprehberger/scheduler/job.rb', line 6 def cron @cron end |
#depends_on ⇒ Object (readonly)
Returns the value of attribute depends_on.
6 7 8 |
# File 'lib/philiprehberger/scheduler/job.rb', line 6 def depends_on @depends_on end |
#input_from ⇒ Object (readonly)
Returns the value of attribute input_from.
6 7 8 |
# File 'lib/philiprehberger/scheduler/job.rb', line 6 def input_from @input_from end |
#interval ⇒ Object (readonly)
Returns the value of attribute interval.
6 7 8 |
# File 'lib/philiprehberger/scheduler/job.rb', line 6 def interval @interval end |
#last_error ⇒ Object
Returns the value of attribute last_error.
8 9 10 |
# File 'lib/philiprehberger/scheduler/job.rb', line 8 def last_error @last_error end |
#last_result ⇒ Object
Returns the value of attribute last_result.
8 9 10 |
# File 'lib/philiprehberger/scheduler/job.rb', line 8 def last_result @last_result end |
#last_run ⇒ Object
Returns the value of attribute last_run.
8 9 10 |
# File 'lib/philiprehberger/scheduler/job.rb', line 8 def last_run @last_run end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/philiprehberger/scheduler/job.rb', line 6 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
6 7 8 |
# File 'lib/philiprehberger/scheduler/job.rb', line 6 def @options end |
#paused ⇒ Object
Returns the value of attribute paused.
8 9 10 |
# File 'lib/philiprehberger/scheduler/job.rb', line 8 def paused @paused end |
#run_at ⇒ Object (readonly)
Returns the value of attribute run_at.
6 7 8 |
# File 'lib/philiprehberger/scheduler/job.rb', line 6 def run_at @run_at end |
#running ⇒ Object
Returns the value of attribute running.
8 9 10 |
# File 'lib/philiprehberger/scheduler/job.rb', line 8 def running @running end |
#timezone ⇒ Object (readonly)
Returns the value of attribute timezone.
6 7 8 |
# File 'lib/philiprehberger/scheduler/job.rb', line 6 def timezone @timezone end |
Instance Method Details
#cron? ⇒ Boolean
40 41 42 |
# File 'lib/philiprehberger/scheduler/job.rb', line 40 def cron? !@cron.nil? end |
#due?(now) ⇒ Boolean
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/philiprehberger/scheduler/job.rb', line 48 def due?(now) return false if paused? return false if @condition && !@condition.call effective_now = apply_timezone(now) return due_by_run_at?(now) if run_at? return due_by_interval?(now) if interval? return due_by_cron?(effective_now) if cron? false end |
#execute(input = nil) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/philiprehberger/scheduler/job.rb', line 60 def execute(input = nil) @running = true @last_error = nil @last_result = if @input_from && !input.nil? @callable.call(input) else @callable.call end @last_result rescue StandardError => e @last_error = e raise ensure @running = false @last_run = Time.now end |
#interval? ⇒ Boolean
36 37 38 |
# File 'lib/philiprehberger/scheduler/job.rb', line 36 def interval? !@interval.nil? end |
#overlap? ⇒ Boolean
28 29 30 |
# File 'lib/philiprehberger/scheduler/job.rb', line 28 def overlap? @options[:overlap] end |
#paused? ⇒ Boolean
32 33 34 |
# File 'lib/philiprehberger/scheduler/job.rb', line 32 def paused? @paused == true end |
#restore_state(state) ⇒ Object
90 91 92 93 |
# File 'lib/philiprehberger/scheduler/job.rb', line 90 def restore_state(state) @last_run = Time.parse(state['last_run']) if state['last_run'] @paused = state['paused'] == true if state.key?('paused') end |
#run_at? ⇒ Boolean
44 45 46 |
# File 'lib/philiprehberger/scheduler/job.rb', line 44 def run_at? !@run_at.nil? end |
#to_state ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/philiprehberger/scheduler/job.rb', line 77 def to_state { 'name' => @name, 'interval' => @interval, 'cron_expression' => @cron&.expression, 'overlap' => overlap?, 'last_run' => @last_run&.iso8601, 'timezone' => @timezone, 'last_error' => @last_error&., 'paused' => paused? } end |