|
| 1 | +import * as lockfile from "lockfile"; |
| 2 | +import * as path from "path"; |
| 3 | +import { cache } from "../decorators"; |
| 4 | + |
| 5 | +export class LockService implements ILockService { |
| 6 | + private currentlyLockedFiles: string[] = []; |
| 7 | + |
| 8 | + @cache() |
| 9 | + private get defaultLockFilePath(): string { |
| 10 | + return this.getAbsoluteLockFilePath("lockfile.lock"); |
| 11 | + } |
| 12 | + |
| 13 | + private getAbsoluteLockFilePath(relativeLockFilePath: string) { |
| 14 | + return path.join(this.$settingsService.getProfileDir(), relativeLockFilePath); |
| 15 | + } |
| 16 | + |
| 17 | + private get defaultLockParams(): ILockOptions { |
| 18 | + // We'll retry 100 times and time between retries is 100ms, i.e. full wait in case we are unable to get lock will be 10 seconds. |
| 19 | + // In case lock is older than the `stale` value, consider it stale and try to get a new lock. |
| 20 | + const lockParams: ILockOptions = { |
| 21 | + retryWait: 100, |
| 22 | + retries: 100, |
| 23 | + stale: 30 * 1000, |
| 24 | + }; |
| 25 | + |
| 26 | + return lockParams; |
| 27 | + } |
| 28 | + |
| 29 | + constructor(private $fs: IFileSystem, |
| 30 | + private $settingsService: ISettingsService, |
| 31 | + private $processService: IProcessService) { |
| 32 | + this.$processService.attachToProcessExitSignals(this, () => { |
| 33 | + const locksToRemove = _.clone(this.currentlyLockedFiles); |
| 34 | + _.each(locksToRemove, lock => { |
| 35 | + this.unlock(lock); |
| 36 | + }); |
| 37 | + }); |
| 38 | + } |
| 39 | + |
| 40 | + public async executeActionWithLock<T>(action: () => Promise<T>, lockFilePath?: string, lockOpts?: ILockOptions): Promise<T> { |
| 41 | + const resolvedLockFilePath = await this.lock(lockFilePath, lockOpts); |
| 42 | + |
| 43 | + try { |
| 44 | + const result = await action(); |
| 45 | + return result; |
| 46 | + } finally { |
| 47 | + this.unlock(resolvedLockFilePath); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + private lock(lockFilePath?: string, lockOpts?: ILockOptions): Promise<string> { |
| 52 | + const { filePath, fileOpts } = this.getLockFileSettings(lockFilePath, lockOpts); |
| 53 | + this.currentlyLockedFiles.push(filePath); |
| 54 | + |
| 55 | + // Prevent ENOENT error when the dir, where lock should be created, does not exist. |
| 56 | + this.$fs.ensureDirectoryExists(path.dirname(filePath)); |
| 57 | + |
| 58 | + return new Promise<string>((resolve, reject) => { |
| 59 | + lockfile.lock(filePath, fileOpts, (err: Error) => { |
| 60 | + err ? reject(new Error(`Timeout while waiting for lock "${filePath}"`)) : resolve(filePath); |
| 61 | + }); |
| 62 | + }); |
| 63 | + } |
| 64 | + |
| 65 | + private unlock(lockFilePath?: string): void { |
| 66 | + const { filePath } = this.getLockFileSettings(lockFilePath); |
| 67 | + _.remove(this.currentlyLockedFiles, e => e === lockFilePath); |
| 68 | + lockfile.unlockSync(filePath); |
| 69 | + } |
| 70 | + |
| 71 | + private getLockFileSettings(filePath?: string, fileOpts?: ILockOptions): { filePath: string, fileOpts: ILockOptions } { |
| 72 | + if (filePath && !path.isAbsolute(filePath)) { |
| 73 | + filePath = this.getAbsoluteLockFilePath(filePath); |
| 74 | + } |
| 75 | + |
| 76 | + filePath = filePath || this.defaultLockFilePath; |
| 77 | + fileOpts = fileOpts || this.defaultLockParams; |
| 78 | + |
| 79 | + return { |
| 80 | + filePath, |
| 81 | + fileOpts |
| 82 | + }; |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +$injector.register("lockService", LockService); |
| 87 | +// backwards compatibility |
| 88 | +$injector.register("lockfile", LockService); |
0 commit comments