]> _ Git - cubist_util.git/commitdiff
wip #7010 @0.5
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 17 Jul 2024 15:58:28 +0000 (17:58 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 17 Jul 2024 15:58:28 +0000 (17:58 +0200)
src/CommandLine.php

index 6bba3eb274ada8c0553573e25df0a02556976352..b64b43c2671ec4ba66d09c73ad6525ff7706269c 100644 (file)
@@ -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();