LockManager.coffee 949 B

1234567891011121314151617181920212223
  1. Settings = require('settings-sharelatex')
  2. logger = require "logger-sharelatex"
  3. Lockfile = require('lockfile') # from https://github.com/npm/lockfile
  4. Errors = require "./Errors"
  5. module.exports = LockManager =
  6. LOCK_TEST_INTERVAL: 1000 # 50ms between each test of the lock
  7. MAX_LOCK_WAIT_TIME: 15000 # 10s maximum time to spend trying to get the lock
  8. LOCK_STALE: 5*60*1000 # 5 mins time until lock auto expires
  9. runWithLock: (path, runner = ((releaseLock = (error) ->) ->), callback = ((error) ->)) ->
  10. lockOpts =
  11. wait: @MAX_LOCK_WAIT_TIME
  12. pollPeriod: @LOCK_TEST_INTERVAL
  13. stale: @LOCK_STALE
  14. Lockfile.lock path, lockOpts, (error) ->
  15. return callback new Errors.AlreadyCompilingError("compile in progress") if error?.code is 'EEXIST'
  16. return callback(error) if error?
  17. runner (error1, args...) ->
  18. Lockfile.unlock path, (error2) ->
  19. error = error1 or error2
  20. return callback(error) if error?
  21. callback(null, args...)