From da63a33c5c5394dbe0e04269e101c462560d71de Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Tue, 18 Jul 2023 09:52:25 +0200 Subject: [PATCH] fix #6066 @0.:10 --- app/Jobs/FluidbookImagesPreprocess.php | 59 +++++++++++++++++++++++--- 1 file changed, 52 insertions(+), 7 deletions(-) diff --git a/app/Jobs/FluidbookImagesPreprocess.php b/app/Jobs/FluidbookImagesPreprocess.php index ca1e8f9e7..bec2c6fbd 100644 --- a/app/Jobs/FluidbookImagesPreprocess.php +++ b/app/Jobs/FluidbookImagesPreprocess.php @@ -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); + } } } -- 2.39.5