use App\Fluidbook\Compiler\Compiler;
use App\Models\FluidbookPublication;
use Cubist\Util\ArrayUtil;
+use Illuminate\Support\Facades\Log;
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();
}
{
$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')
if ($job->isDone()) {
return;
}
- echo $job->getPath() . "\n";
- dispatch($job);
-
+ if ($this->_sync) {
+ $job->handle();
+ } else {
+ dispatch($job);
+ }
}
}