Module: RocketJob::Plugins::Job::Persistence

Extended by:
ActiveSupport::Concern
Included in:
Job
Defined in:
lib/rocket_job/plugins/job/persistence.rb

Overview

Prevent more than one instance of this job class from running at a time

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#create_restart!(**overrides) ⇒ Object

Create a new instance of this job, copying across only the ‘copy_on_restart` attributes. Copy across input and output categories to new scheduled job so that all of the settings are remembered between instance. Example: slice_size



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/rocket_job/plugins/job/persistence.rb', line 76

def create_restart!(**overrides)
  if expired?
    logger.info("Job has expired. Not creating a new instance.")
    return
  end

  job_attrs = self.class.rocket_job_restart_attributes.each_with_object({}) do |attr, attrs|
    attrs[attr] = send(attr)
  end
  job_attrs.merge!(overrides)

  job                   = self.class.new(job_attrs)
  job.input_categories  = input_categories if respond_to?(:input_categories)
  job.output_categories = output_categories if respond_to?(:output_categories)

  job.save_with_retry!

  logger.info("Created a new job instance: #{job.id}")
end

#reloadObject

Set in-memory job to complete if ‘destroy_on_complete` and the job has been destroyed



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/rocket_job/plugins/job/persistence.rb', line 97

def reload
  return super unless destroy_on_complete

  begin
    super
  rescue ::Mongoid::Errors::DocumentNotFound
    unless completed?
      self.state = :completed
      rocket_job_set_completed_at
      rocket_job_mark_complete
    end
    self
  end
end

#save_with_retry!(retry_limit = 10, sleep_interval = 0.5) ⇒ Object

Save with retry in case persistence takes a moment.



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/rocket_job/plugins/job/persistence.rb', line 113

def save_with_retry!(retry_limit = 10, sleep_interval = 0.5)
  count = 0
  while count < retry_limit
    return true if save

    logger.info("Retrying to persist new scheduled instance: #{errors.messages.inspect}")
    sleep(sleep_interval)
    count += 1
  end
  save!
end