mirror of
https://github.com/danog/file.git
synced 2024-12-02 09:17:57 +01:00
Add Filesystem arg to FileMutex
This commit is contained in:
parent
8e063788e8
commit
0bc3e2d251
@ -11,11 +11,14 @@ final class FileMutex implements Mutex
|
||||
{
|
||||
private const LATENCY_TIMEOUT = 0.01;
|
||||
|
||||
private readonly Filesystem $filesystem;
|
||||
|
||||
/**
|
||||
* @param string $fileName Name of temporary file to use as a mutex.
|
||||
*/
|
||||
public function __construct(private readonly string $fileName)
|
||||
public function __construct(private readonly string $fileName, ?Filesystem $filesystem = null)
|
||||
{
|
||||
$this->filesystem = $filesystem ?? filesystem();
|
||||
}
|
||||
|
||||
public function acquire(): Lock
|
||||
@ -24,7 +27,7 @@ final class FileMutex implements Mutex
|
||||
// has the lock, so set an asynchronous timer and try again.
|
||||
while (true) {
|
||||
try {
|
||||
$file = openFile($this->fileName, 'x');
|
||||
$file = $this->filesystem->openFile($this->fileName, 'x');
|
||||
|
||||
// Return a lock object that can be used to release the lock on the mutex.
|
||||
$lock = new Lock($this->release(...));
|
||||
@ -46,12 +49,11 @@ final class FileMutex implements Mutex
|
||||
private function release(): void
|
||||
{
|
||||
try {
|
||||
deleteFile($this->fileName);
|
||||
$this->filesystem->deleteFile($this->fileName);
|
||||
} catch (\Throwable $exception) {
|
||||
throw new SyncException(
|
||||
'Failed to unlock the mutex file: ' . $this->fileName,
|
||||
0,
|
||||
$exception
|
||||
previous: $exception,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user