Class: Cuetip::Models::Job

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/cuetip/models/job.rb

Constant Summary collapse

STATUSES =
%w[Pending Running Complete Aborted Expired].freeze

Instance Method Summary collapse

Instance Method Details

#execute(&block) ⇒ Boolean

Execute the job

Returns:

  • (Boolean)

    whether the job executed successfully or not



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/cuetip/models/job.rb', line 65

def execute(&block)
  log "Beginning execution of job #{id} with #{class_name}"
  # Initialize a new instance of the job we wish to execute
  job_klass = class_name.constantize.new(self)

  # If the job has expired, we should not be executing this so we'll just
  # remove it from the queue and mark it as expired.
  if expired?
    log 'Job has expired'
    self.status = 'Expired'
    remove_from_queue
    Cuetip.config.emit(:expired, self, job_klass)
    return false
  end

  Cuetip.config.emit(:before_perform, self, job_klass)

  # If we have a block, call this so we can manipulate our actual job class
  # before execution if needed (mostly for testing)
  block.call(job_klass) if block_given?

  # Mark the job as runnign
  update!(status: 'Running', started_at: Time.now, executions: executions + 1)

  begin
    # Perform the job within a timeout
    Timeout.timeout(maximum_execution_time || 1.year) do
      job_klass.perform
    end
    # Mark the job as complete and remove it from the queue
    self.status = 'Complete'
    log 'Job completed successfully'
    remove_from_queue

    Cuetip.config.emit(:completed, self, job_klass)

    true
  rescue Exception, Timeout::TimeoutError => e
    log "Job failed with #{e.class} (#{e.message})"

    # If there's an error, mark the job as failed and copy exception
    # data into the job
    self.status = 'Failed'
    self.exception_class = e.class.name
    self.exception_message = e.message
    self.exception_backtrace = e.backtrace.join("\n")

    # Handle requeing the job if needed.
    if requeue_on_failure?
      # Requeue this job for execution again after the retry interval.
      new_job = queued_job.requeue(run_after: Time.now + retry_interval.to_i)
      log "Requeing job to run after #{new_job.run_after.to_s(:long)}"
      self.status = 'Pending'
    else
      # We're done with this job. We can't do any more retries.
      remove_from_queue
    end

    Cuetip.config.emit(:exception, e, self, job_klass)

    false
  end
ensure
  self.finished_at = Time.now
  save!
  Cuetip.config.emit(:finished, self, job_klass)
  log 'Finished processing'
end

#expired?Boolean

Has this job expired?

Returns:

  • (Boolean)


34
35
36
# File 'lib/cuetip/models/job.rb', line 34

def expired?
  ttl? ? expires_at <= Time.now : false
end

#expires_atObject

The time that this job expired



39
40
41
# File 'lib/cuetip/models/job.rb', line 39

def expires_at
  ttl? ? created_at + ttl : nil
end

#log(text) ⇒ Object

Log some text about this job

Parameters:

  • text (String)


58
59
60
# File 'lib/cuetip/models/job.rb', line 58

def log(text)
  Cuetip.logger.info "[#{id}] #{text}"
end

#queued?Boolean

Is this job in the queue

Returns:

  • (Boolean)


29
30
31
# File 'lib/cuetip/models/job.rb', line 29

def queued?
  queued_job.present?
end

#remove_from_queueObject

Remove this job from the queue



49
50
51
52
53
# File 'lib/cuetip/models/job.rb', line 49

def remove_from_queue
  queued_job&.destroy
  self.queued_job = nil
  log 'Removed from queue'
end

#requeue_on_failure?Boolean

Should this job be requeued on a failure right now?

Returns:

  • (Boolean)


44
45
46
# File 'lib/cuetip/models/job.rb', line 44

def requeue_on_failure?
  retry_count && retry_interval ? executions <= retry_count : false
end