rwlockfile
Version:
lockfile utility with reader/writers
87 lines (86 loc) • 2.4 kB
TypeScript
import { LockfileOptions } from './lockfile';
export declare type ReadStatus = {
status: 'open';
file: string;
} | {
status: 'write_lock';
job: Job;
file: string;
};
export declare type WriteStatus = ReadStatus | {
status: 'read_lock';
file: string;
jobs: Job[];
};
export declare type Status = WriteStatus;
export interface RWLockOptions {
reason?: string;
ifLocked?: IfLockedFn;
timeout?: number;
retryInterval?: number;
}
export interface RWLockfileOptions extends LockfileOptions {
timeout?: number;
retryInterval?: number;
ifLocked?: IfLockedFn;
}
export declare type RWLockType = 'read' | 'write';
export interface Job {
uuid: string;
pid: number;
reason?: string;
created: Date;
}
export interface IfLockedFn {
({status}: {
status: Status;
}): Promise<void> | void;
}
export declare class RWLockfile {
base: string;
ifLocked: IfLockedFn;
timeout: number;
retryInterval: number;
private _debug;
private uuid;
private fs;
private internal;
private _count;
/**
* creates a new read/write lockfile
* @param base {string} - base filepath to create lock from
*/
constructor(base: string, options?: RWLockfileOptions);
readonly count: {
readonly read: number;
readonly write: number;
};
readonly file: string;
add(type: RWLockType, opts?: RWLockOptions): Promise<void>;
addSync(type: RWLockType, opts?: {
reason?: string;
}): void;
remove(type: RWLockType): Promise<void>;
removeSync(type: RWLockType): void;
unlock(type?: RWLockType): Promise<void>;
unlockSync(type?: RWLockType): void;
check(type: RWLockType): Promise<Status>;
checkSync(type: RWLockType): Status;
private _statusFromFile(type, f);
private _parseFile(input);
private _stringifyFile(input);
private _fetchFile();
private _fetchFileSync();
private addJob(type, reason, f);
private _removeJob(type);
private _removeJobSync(type);
private _removeJobFromFile(type, f);
private _lock(type, opts);
tryLock(type: RWLockType, reason?: string, inc?: boolean): Promise<void>;
private _lockSync(type, reason?);
private writeFile(f);
private writeFileSync(f);
private readonly debug;
private _debugReport(action, type, {reason}?);
}
export default RWLockfile;