2020-02-19 06:14:28 -05:00
|
|
|
/* eslint-disable
|
|
|
|
handle-callback-err,
|
|
|
|
no-unused-vars,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-02-19 06:14:14 -05:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS101: Remove unnecessary use of Array.from
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-02-19 06:14:37 -05:00
|
|
|
let LockManager
|
2021-07-12 12:47:21 -04:00
|
|
|
const Settings = require('@overleaf/settings')
|
2020-02-19 06:14:37 -05:00
|
|
|
const logger = require('logger-sharelatex')
|
|
|
|
const Lockfile = require('lockfile') // from https://github.com/npm/lockfile
|
|
|
|
const Errors = require('./Errors')
|
|
|
|
const fs = require('fs')
|
|
|
|
const Path = require('path')
|
|
|
|
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
|
2017-09-22 11:19:33 -04:00
|
|
|
|
2020-02-19 06:14:37 -05:00
|
|
|
runWithLock(path, runner, callback) {
|
|
|
|
if (callback == null) {
|
2020-08-10 12:01:11 -04:00
|
|
|
callback = function (error) {}
|
2020-02-19 06:14:37 -05:00
|
|
|
}
|
|
|
|
const lockOpts = {
|
|
|
|
wait: this.MAX_LOCK_WAIT_TIME,
|
|
|
|
pollPeriod: this.LOCK_TEST_INTERVAL,
|
2021-07-13 07:04:48 -04:00
|
|
|
stale: this.LOCK_STALE,
|
2020-02-19 06:14:37 -05:00
|
|
|
}
|
2020-08-10 12:01:11 -04:00
|
|
|
return Lockfile.lock(path, lockOpts, function (error) {
|
2020-02-19 06:14:37 -05:00
|
|
|
if ((error != null ? error.code : undefined) === 'EEXIST') {
|
|
|
|
return callback(new Errors.AlreadyCompilingError('compile in progress'))
|
|
|
|
} else if (error != null) {
|
|
|
|
return fs.lstat(path, (statLockErr, statLock) =>
|
|
|
|
fs.lstat(Path.dirname(path), (statDirErr, statDir) =>
|
2020-08-10 12:01:11 -04:00
|
|
|
fs.readdir(Path.dirname(path), function (readdirErr, readdirDir) {
|
2020-02-19 06:14:37 -05:00
|
|
|
logger.err(
|
|
|
|
{
|
|
|
|
error,
|
|
|
|
path,
|
|
|
|
statLock,
|
|
|
|
statLockErr,
|
|
|
|
statDir,
|
|
|
|
statDirErr,
|
|
|
|
readdirErr,
|
2021-07-13 07:04:48 -04:00
|
|
|
readdirDir,
|
2020-02-19 06:14:37 -05:00
|
|
|
},
|
|
|
|
'unable to get lock'
|
|
|
|
)
|
|
|
|
return callback(error)
|
|
|
|
})
|
|
|
|
)
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
return runner((error1, ...args) =>
|
2020-08-10 12:01:11 -04:00
|
|
|
Lockfile.unlock(path, function (error2) {
|
2020-02-19 06:14:37 -05:00
|
|
|
error = error1 || error2
|
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
return callback(null, ...Array.from(args))
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
2021-07-13 07:04:48 -04:00
|
|
|
},
|
2020-02-19 06:14:37 -05:00
|
|
|
}
|