DockerLockManager.js 3.4 KB

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