Sha256: d4dc2b1358e4529266b49c8cae59d2a29fb88b832f7f04eb8ee0d1bc75bcfe86

Contents?: true

Size: 1.32 KB

Versions: 3

Compression:

Stored size: 1.32 KB

Contents

# 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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
wayfarer-0.4.6 lib/wayfarer/base.rb
wayfarer-0.4.5 lib/wayfarer/base.rb
wayfarer-0.4.4 lib/wayfarer/base.rb