# frozen_string_literal: true module Wayfarer class Base < ActiveJob::Base include Wayfarer::Middleware::Controller use Wayfarer::Middleware::Stage use Wayfarer::Middleware::Dedup use Wayfarer::Middleware::Normalize use Wayfarer::Middleware::Router use Wayfarer::Middleware::Fetch use Wayfarer::Middleware::Dispatch ErrorHandler = lambda do |&block| lambda do |job, error| task = job.arguments.first task.barrier.seen?(task.url) task.gc.run block.call(job, error) end end after_enqueue do |job| task = job.arguments.first task.counter.increment end after_perform do |job| task = job.arguments.first task.gc.run end rescue_from(StandardError) do task = arguments.first task.gc.run end def self.retry_on(*argv, &block) super(*argv, &ErrorHandler.call(&block)) end def self.discard_on(*argv, &block) super(*argv, &ErrorHandler.call(&block)) end def self.crawl(url, batch: SecureRandom.uuid) Task.new(url, batch).tap do |task| perform_later(task) end end def retry_job(...) super(...) # increments the counter by re-enqueuing the job task = arguments.first task.counter.decrement end alias perform call end end