2017-09-22 15:19:33 +00:00
Settings = require ( ' settings-sharelatex ' )
logger = require " logger-sharelatex "
Lockfile = require ( ' lockfile ' ) # from https://github.com/npm/lockfile
Errors = require " ./Errors "
2018-08-19 10:38:27 +00:00
fs = require ( " fs " )
Path = require ( " path " )
2017-09-22 15:19:33 +00:00
module.exports = LockManager =
LOCK_TEST_INTERVAL: 1000 # 50ms between each test of the lock
MAX_LOCK_WAIT_TIME: 15000 # 10s maximum time to spend trying to get the lock
LOCK_STALE: 5 * 60 * 1000 # 5 mins time until lock auto expires
2020-02-19 11:06:09 +00:00
runWithLock: ( path , runner , callback = ( (error) -> ) ) ->
2017-09-22 15:19:33 +00:00
lockOpts =
wait: @ MAX_LOCK_WAIT_TIME
pollPeriod: @ LOCK_TEST_INTERVAL
stale: @ LOCK_STALE
Lockfile . lock path , lockOpts , (error) ->
2018-08-22 23:12:05 +00:00
if error ? . code is ' EEXIST '
return callback new Errors . AlreadyCompilingError ( " compile in progress " )
else if error ?
fs . lstat path , (statLockErr, statLock)->
fs . lstat Path . dirname ( path ) , (statDirErr, statDir)->
fs . readdir Path . dirname ( path ) , (readdirErr, readdirDir)->
2018-08-22 22:54:40 +00:00
logger . err error : error , path : path , statLock : statLock , statLockErr : statLockErr , statDir : statDir , statDirErr: statDirErr , readdirErr : readdirErr , readdirDir : readdirDir , " unable to get lock "
return callback ( error )
2018-08-22 23:12:05 +00:00
else
runner (error1, args...) ->
Lockfile . unlock path , (error2) ->
error = error1 or error2
return callback ( error ) if error ?
callback ( null , args . . . )