mirror of
https://github.com/codeninjasllc/codecombat.git
synced 2025-04-26 22:13:32 -04:00
Updated locking/release procedure
This commit is contained in:
parent
3ef5d88e2a
commit
449f1e1afc
1 changed files with 13 additions and 7 deletions
|
@ -1,5 +1,6 @@
|
|||
config = require '../../server_config'
|
||||
redis = require 'redis'
|
||||
|
||||
class LockManager
|
||||
constructor: ->
|
||||
unless config.isProduction or config.redis.host isnt "localhost"
|
||||
|
@ -10,15 +11,20 @@ class LockManager
|
|||
|
||||
setLock: (lockName, timeoutMs, cb) =>
|
||||
randomNumber = Math.floor(Math.random() * 1000000000)
|
||||
@redisClient.set [lockName,randomNumber, "NX", "PX", timeoutMs], (err, res) ->
|
||||
@redisClient.set [lockName,randomNumber, "NX", "PX", timeoutMs], (err, res) =>
|
||||
if err? then return cb err, null
|
||||
@lockValues[lockName] = randomNumber
|
||||
cb null, res
|
||||
if res is "OK"
|
||||
@lockValues[lockName] = randomNumber
|
||||
return cb null, "Lock set!"
|
||||
unless res
|
||||
return cb "Lock already set!", null
|
||||
|
||||
releaseLock: (lockName, cb) =>
|
||||
@redisClient.eval [@unlockScript, 1, lockName, @lockValues[lockName]], (err, res) ->
|
||||
if err? then return cb err, null
|
||||
#1 represents success, 0 failure
|
||||
cb null, Boolean(Number(res))
|
||||
|
||||
module.exports = new RedisLock()
|
||||
if res
|
||||
cb null, "The lock was released!"
|
||||
else
|
||||
cb "The lock was not released.", null
|
||||
|
||||
module.exports = new LockManager()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue