Class: Crono::Job
Overview
Crono::Job represents a Crono job
Instance Attribute Summary collapse
-
#execution_interval ⇒ Object
Returns the value of attribute execution_interval.
-
#healthy ⇒ Object
Returns the value of attribute healthy.
-
#job_args ⇒ Object
Returns the value of attribute job_args.
-
#job_log ⇒ Object
Returns the value of attribute job_log.
-
#job_logger ⇒ Object
Returns the value of attribute job_logger.
-
#job_options ⇒ Object
Returns the value of attribute job_options.
-
#last_performed_at ⇒ Object
Returns the value of attribute last_performed_at.
-
#next_performed_at ⇒ Object
Returns the value of attribute next_performed_at.
-
#performer ⇒ Object
Returns the value of attribute performer.
-
#period ⇒ Object
Returns the value of attribute period.
Instance Method Summary collapse
- #description ⇒ Object
-
#initialize(performer, period, job_args, job_options = nil) ⇒ Job
constructor
A new instance of Job.
- #job_id ⇒ Object
- #load ⇒ Object
- #next ⇒ Object
- #perform ⇒ Object
- #save ⇒ Object
Methods included from Logging
Constructor Details
#initialize(performer, period, job_args, job_options = nil) ⇒ Job
Returns a new instance of Job.
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/crono/job.rb', line 12 def initialize(performer, period, job_args, = nil) self.execution_interval = 0.minutes self.performer, self.period = performer, period self.job_args = JSON.generate(job_args) self.job_log = StringIO.new self.job_logger = Logger.new(job_log) self. = || {} self.next_performed_at = period.next @semaphore = Mutex.new end |
Instance Attribute Details
#execution_interval ⇒ Object
Returns the value of attribute execution_interval.
9 10 11 |
# File 'lib/crono/job.rb', line 9 def execution_interval @execution_interval end |
#healthy ⇒ Object
Returns the value of attribute healthy.
9 10 11 |
# File 'lib/crono/job.rb', line 9 def healthy @healthy end |
#job_args ⇒ Object
Returns the value of attribute job_args.
9 10 11 |
# File 'lib/crono/job.rb', line 9 def job_args @job_args end |
#job_log ⇒ Object
Returns the value of attribute job_log.
9 10 11 |
# File 'lib/crono/job.rb', line 9 def job_log @job_log end |
#job_logger ⇒ Object
Returns the value of attribute job_logger.
9 10 11 |
# File 'lib/crono/job.rb', line 9 def job_logger @job_logger end |
#job_options ⇒ Object
Returns the value of attribute job_options.
9 10 11 |
# File 'lib/crono/job.rb', line 9 def @job_options end |
#last_performed_at ⇒ Object
Returns the value of attribute last_performed_at.
9 10 11 |
# File 'lib/crono/job.rb', line 9 def last_performed_at @last_performed_at end |
#next_performed_at ⇒ Object
Returns the value of attribute next_performed_at.
9 10 11 |
# File 'lib/crono/job.rb', line 9 def next_performed_at @next_performed_at end |
#performer ⇒ Object
Returns the value of attribute performer.
9 10 11 |
# File 'lib/crono/job.rb', line 9 def performer @performer end |
#period ⇒ Object
Returns the value of attribute period.
9 10 11 |
# File 'lib/crono/job.rb', line 9 def period @period end |
Instance Method Details
#description ⇒ Object
28 29 30 |
# File 'lib/crono/job.rb', line 28 def description "Perform #{performer} #{period.description}" end |
#job_id ⇒ Object
32 33 34 |
# File 'lib/crono/job.rb', line 32 def job_id description end |
#load ⇒ Object
54 55 56 57 |
# File 'lib/crono/job.rb', line 54 def load self.last_performed_at = model.last_performed_at self.next_performed_at = period.next(since: last_performed_at) end |
#next ⇒ Object
23 24 25 26 |
# File 'lib/crono/job.rb', line 23 def next return next_performed_at if next_performed_at.future? Time.now end |
#perform ⇒ Object
36 37 38 39 40 41 42 43 44 |
# File 'lib/crono/job.rb', line 36 def perform return Thread.new {} if perform_before_interval? log "Perform #{performer}" self.last_performed_at = Time.now self.next_performed_at = period.next(since: last_performed_at) Thread.new { perform_job } end |
#save ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/crono/job.rb', line 46 def save @semaphore.synchronize do update_model clear_job_log ActiveRecord::Base.clear_active_connections! end end |