<commit_before>Settings = require "settings-sharelatex"
redis = require("redis-sharelatex")
rclient = redis.createClient(Settings.redis.lock)
os = require "os"
crypto = require "crypto"
logger = require "logger-sharelatex"

HOST = os.hostname()
PID = process.pid
RND = crypto.randomBytes(4).toString('hex')
COUNT = 0

module.exports = LockManager =
	LOCK_TEST_INTERVAL: 50 # 50ms between each test of the lock
	MAX_LOCK_WAIT_TIME: 10000 # 10s maximum time to spend trying to get the lock
	LOCK_TTL: 300 # seconds (allow 5 minutes for any operation to complete)

	# Use a signed lock value as described in
	# http://redis.io/topics/distlock#correct-implementation-with-a-single-instance
	# to prevent accidental unlocking by multiple processes
	randomLock : () ->
		time = Date.now()
		return "locked:host=#{HOST}:pid=#{PID}:random=#{RND}:time=#{time}:count=#{COUNT++}"

	unlockScript: 'if redis.call("get", KEYS[1]) == ARGV[1] then return redis.call("del", KEYS[1]) else return 0 end';

	tryLock : (key, callback = (err, gotLock) ->) ->
		lockValue = LockManager.randomLock()
		rclient.set key, lockValue, "EX", @LOCK_TTL, "NX", (err, gotLock)->
			return callback(err) if err?
			if gotLock == "OK"
				callback err, true, lockValue
			else
				callback err, false

	getLock: (key, callback = (error) ->) ->
		startTime = Date.now()
		do attempt = () ->
			if Date.now() - startTime > LockManager.MAX_LOCK_WAIT_TIME
				e = new Error("Timeout")
				e.key = key
				return callback(e)

			LockManager.tryLock key, (error, gotLock, lockValue) ->
				return callback(error) if error?
				if gotLock
					callback(null, lockValue)
				else
					setTimeout attempt, LockManager.LOCK_TEST_INTERVAL

	checkLock: (key, callback = (err, isFree) ->) ->
		rclient.exists key, (err, exists) ->
			return callback(err) if err?
			exists = parseInt exists
			if exists == 1
				callback err, false
			else
				callback err, true

	releaseLock: (key, lockValue, callback) ->
		rclient.eval LockManager.unlockScript, 1, key, lockValue, (err, result) ->
			if err?
				return callback(err)
			if result? and result isnt 1 # successful unlock should release exactly one key
				logger.error {key:key, lockValue:lockValue, redis_err:err, redis_result:result}, "unlocking error"
				return callback(new Error("tried to release timed out lock"))
			callback(err,result)

	runWithLock: (key, runner = ( (releaseLock = (error) ->) -> ), callback = ( (error) -> )) ->
		LockManager.getLock key, (error, lockValue) ->
			return callback(error) if error?
			runner (error1) ->
				LockManager.releaseLock key, lockValue, (error2) ->
					error = error1 or error2
					return callback(error) if error?
					callback()

	healthCheck: (callback) ->
		action = (releaseLock) ->
			releaseLock()
		LockManager.runWithLock	"HistoryLock:HealthCheck:host=#{HOST}:pid=#{PID}:random=#{RND}", action, callback

	close: (callback) ->
		rclient.quit()
		rclient.once 'end', callback
<commit_msg>removed unnecessary default argument<commit_after>Settings = require "settings-sharelatex"
redis = require("redis-sharelatex")
rclient = redis.createClient(Settings.redis.lock)
os = require "os"
crypto = require "crypto"
logger = require "logger-sharelatex"

HOST = os.hostname()
PID = process.pid
RND = crypto.randomBytes(4).toString('hex')
COUNT = 0

module.exports = LockManager =
	LOCK_TEST_INTERVAL: 50 # 50ms between each test of the lock
	MAX_LOCK_WAIT_TIME: 10000 # 10s maximum time to spend trying to get the lock
	LOCK_TTL: 300 # seconds (allow 5 minutes for any operation to complete)

	# Use a signed lock value as described in
	# http://redis.io/topics/distlock#correct-implementation-with-a-single-instance
	# to prevent accidental unlocking by multiple processes
	randomLock : () ->
		time = Date.now()
		return "locked:host=#{HOST}:pid=#{PID}:random=#{RND}:time=#{time}:count=#{COUNT++}"

	unlockScript: 'if redis.call("get", KEYS[1]) == ARGV[1] then return redis.call("del", KEYS[1]) else return 0 end';

	tryLock : (key, callback = (err, gotLock) ->) ->
		lockValue = LockManager.randomLock()
		rclient.set key, lockValue, "EX", @LOCK_TTL, "NX", (err, gotLock)->
			return callback(err) if err?
			if gotLock == "OK"
				callback err, true, lockValue
			else
				callback err, false

	getLock: (key, callback = (error) ->) ->
		startTime = Date.now()
		do attempt = () ->
			if Date.now() - startTime > LockManager.MAX_LOCK_WAIT_TIME
				e = new Error("Timeout")
				e.key = key
				return callback(e)

			LockManager.tryLock key, (error, gotLock, lockValue) ->
				return callback(error) if error?
				if gotLock
					callback(null, lockValue)
				else
					setTimeout attempt, LockManager.LOCK_TEST_INTERVAL

	checkLock: (key, callback = (err, isFree) ->) ->
		rclient.exists key, (err, exists) ->
			return callback(err) if err?
			exists = parseInt exists
			if exists == 1
				callback err, false
			else
				callback err, true

	releaseLock: (key, lockValue, callback) ->
		rclient.eval LockManager.unlockScript, 1, key, lockValue, (err, result) ->
			if err?
				return callback(err)
			if result? and result isnt 1 # successful unlock should release exactly one key
				logger.error {key:key, lockValue:lockValue, redis_err:err, redis_result:result}, "unlocking error"
				return callback(new Error("tried to release timed out lock"))
			callback(err,result)

	runWithLock: (key, runner, callback = ( (error) -> )) ->
		LockManager.getLock key, (error, lockValue) ->
			return callback(error) if error?
			runner (error1) ->
				LockManager.releaseLock key, lockValue, (error2) ->
					error = error1 or error2
					return callback(error) if error?
					callback()

	healthCheck: (callback) ->
		action = (releaseLock) ->
			releaseLock()
		LockManager.runWithLock	"HistoryLock:HealthCheck:host=#{HOST}:pid=#{PID}:random=#{RND}", action, callback

	close: (callback) ->
		rclient.quit()
		rclient.once 'end', callback

{'fraction_non_alphanumeric': 0.13218098627351296, 'fraction_numerical': 0.009489916963226572, 'mean_word_length': 4.061749571183533, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4664789', 'n_tokens_mistral': 2158, 'n_tokens_neox': 1876, 'n_words': 547}