utils.ts 1.58 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import fs from 'fs';
import path from 'path';
import * as timersPromises from 'timers/promises';

import glob from 'glob';
import lockfile from 'lockfile';

import globals from 'common/globals';

const lockStale: number = 2000;
const retry: number = 100;

export function withLockNoWait<T>(protectedFile: string, func: () => T): T {
    const lockName = path.join(path.dirname(protectedFile), path.basename(protectedFile) + `.lock.${process.pid}`);
    const lockPath = path.join(path.dirname(protectedFile), path.basename(protectedFile) + '.lock.*');
    const lockFileNames: string[] = glob.sync(lockPath);
    const canLock: boolean = lockFileNames.map((fileName) => {
        return fs.existsSync(fileName) && Date.now() - fs.statSync(fileName).mtimeMs < lockStale;
    }).filter(unexpired=>unexpired === true).length === 0;
    if (!canLock) {
        throw new Error('File has been locked.');
    }
    lockfile.lockSync(lockName, { stale: lockStale });
    const result = func();
    lockfile.unlockSync(lockName);
    return result;
}

export async function withLock<T>(protectedFile: string, func: () => T): Promise<T> {
    for (let i = 0; i < retry; i += 1) {
        try {
            return withLockNoWait(protectedFile, func);
        } catch (error: any) {
            if (error.code === 'EEXIST' || error.message === 'File has been locked.') {
                await timersPromises.setTimeout(50);
            } else {
                throw error;
            }
        }
    }
    throw new Error('Lock file out of retries.');
}