From 51c376cc00eaec82d15c531ff05d51236bb105d0 Mon Sep 17 00:00:00 2001 From: multiple creatures Date: Sat, 14 Dec 2019 10:05:52 -0600 Subject: use redis lock instead of rolling own --- .../activitypub/fetch_account_statuses_service.rb | 54 +++++++++++----------- 1 file changed, 27 insertions(+), 27 deletions(-) (limited to 'app/services/activitypub') diff --git a/app/services/activitypub/fetch_account_statuses_service.rb b/app/services/activitypub/fetch_account_statuses_service.rb index d9d8266fa..bc47fa119 100644 --- a/app/services/activitypub/fetch_account_statuses_service.rb +++ b/app/services/activitypub/fetch_account_statuses_service.rb @@ -7,39 +7,39 @@ class ActivityPub::FetchAccountStatusesService < BaseService MAX_PAGES = 100 def call(account, url = nil) + return if account.local? || account.suspended? + @account = account - return if account.local? || account.suspended? || redis.get('busy_key') + @items = Rails.cache.fetch(sync_key) || [] + return if redis.get(cooldown_key).present? && @items.empty? - redis.set(busy_key, 1, ex: 3.days) + RedisLock.acquire(lock_options) do |lock| + return unless lock.acquired? - @items = Rails.cache.fetch(sync_key) || [] + redis.set(cooldown_key, 1, ex: 1.day) - return if redis.get(cooldown_key).present? && @items.empty? - redis.set(cooldown_key, 1, ex: 1.day) - - @json = fetch_collection(url || account.outbox_url) - page = 1 - - if @items.empty? - until page == MAX_PAGES || @json.blank? - items = collection_items(@json).select { |item| item['type'] == 'Create' } - @items.concat(items) - break if @json['next'].blank? - page += 1 - @json = fetch_collection(@json['next']) + @json = fetch_collection(url || account.outbox_url) + page = 1 + + if @items.empty? + until page == MAX_PAGES || @json.blank? + items = collection_items(@json).select { |item| item['type'] == 'Create' } + @items.concat(items) + break if @json['next'].blank? + page += 1 + @json = fetch_collection(@json['next']) + end end - end - Rails.cache.write(sync_key, @items, expires_in: 1.day) + Rails.cache.write(sync_key, @items, expires_in: 1.day) - process_items(@items) + process_items(@items) - Rails.cache.delete(sync_key) - redis.expire(cooldown_key, 1.week) + Rails.cache.delete(sync_key) + redis.expire(cooldown_key, 1.week) + end @items - ensure - redis.del(busy_key) end private @@ -48,14 +48,14 @@ class ActivityPub::FetchAccountStatusesService < BaseService "account_sync:#{@account.id}" end - def busy_key - "account_sync:#{@account.id}:busy" - end - def cooldown_key "account_sync:#{@account.id}:cooldown" end + def lock_options + { redis: Redis.current, key: "account_sync:#{@account.id}:lock" } + end + def fetch_collection(collection_or_uri) return collection_or_uri if collection_or_uri.is_a?(Hash) -- cgit