From 4d3368a92bfaed090db19566c03d062721171805 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Fri, 12 Jun 2020 10:29:12 +1200 Subject: [PATCH] FIX Don't write log buffer without db This can happen during unit test execution. I've removed the manual flushing in https://github.com/symbiote/silverstripe-queuedjobs/pull/310/commits/d0acfad29e5147bf4a300b2b9be15e3bf5eb04f9 which caused this. But regardless of this change, there could be additional messages added to the logger after this manual flush which then result in the same situation. So the safest way is to check the log handler is actually in a state to write log messages. This will now "swallow" lowlevel errors such as "no database selected", but *only* for the buffer handler writing to the job database record. Since it bubbles by default, other handlers will still handle that error, e.g. outputting it to php://stdout. --- src/Services/QueuedJobHandler.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/Services/QueuedJobHandler.php b/src/Services/QueuedJobHandler.php index 316948e2..c8958cf9 100644 --- a/src/Services/QueuedJobHandler.php +++ b/src/Services/QueuedJobHandler.php @@ -4,6 +4,7 @@ use Monolog\Handler\AbstractProcessingHandler; use SilverStripe\Core\Injector\Injectable; +use SilverStripe\ORM\DB; use Symbiote\QueuedJobs\DataObjects\QueuedJobDescriptor; /** @@ -56,6 +57,12 @@ protected function write(array $record) public function handleBatch(array $records) { + // Guard against handlers auto-flushing on destruct. + // When this is part of a PHPUnit run, the connection to the database can have already been closed. + if (!DB::is_active()) { + return; + } + foreach ($records as $record) { $this->job->addMessage($record['message'], $record['level_name'], $record['datetime']); };