about summary refs log tree commit diff
path: root/app/services/activitypub
diff options
context:
space:
mode:
authormultiple creatures <dev@multiple-creature.party>2019-12-10 01:13:08 -0600
committermultiple creatures <dev@multiple-creature.party>2019-12-10 02:36:38 -0600
commit882d089dd0d863ac6ca81b75b78472683b533420 (patch)
tree7e55aba26f19f523402cc0c77a14aa2e511705c1 /app/services/activitypub
parent002fb7fbb7f7a3d3b4fa2c6a1d72c3384fcfc20b (diff)
add cooldown to post sync just in case
Diffstat (limited to 'app/services/activitypub')
-rw-r--r--app/services/activitypub/fetch_account_statuses_service.rb33
1 files changed, 28 insertions, 5 deletions
diff --git a/app/services/activitypub/fetch_account_statuses_service.rb b/app/services/activitypub/fetch_account_statuses_service.rb
index 59d4c52bf..27fa8bd6c 100644
--- a/app/services/activitypub/fetch_account_statuses_service.rb
+++ b/app/services/activitypub/fetch_account_statuses_service.rb
@@ -2,17 +2,23 @@
 
 class ActivityPub::FetchAccountStatusesService < BaseService
   include JsonLdHelper
+  include Redisable
 
   MAX_PAGES = 50
 
   def call(account, url = nil)
     @account = account
-    return if account.local? || account.suspended?
+    return if account.local? || account.suspended? || redis.get('busy_key')
 
-    page = 1
+    redis.set(busy_key, 1, ex: 3.days)
+
+    @items = Rails.cache.fetch(sync_key) || []
+
+    return if redis.get(cooldown_key) && @items.empty?
+    redis.set(cooldown_key, 1, ex: 1.day)
 
     @json = fetch_collection(url || account.outbox_url)
-    @items = Rails.cache.fetch("account_sync:#{account.id}") || []
+    page = 1
 
     if @items.empty?
       until page == MAX_PAGES || @json.blank?
@@ -24,15 +30,32 @@ class ActivityPub::FetchAccountStatusesService < BaseService
       end
     end
 
-    Rails.cache.write("account_sync:#{account.id}", @items, expires_in: 1.day)
+    Rails.cache.write(sync_key, @items, expires_in: 1.day)
+
     process_items(@items)
-    Rails.cache.delete("account_sync:#{account.id}")
+
+    Rails.cache.delete(sync_key)
+    redis.expire(cooldown_key, 1.week)
 
     @items
+  ensure
+    redis.del(busy_key)
   end
 
   private
 
+  def sync_key
+    "account_sync:#{@account.id}"
+  end
+
+  def busy_key
+    "account_sync:#{@account.id}:busy"
+  end
+
+  def cooldown_key
+    "account_sync:#{@account.id}:cooldown"
+  end
+
   def fetch_collection(collection_or_uri)
     return collection_or_uri if collection_or_uri.is_a?(Hash)