FIX: Jobs.cancel_scheduled_job wasn't working anymore due to our move to using multiple queues

FIX: Don't queue more than 1 'update_gravatar' job per user
This commit is contained in:
Régis Hanol 2016-04-13 18:12:28 +02:00
parent b782a01148
commit b7c16991f7
3 changed files with 20 additions and 14 deletions

View File

@ -244,21 +244,25 @@ module Jobs
def self.scheduled_for(job_name, params={}) def self.scheduled_for(job_name, params={})
params = params.with_indifferent_access params = params.with_indifferent_access
job_class = "Jobs::#{job_name.to_s.camelcase}" job_class = "Jobs::#{job_name.to_s.camelcase}"
Sidekiq::ScheduledSet.new.select do |scheduled_job|
if scheduled_job.klass.to_s == job_class scheduled_jobs = []
matched = true Sidekiq::Queue.all.each do |queue|
job_params = scheduled_job.item["args"][0].with_indifferent_access queue.each do |scheduled_job|
params.each do |key, value| if scheduled_job.klass.to_s == job_class
if job_params[key] != value matched = true
matched = false job_params = scheduled_job.item["args"][0].with_indifferent_access
break params.each do |key, value|
if job_params[key] != value
matched = false
break
end
end end
scheduled_jobs << scheduled_job if matched
end end
matched
else
false
end end
end end
scheduled_jobs
end end
end end

View File

@ -7,9 +7,10 @@ module Jobs
UserAvatar.includes(:user) UserAvatar.includes(:user)
.where(last_gravatar_download_attempt: nil) .where(last_gravatar_download_attempt: nil)
.order("users.last_posted_at DESC") .order("users.last_posted_at DESC")
.limit(5000) .find_in_batches(batch_size: 5000) do |user_avatars|
.each do |u| user_avatars.each do |user_avatar|
u.user.refresh_avatar user_avatar.user.refresh_avatar
end
end end
end end
end end

View File

@ -753,6 +753,7 @@ class User < ActiveRecord::Base
avatar = user_avatar || create_user_avatar avatar = user_avatar || create_user_avatar
if SiteSetting.automatically_download_gravatars? && !avatar.last_gravatar_download_attempt if SiteSetting.automatically_download_gravatars? && !avatar.last_gravatar_download_attempt
Jobs.cancel_scheduled_job(:update_gravatar, user_id: self.id, avatar_id: avatar.id)
Jobs.enqueue(:update_gravatar, user_id: self.id, avatar_id: avatar.id) Jobs.enqueue(:update_gravatar, user_id: self.id, avatar_id: avatar.id)
end end