DockerLockManager.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // TODO: This file was created by bulk-decaffeinate.
  2. // Fix any style issues and re-enable lint.
  3. /*
  4. * decaffeinate suggestions:
  5. * DS101: Remove unnecessary use of Array.from
  6. * DS102: Remove unnecessary code created because of implicit returns
  7. * DS207: Consider shorter variations of null checks
  8. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  9. */
  10. import logger from '@overleaf/logger'
  11. let LockManager
  12. const LockState = {} // locks for docker container operations, by container name
  13. export default LockManager = {
  14. MAX_LOCK_HOLD_TIME: 15000, // how long we can keep a lock
  15. MAX_LOCK_WAIT_TIME: 10000, // how long we wait for a lock
  16. LOCK_TEST_INTERVAL: 1000, // retry time
  17. tryLock(key, callback) {
  18. let lockValue
  19. if (callback == null) {
  20. callback = function () {}
  21. }
  22. const existingLock = LockState[key]
  23. if (existingLock != null) {
  24. // the lock is already taken, check how old it is
  25. const lockAge = Date.now() - existingLock.created
  26. if (lockAge < LockManager.MAX_LOCK_HOLD_TIME) {
  27. return callback(null, false) // we didn't get the lock, bail out
  28. } else {
  29. logger.error(
  30. { key, lock: existingLock, age: lockAge },
  31. 'taking old lock by force'
  32. )
  33. }
  34. }
  35. // take the lock
  36. LockState[key] = lockValue = { created: Date.now() }
  37. return callback(null, true, lockValue)
  38. },
  39. getLock(key, callback) {
  40. let attempt
  41. if (callback == null) {
  42. callback = function () {}
  43. }
  44. const startTime = Date.now()
  45. return (attempt = () =>
  46. LockManager.tryLock(key, function (error, gotLock, lockValue) {
  47. if (error != null) {
  48. return callback(error)
  49. }
  50. if (gotLock) {
  51. return callback(null, lockValue)
  52. } else if (Date.now() - startTime > LockManager.MAX_LOCK_WAIT_TIME) {
  53. const e = new Error('Lock timeout')
  54. e.key = key
  55. return callback(e)
  56. } else {
  57. return setTimeout(attempt, LockManager.LOCK_TEST_INTERVAL)
  58. }
  59. }))()
  60. },
  61. releaseLock(key, lockValue, callback) {
  62. if (callback == null) {
  63. callback = function () {}
  64. }
  65. const existingLock = LockState[key]
  66. if (existingLock === lockValue) {
  67. // lockValue is an object, so we can test by reference
  68. delete LockState[key] // our lock, so we can free it
  69. return callback()
  70. } else if (existingLock != null) {
  71. // lock exists but doesn't match ours
  72. logger.error(
  73. { key, lock: existingLock },
  74. 'tried to release lock taken by force'
  75. )
  76. return callback()
  77. } else {
  78. logger.error(
  79. { key, lock: existingLock },
  80. 'tried to release lock that has gone'
  81. )
  82. return callback()
  83. }
  84. },
  85. runWithLock(key, runner, callback) {
  86. if (callback == null) {
  87. callback = function () {}
  88. }
  89. return LockManager.getLock(key, function (error, lockValue) {
  90. if (error != null) {
  91. return callback(error)
  92. }
  93. return runner((error1, ...args) =>
  94. LockManager.releaseLock(key, lockValue, function (error2) {
  95. error = error1 || error2
  96. if (error != null) {
  97. return callback(error)
  98. }
  99. return callback(null, ...Array.from(args))
  100. })
  101. )
  102. })
  103. },
  104. }