From 2e635f94f626c9b1dcc1f7614e2fbef97449505a Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Thu, 13 Sep 2018 17:18:03 +1000 Subject: [PATCH] Make job queuing more robust and efficient --- .../products_cache_refreshment.rb | 32 ++++++++----------- .../products_cache_refreshment_spec.rb | 1 + 2 files changed, 14 insertions(+), 19 deletions(-) diff --git a/lib/open_food_network/products_cache_refreshment.rb b/lib/open_food_network/products_cache_refreshment.rb index 276734570f..17efd2acd5 100644 --- a/lib/open_food_network/products_cache_refreshment.rb +++ b/lib/open_food_network/products_cache_refreshment.rb @@ -17,31 +17,25 @@ module OpenFoodNetwork class ProductsCacheRefreshment def self.refresh(distributor, order_cycle) - unless pending_job? distributor, order_cycle - enqueue_job distributor, order_cycle - end + job = refresh_job(distributor, order_cycle) + enqueue_job(job) unless pending_job?(job) end - private - def self.pending_job?(distributor, order_cycle) - # To inspect each job, we need to deserialize the payload. - # This is slow, and if it's a problem in practice, we could pre-filter in SQL - # for handlers matching the class name, distributor id and order cycle id. - - Delayed::Job. - where(locked_at: nil). - map(&:payload_object). - select { |j| - j.class == RefreshProductsCacheJob && - j.distributor_id == distributor.id && - j.order_cycle_id == order_cycle.id - }.any? + def self.refresh_job(distributor, order_cycle) + RefreshProductsCacheJob.new(distributor.id, order_cycle.id) end - def self.enqueue_job(distributor, order_cycle) - Delayed::Job.enqueue RefreshProductsCacheJob.new(distributor.id, order_cycle.id), priority: 10 + def self.pending_job?(job) + Delayed::Job. + where(locked_at: nil). + where(handler: job.to_yaml). + exists? + end + + def self.enqueue_job(job) + Delayed::Job.enqueue job, priority: 10 end end end diff --git a/spec/lib/open_food_network/products_cache_refreshment_spec.rb b/spec/lib/open_food_network/products_cache_refreshment_spec.rb index f65b81346e..74998928d5 100644 --- a/spec/lib/open_food_network/products_cache_refreshment_spec.rb +++ b/spec/lib/open_food_network/products_cache_refreshment_spec.rb @@ -1,3 +1,4 @@ +require 'spec_helper' require 'open_food_network/products_cache_refreshment' module OpenFoodNetwork