Skip to content

Commit

Permalink
FIX Don't write log buffer without db
Browse files Browse the repository at this point in the history
This can happen during unit test execution.
I've removed the manual flushing in d0acfad 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.
  • Loading branch information
chillu committed Jun 11, 2020
1 parent e2af84b commit 4d3368a
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/Services/QueuedJobHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Monolog\Handler\AbstractProcessingHandler;
use SilverStripe\Core\Injector\Injectable;
use SilverStripe\ORM\DB;
use Symbiote\QueuedJobs\DataObjects\QueuedJobDescriptor;

/**
Expand Down Expand Up @@ -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']);
};
Expand Down

0 comments on commit 4d3368a

Please sign in to comment.