]> _ Git - fluidbook-toolbox.git/commitdiff
fix #6066 @0.:10
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 18 Jul 2023 07:52:25 +0000 (09:52 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 18 Jul 2023 07:52:25 +0000 (09:52 +0200)
app/Jobs/FluidbookImagesPreprocess.php

index ca1e8f9e721ef021df2ded5f9fb51ff7f62195dd..bec2c6fbdc5c91fdd8bca9888041bdaf36e1117d 100644 (file)
@@ -5,6 +5,7 @@ namespace App\Jobs;
 use App\Fluidbook\Compiler\Compiler;
 use App\Models\FluidbookPublication;
 use Cubist\Util\ArrayUtil;
+use Illuminate\Support\Facades\Log;
 
 class FluidbookImagesPreprocess extends Base
 {
@@ -19,9 +20,17 @@ class FluidbookImagesPreprocess extends Base
      */
     protected $jobs = [];
 
-    public function __construct($book_id)
+    protected $_currentState = '';
+    protected $_stateTime = null;
+
+    protected $_sync = false;
+    protected $_hangMaxTime;
+
+    public function __construct($book_id, $sync = false, $hangMaxTime = 10)
     {
         $this->book_id = $book_id;
+        $this->_sync = $sync;
+        $this->_hangMaxTime = $hangMaxTime;
         parent::__construct();
     }
 
@@ -81,15 +90,49 @@ class FluidbookImagesPreprocess extends Base
     {
         $nbjobs = count($this->jobs);
         $done = 0;
+        $error = 0;
+
         foreach ($this->jobs as $job) {
             if ($job->isDone()) {
                 $done++;
+            } else if ($job->isError()) {
+                $error++;
+            }
+        }
+        $finished = $done + $error;
+
+        $state = $finished . '(--' . $error . ') /' . $nbjobs;
+        if ($this->_currentState != $state) {
+            $this->_currentState = $state;
+            $this->_stateTime = time();
+        }
+
+        $missing = ($nbjobs - $finished);
+        $difftime = (time() - $this->_stateTime);
+        if ($missing > 0 && $difftime > $this->_hangMaxTime) {
+            Log::error('Fluidbook compilation failed after ' . $this->_hangMaxTime . 's with ' . $missing . ' jobs remaining (on ' . $nbjobs . ' jobs launched)');
+            foreach ($this->jobs as $job) {
+                if ($job->isDone()) {
+                    continue;
+                }
+                if ($job->isError()) {
+                    Log::error('This file failed ' . $job->getPath());
+                }
+                Log::error('This file is stuck ' . $job->getPath());
             }
+            return true;
         }
-        if (rand(1, 10) === 5) {
-            echo $done . '/' . $nbjobs . "\n";
+
+        if ($missing <= 0 && $error > 0) {
+            Log::error('Fluidbook compilation failed with some errors');
+            foreach ($this->jobs as $job) {
+                $job->isOK(true);
+                if ($job->isError()) {
+                    Log::error('This file failed ' . $job->getPath() . " - " . $job->getLog());
+                }
+            }
         }
-        return $done === $nbjobs;
+        return $missing <= 0;
     }
 
     protected function getFile($page, $format = 'jpg', $resolution = 150, $withText = true, $withGraphics = true, $version = 'html')
@@ -102,8 +145,10 @@ class FluidbookImagesPreprocess extends Base
         if ($job->isDone()) {
             return;
         }
-        echo $job->getPath() . "\n";
-        dispatch($job);
-
+        if ($this->_sync) {
+            $job->handle();
+        } else {
+            dispatch($job);
+        }
     }
 }