Sha256: b4206b827664e649d0a4da67c8bcf911198f9c1159986b5a6bcce97906b963eb

Contents?: true

Size: 1.27 KB

Versions: 1

Compression:

Stored size: 1.27 KB

Contents

Delayed::Backend::ActiveRecord::Job.class_eval do

  belongs_to :record, :polymorphic => true

  # When enqueue hook is executed, we need to look if there's an identifier provided
  # If there's another Delayed::Job out there with same identifier we need to bail
  def hook(name, *args)
    super

    if name == :enqueue && self.identifier.present?
      if Delayed::Job.where(:identifier => self.identifier, :completed_at => nil, :failed_at => nil).any?
        raise DelayedJobProgress::DuplicateJobError, "Delayed::Job with identifier: #{self.identifier} already present"
      end
    end
  end

  # Associating AR record with Delayed::Job. Generally when doing: `something.delay.method`
  def payload_object=(object)
    if object.respond_to?(:object) && object.object.is_a?(ActiveRecord::Base)
      self.record = object.object
    end

    super
  end

  def destroy_completed_jobs?
    payload_object.respond_to?(:destroy_completed_jobs?) ?
      payload_object.destroy_completed_jobs? :
      Delayed::Worker.destroy_completed_jobs
  end

  def destroy(force_destroy = false)
    if destroy_completed_jobs? || force_destroy
      super()
    else
      update_columns(
        :completed_at     => Time.zone.now,
        :progress_current => self.progress_max
      )
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
delayed_job_progress-0.0.1 lib/delayed_job_progress/extensions/job.rb