about summary refs log tree commit diff
path: root/app/models/account_migration.rb
diff options
context:
space:
mode:
authorClaire <claire.github-309c@sitedethib.com>2021-02-03 18:57:38 +0100
committerGitHub <noreply@github.com>2021-02-03 18:57:38 +0100
commit4ed9576cd28abc033a094fed6babe9825014f1af (patch)
treed6d5e7b07e24248da5c237726dca874727c10b38 /app/models/account_migration.rb
parent7632255115a7f6e6f9f8047f0d0d1b90d7ee9315 (diff)
parente1fa06c459a28a575d0da540432c61b702e99cdd (diff)
Merge pull request #1496 from ClearlyClaire/glitch-soc/merge-upstream
Merge upstream changes
Diffstat (limited to 'app/models/account_migration.rb')
-rw-r--r--app/models/account_migration.rb14
1 files changed, 13 insertions, 1 deletions
diff --git a/app/models/account_migration.rb b/app/models/account_migration.rb
index 4fae98ed7..ded32c9c6 100644
--- a/app/models/account_migration.rb
+++ b/app/models/account_migration.rb
@@ -14,6 +14,8 @@
 #
 
 class AccountMigration < ApplicationRecord
+  include Redisable
+
   COOLDOWN_PERIOD = 30.days.freeze
 
   belongs_to :account
@@ -39,7 +41,13 @@ class AccountMigration < ApplicationRecord
 
     return false unless errors.empty?
 
-    save
+    RedisLock.acquire(lock_options) do |lock|
+      if lock.acquired?
+        save
+      else
+        raise Mastodon::RaceConditionError
+      end
+    end
   end
 
   def cooldown_at
@@ -75,4 +83,8 @@ class AccountMigration < ApplicationRecord
   def validate_migration_cooldown
     errors.add(:base, I18n.t('migrations.errors.on_cooldown')) if account.migrations.within_cooldown.exists?
   end
+
+  def lock_options
+    { redis: redis, key: "account_migration:#{account.id}" }
+  end
 end