Make job queuing more robust and efficient

This commit is contained in:
Maikel Linke
2018-09-13 17:18:03 +10:00
parent 065df96872
commit 2e635f94f6
2 changed files with 14 additions and 19 deletions

View File

@@ -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

View File

@@ -1,3 +1,4 @@
require 'spec_helper'
require 'open_food_network/products_cache_refreshment'
module OpenFoodNetwork