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



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
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/cuetip/models/job.rb', line 71

def execute(&block)
    Klogger.tagged(job_id: id, job_class: class_name) do
      begin
      Cuetip.logger.info "Executing"
      # 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?
        Cuetip.logger.warn 'Job has expired, removed from queue'
        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'
        Cuetip.logger.info 'Execution complete'
        remove_from_queue

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

        true
      rescue Exception, Timeout::TimeoutError => e
        Cuetip.logger.exception(e, "Job failed")

        # 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)
          Cuetip.logger.info "Requeueing job", 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
    end
  ensure
    unless destroyed?
      self.finished_at = Time.now
      save!
    end
    Cuetip.config.emit(:finished, self, job_klass)
    Cuetip.logger.debug 'Finished processing'
  end
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, **tags) ⇒ Object

Log some text about this job

Parameters:

  • text (String)


64
65
66
# File 'lib/cuetip/models/job.rb', line 64

def log(text, **tags)
  Cuetip.logger.info text, **tags
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
54
55
56
57
58
59
# File 'lib/cuetip/models/job.rb', line 49

def remove_from_queue
  queued_job&.destroy
  self.queued_job = nil
  Cuetip.logger.debug 'Removed from queue'

  if delete_after_execution?
    destroy
    Cuetip.logger.debug 'Removed job from database'
  end

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