]> _ Git - fluidbook-toolbox.git/commitdiff
wip #6711 @0.5
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 13 Feb 2024 17:53:21 +0000 (18:53 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 13 Feb 2024 17:53:21 +0000 (18:53 +0100)
app/Fluidbook/Packager/Packager.php

index 76b3f374c8a83937e00e207685fe1ed857226f29..ba9ae08d872090ae60aa06e1a249604d70c81b04 100644 (file)
@@ -10,6 +10,8 @@ use Cubist\Util\Files\Files;
 use Cubist\Util\PHP;
 use Cubist\Util\Text;
 use Exception;
+use Illuminate\Contracts\Cache\Lock;
+use Illuminate\Support\Facades\Cache;
 
 class Packager extends \App\Jobs\Base
 {
@@ -40,6 +42,11 @@ class Packager extends \App\Jobs\Base
 
     public $downloadExt = 'zip';
 
+    public $type = "unset";
+
+    /** @var Lock  */
+    protected $lock;
+
     /**
      * @var string
      */
@@ -108,11 +115,13 @@ class Packager extends \App\Jobs\Base
 
     public function __construct($book_id, $vdir = null, $options = [])
     {
-
         $this->_time = time();
         $this->_clean = (null === $vdir);
 
         $this->book_id = $book_id;
+
+        $this->lock = Cache::lock('fluidbook_package_' . $this->type . '_' . $this->book_id, 1800);
+
         $this->book = FluidbookPublication::find($book_id);
 
         $this->vdir = $vdir;
@@ -151,8 +160,14 @@ class Packager extends \App\Jobs\Base
 
     public function handle()
     {
-        PHP::neverStop();
-        $this->makePackage($this->zipPackage);
+        if ($this->lock->block(1800)) {
+            try {
+                PHP::neverStop();
+                $this->makePackage($this->zipPackage);
+            } finally {
+                $this->lock?->release();
+            }
+        }
     }
 
     protected function packager_path($path = '')
@@ -369,4 +384,13 @@ class Packager extends \App\Jobs\Base
     {
         $this->_finalURL = $finalURL;
     }
+
+    public function __destruct()
+    {
+        try {
+            $this->lock?->release();
+        } catch (\Exception $e) {
+
+        }
+    }
 }