Sha256: 7fa499a0ba92997ede717c94ce0c9d80a953d6e24d6bd5ff2a3f8b33b2c6be9f

Contents?: true

Size: 804 Bytes

Versions: 5

Compression:

Stored size: 804 Bytes

Contents

class TbRedirects::DetectRedirectLoopJob < ActiveJob::Base
  queue_as :default

  attr_reader :attempts
  MAX_ATTEMPTS = 100

  def initialize(*arguments)
    super(*arguments)
    @attempts = 0
  end

  def perform(redirect)
    parents = TbRedirect.where(destination: redirect.source)
    parents.each { |p| search_parent(p, redirect.destination) }
  end

  private

  class AttemptsExceeded < StandardError
  end

  def search_parent(parent, destination_to_check)
    @attempts += 1
    raise AttemptsExceeded, "Exceeded #{MAX_ATTEMPTS} max attempts" if @attempts > MAX_ATTEMPTS

    if parent.source == destination_to_check
      parent.destroy
    else
      parents = TbRedirect.where(destination: parent.source)
      parents.each { |p| search_parent(p, destination_to_check) }
    end
  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
tb_redirects-1.0.3 app/jobs/tb_redirects/detect_redirect_loop_job.rb
tb_redirects-1.0.2 app/jobs/tb_redirects/detect_redirect_loop_job.rb
tb_redirects-1.0.1 app/jobs/tb_redirects/detect_redirect_loop_job.rb
tb_redirects-1.0 app/jobs/tb_redirects/detect_redirect_loop_job.rb
tb_redirects-1.0.beta1 app/jobs/tb_redirects/detect_redirect_loop_job.rb