mirror of
https://github.com/codeninjasllc/codecombat.git
synced 2025-04-29 07:24:08 -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'
|
config = require '../../server_config'
|
||||||
redis = require 'redis'
|
redis = require 'redis'
|
||||||
|
|
||||||
class LockManager
|
class LockManager
|
||||||
constructor: ->
|
constructor: ->
|
||||||
unless config.isProduction or config.redis.host isnt "localhost"
|
unless config.isProduction or config.redis.host isnt "localhost"
|
||||||
|
@ -10,15 +11,20 @@ class LockManager
|
||||||
|
|
||||||
setLock: (lockName, timeoutMs, cb) =>
|
setLock: (lockName, timeoutMs, cb) =>
|
||||||
randomNumber = Math.floor(Math.random() * 1000000000)
|
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
|
if err? then return cb err, null
|
||||||
@lockValues[lockName] = randomNumber
|
if res is "OK"
|
||||||
cb null, res
|
@lockValues[lockName] = randomNumber
|
||||||
|
return cb null, "Lock set!"
|
||||||
|
unless res
|
||||||
|
return cb "Lock already set!", null
|
||||||
|
|
||||||
releaseLock: (lockName, cb) =>
|
releaseLock: (lockName, cb) =>
|
||||||
@redisClient.eval [@unlockScript, 1, lockName, @lockValues[lockName]], (err, res) ->
|
@redisClient.eval [@unlockScript, 1, lockName, @lockValues[lockName]], (err, res) ->
|
||||||
if err? then return cb err, null
|
if err? then return cb err, null
|
||||||
#1 represents success, 0 failure
|
if res
|
||||||
cb null, Boolean(Number(res))
|
cb null, "The lock was released!"
|
||||||
|
else
|
||||||
module.exports = new RedisLock()
|
cb "The lock was not released.", null
|
||||||
|
|
||||||
|
module.exports = new LockManager()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue