Class: Crono::Job

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/crono/job.rb

Overview

Crono::Job represents a Crono job

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#logfile=, #logger

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, job_options = 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.job_options = job_options || {}
  self.next_performed_at = period.next
  @semaphore = Mutex.new
end

Instance Attribute Details

#execution_intervalObject

Returns the value of attribute execution_interval.



9
10
11
# File 'lib/crono/job.rb', line 9

def execution_interval
  @execution_interval
end

#healthyObject

Returns the value of attribute healthy.



9
10
11
# File 'lib/crono/job.rb', line 9

def healthy
  @healthy
end

#job_argsObject

Returns the value of attribute job_args.



9
10
11
# File 'lib/crono/job.rb', line 9

def job_args
  @job_args
end

#job_logObject

Returns the value of attribute job_log.



9
10
11
# File 'lib/crono/job.rb', line 9

def job_log
  @job_log
end

#job_loggerObject

Returns the value of attribute job_logger.



9
10
11
# File 'lib/crono/job.rb', line 9

def job_logger
  @job_logger
end

#job_optionsObject

Returns the value of attribute job_options.



9
10
11
# File 'lib/crono/job.rb', line 9

def job_options
  @job_options
end

#last_performed_atObject

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_atObject

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

#performerObject

Returns the value of attribute performer.



9
10
11
# File 'lib/crono/job.rb', line 9

def performer
  @performer
end

#periodObject

Returns the value of attribute period.



9
10
11
# File 'lib/crono/job.rb', line 9

def period
  @period
end

Instance Method Details

#descriptionObject



28
29
30
# File 'lib/crono/job.rb', line 28

def description
  "Perform #{performer} #{period.description}"
end

#job_idObject



32
33
34
# File 'lib/crono/job.rb', line 32

def job_id
  description
end

#loadObject



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

#nextObject



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

#performObject



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

#saveObject



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