about summary refs log tree commit diff
diff options
context:
space:
mode:
authormultiple creatures <dev@multiple-creature.party>2019-12-14 10:05:52 -0600
committermultiple creatures <dev@multiple-creature.party>2019-12-14 10:05:52 -0600
commit51c376cc00eaec82d15c531ff05d51236bb105d0 (patch)
tree7064431c29342acb7f50291cea6514f00cfd241a
parent026b0626cf60c02917f279923e13e5713310206e (diff)
use redis lock instead of rolling own
-rw-r--r--app/services/activitypub/fetch_account_statuses_service.rb54
1 files changed, 27 insertions, 27 deletions
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)