Skip to content

Commit

Permalink
Safe lock filename
Browse files Browse the repository at this point in the history
  • Loading branch information
Zarganwar authored Apr 9, 2021
1 parent c094e3a commit 57ffad8
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions src/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@

class Runner
{
/**
* @var string
*/
/** @var string */
private $lockPath;

public function __construct(string $lockPath)
Expand All @@ -21,7 +19,8 @@ public function runCallable(string $id, callable $callable)
throw new \RuntimeException(sprintf('Directory `%s` was not created', $this->lockPath));
}

$resource = fopen($this->lockPath . '/' . $id . '.lock', 'w+');
$lock = $this->lockPath . '/' . md5($id) . '.lock';
$resource = fopen($lock, 'w+');
if (!flock($resource, LOCK_EX | LOCK_NB)) { // acquire an exclusive lock
fclose($resource);
return false;
Expand All @@ -33,7 +32,7 @@ public function runCallable(string $id, callable $callable)
throw $e;
} finally {
flock($resource, LOCK_UN);
unlink($this->lockPath . '/' . $id . '.lock');
unlink($lock);
fclose($resource);
}

Expand All @@ -46,4 +45,4 @@ public function run(string $id, IProcess $process)
$process->run();
});
}
}
}

0 comments on commit 57ffad8

Please sign in to comment.