mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
Extract scheduler cross-process locking into DistributedMutex.
This commit is contained in:
@@ -4,6 +4,8 @@
|
||||
# 2. No stats about previous runs or failures
|
||||
# 3. Dependency on ice_cube gem causes runaway CPU
|
||||
|
||||
require_dependency 'distributed_mutex'
|
||||
|
||||
module Scheduler
|
||||
class Manager
|
||||
attr_accessor :random_ratio, :redis
|
||||
@@ -220,33 +222,9 @@ module Scheduler
|
||||
end
|
||||
|
||||
def lock
|
||||
got_lock = false
|
||||
lock_key = Manager.lock_key
|
||||
|
||||
while(!got_lock)
|
||||
begin
|
||||
if redis.setnx lock_key, Time.now.to_i + 60
|
||||
redis.expire lock_key, 60
|
||||
got_lock = true
|
||||
else
|
||||
begin
|
||||
redis.watch lock_key
|
||||
time = redis.get Manager.lock_key
|
||||
if time && time.to_i < Time.now.to_i
|
||||
got_lock = redis.multi do
|
||||
redis.set Manager.lock_key, Time.now.to_i + 60
|
||||
end
|
||||
end
|
||||
ensure
|
||||
redis.unwatch
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
DistributedMutex.new(Manager.lock_key).synchronize do
|
||||
yield
|
||||
end
|
||||
yield
|
||||
ensure
|
||||
redis.del Manager.lock_key
|
||||
end
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user