class BackRun::Job

Constants

MAX_REMAINING_SECONDS

This is a Google Pubsub restriction. The ack deadline of a message can not be modified more than 600 seconds

MINUTES_PER_RETRY

Attributes

duration_seconds[R]
klass[R]
queue_name[R]

Public Class Methods

from_json(json) click to toggle source
# File lib/back_run/job.rb, line 24
def self.from_json(json)
  attributes = JSON.parse(json)
  new(
    attributes['class'], attributes['args'], attributes['queue_name'],
    attributes['scheduled_at'], attributes['retries']
  )
end
new(klass, json_args, queue_name, scheduled_at, retries) click to toggle source
# File lib/back_run/job.rb, line 12
def initialize(klass, json_args, queue_name, scheduled_at, retries)
  @klass = klass
  @args = json_args
  @queue_name = queue_name
  @scheduled_at = scheduled_at
  @retries = retries
end
new_from_active_job(job) click to toggle source
# File lib/back_run/job.rb, line 20
def self.new_from_active_job(job)
  new(job.class.to_s, job.arguments.to_json, job.queue_name, job.scheduled_at, 0)
end

Public Instance Methods

perform(worker) click to toggle source
# File lib/back_run/job.rb, line 39
def perform(worker)
  register_duration { @klass.constantize.new.perform(*JSON.parse(@args)) }
  BackRun.logger.info("Finished processing the job: #{@klass}")
rescue StandardError => e
  BackRun.logger.error("Failed processing the job: #{e.message}")
  handle_job_failure(worker)
end
remaining_seconds_to_run() click to toggle source
# File lib/back_run/job.rb, line 51
def remaining_seconds_to_run
  [((Time.at(@scheduled_at) - Time.now) / 1.second).round, MAX_REMAINING_SECONDS].min
end
should_run_now?() click to toggle source
# File lib/back_run/job.rb, line 47
def should_run_now?
  @scheduled_at.nil? || @scheduled_at <= Time.now.to_f
end
to_json(*_args) click to toggle source
# File lib/back_run/job.rb, line 32
def to_json(*_args)
  {
    class: @klass, args: @args, queue_name: @queue_name, scheduled_at: @scheduled_at.to_f,
    retries: @retries
  }.to_json
end

Private Instance Methods

handle_job_failure(worker) click to toggle source
# File lib/back_run/job.rb, line 57
def handle_job_failure(worker)
  @retries += 1
  if @retries <= 2
    @scheduled_at = (Time.now + MINUTES_PER_RETRY).to_f
    worker.retry_job(self)
  else
    worker.kill_job(self)
  end
end
register_duration() { || ... } click to toggle source
# File lib/back_run/job.rb, line 67
def register_duration
  start_time = Time.now
  yield
  end_time = Time.now
  @duration_seconds = (end_time - start_time) / 1.second
end