Files
fresh/src/State/Lockfile.php
Christopher Vagnetoft e77e61d0b0 Bugfixes and improvements
* Implemented the --image option
* Implemented the --write-state option to go with --check
* Fixed bug in Lockfile class
2022-03-11 01:55:35 +01:00

36 lines
723 B
PHP

<?php
namespace NoccyLabs\FreshDocker\State;
class Lockfile
{
private string $filename;
private int $maxLock = 3600;
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);
}
public function release()
{
if (file_exists($this->filename)) {
unlink($this->filename);
}
}
}