Refactoring and cleanup
* Moved logic from entrypoint to a dedicated class. * Disabled automatic flush of state. * Added locking support to prevent multiple instances. * Added logging * Added base interface for CredentialsLoader
This commit is contained in:
36
src/State/Lockfile.php
Normal file
36
src/State/Lockfile.php
Normal file
@ -0,0 +1,36 @@
|
||||
<?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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
Reference in New Issue
Block a user