From ce853adc75da35219034ed07111ed5c6ac5e69e0 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Wed, 17 Jul 2024 17:58:28 +0200 Subject: [PATCH] wip #7010 @0.5 --- src/CommandLine.php | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/CommandLine.php b/src/CommandLine.php index 6bba3eb..b64b43c 100644 --- a/src/CommandLine.php +++ b/src/CommandLine.php @@ -24,6 +24,8 @@ class CommandLine protected $longArgSeparator = '='; protected $timeout = 0; protected $sudo = false; + protected $unique = false; + protected $uniqueCommand = null; function __construct($program, $output = null, $error = true, $outputPrefix = null) { @@ -172,6 +174,17 @@ class CommandLine } $endTime = microtime(true); $this->execTime = $endTime - $startTime; + $this->_releaseUniqueLockFile(); + } + + protected function _releaseUniqueLockFile() + { + if ($this->isUnique()) { + $lock = $this->getUniqueLockFile(); + if (file_exists($lock)) { + unlink($lock); + } + } } public function __set($varname, $value) @@ -181,6 +194,7 @@ class CommandLine public function __destruct() { + $this->_releaseUniqueLockFile(); // if ($this->temp_output && file_exists($this->output)) { // unlink($this->output); // } @@ -249,6 +263,15 @@ class CommandLine $this->commande = implode(';', $commandes); + if ($this->isUnique()) { + $this->uniqueCommand = $this->commande; + $lockFile = $this->getUniqueLockFile(); + while (file_exists($lockFile) && filemtime($lockFile) > time() - 3600) { + sleep(3); + } + touch($lockFile); + } + if ($output) { $this->commande .= ' > ' . $output; } @@ -260,6 +283,11 @@ class CommandLine } } + protected function getUniqueLockFile() + { + return sys_get_temp_dir() . '/' . hash("sha256", $this->uniqueCommand) . '.lock'; + } + public static function getArgs($argv) { array_shift($argv); @@ -364,6 +392,22 @@ class CommandLine $this->sudo = $sudo; } + /** + * @return bool + */ + public function isUnique(): bool + { + return $this->unique; + } + + /** + * @param bool $unique + */ + public function setUnique(bool $unique): void + { + $this->unique = $unique; + } + public function ddCommand() { $this->_preExecute(); -- 2.39.5