fresh/src/State/Lockfile.php
Christopher Vagnetoft 88f3b75383 Bugfixes
* Fixed a bug where the lockfile would be released on exit even if
  it wasn't locked by the process
* Disabled the phpx build as it is broken
2022-07-05 22:49:26 +02:00

40 lines
836 B
PHP

<?php
namespace NoccyLabs\FreshDocker\State;
class Lockfile
{
private string $filename;
private int $maxLock = 3600;
private bool $locked = false;
public function __construct(string $filename)
{
$this->filename = $filename;
register_shutdown_function([$this,"release"]);
}
public function lock()
{
if (file_exists($this->filename)) {
if (time() - filemtime($this->filename) < $this->maxLock) {
throw new \RuntimeException("Lockfile {$this->filename} already exists");
}
}
touch($this->filename);
$this->locked = true;
}
public function release()
{
if ($this->locked && file_exists($this->filename)) {
unlink($this->filename);
}
$this->locked = false;
}
}