diff --git a/src/Services/QueuedJobService.php b/src/Services/QueuedJobService.php index a112be6b..5e72fef7 100644 --- a/src/Services/QueuedJobService.php +++ b/src/Services/QueuedJobService.php @@ -882,11 +882,6 @@ public function runJob($jobId) $this->extend('updateJobDescriptorAndJobOnException', $jobDescriptor, $job, $e); } - // Write any remaining batched messages at the end - if (isset($bufferHandler)) { - $bufferHandler->flush(); - } - ob_end_flush(); // now check the job state @@ -964,6 +959,24 @@ public function runJob($jobId) $this->handleBrokenJobException($jobDescriptor, $job, $e); $broken = true; } + + // Write any remaining batched messages at the end. + if ($logger instanceof Logger) { + foreach ($logger->getHandlers() as $handler) { + if ($handler instanceof BufferHandler) { + $handler->flush(); + } + } + } + + // If using a global singleton logger here, + // any messages added after this point will be auto-flushed on PHP shutdown through the handler. + // This causes a database write, and assumes the database and table will be available at this point. + if ($logger instanceof Logger) { + $logger->setHandlers(array_filter($logger->getHandlers(), function ($handler) { + return !($handler instanceof BufferHandler); + })); + } }); $this->unsetRunAsUser($runAsUser, $originalUser);