lib/wayfarer/middleware/dedup.rb in wayfarer-0.4.6 vs lib/wayfarer/middleware/dedup.rb in wayfarer-0.4.7
- old
+ new
@@ -3,28 +3,31 @@
module Wayfarer
module Middleware
class Dedup
extend Base
+ include Wayfarer::Logging.emit(
+ deduplicated: [:info, "Deduplicated URL"],
+ retry: [:debug, "Not deduplicating retry"],
+ rerouted: [:debug, "Not deduplicating rerouted task"]
+ )
+
def call(task)
- # Was task routed by a previous controller already?
- return yield if task.metadata.action
+ task[:barrier] ||= Wayfarer::Redis::Barrier.new(task)
- return if task.barrier.seen?(task.url)
+ if task[:job].executions > 1
+ log(:retry, task)
+ return yield if block_given?
+ end
- begin
- yield if block_given?
- rescue StandardError => e
- task.barrier.unsee(task.url)
- raise e
+ if task[:job] != task[:controller]
+ log(:rerouted, task)
+ return yield if block_given?
end
- staged_urls = task.metadata.staged_urls
- return if staged_urls.none?
+ return log(:deduplicated, task) if task[:barrier].check!(task[:normalized_url])
- inclusion = task.barrier.peek(staged_urls.to_a)
- unseen = staged_urls.zip(inclusion).reject { |_, seen| seen }.map(&:first)
- task.metadata.staged_urls = SortedSet.new(unseen)
+ yield if block_given?
end
end
end
end