Class: BackRun::Job

Inherits:
Object
  • Object
show all
Defined in:
lib/back_run/job.rb

Constant Summary collapse

MAX_REMAINING_SECONDS =

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

600
MINUTES_PER_RETRY =
5.minutes

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, json_args, queue_name, scheduled_at, retries) ⇒ Job

Returns a new instance of Job.



12
13
14
15
16
17
18
# 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

Instance Attribute Details

#duration_secondsObject (readonly)

Returns the value of attribute duration_seconds.



10
11
12
# File 'lib/back_run/job.rb', line 10

def duration_seconds
  @duration_seconds
end

#klassObject (readonly)

Returns the value of attribute klass.



10
11
12
# File 'lib/back_run/job.rb', line 10

def klass
  @klass
end

#queue_nameObject (readonly)

Returns the value of attribute queue_name.



10
11
12
# File 'lib/back_run/job.rb', line 10

def queue_name
  @queue_name
end

Class Method Details

.from_json(json) ⇒ Object



24
25
26
27
28
29
30
# 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_from_active_job(job) ⇒ Object



20
21
22
# 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

Instance Method Details

#perform(worker) ⇒ Object



39
40
41
42
43
44
45
# 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_runObject



51
52
53
# 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?Boolean

Returns:

  • (Boolean)


47
48
49
# 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) ⇒ Object



32
33
34
35
36
37
# 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