Skip to content

Commit 798f9af

Browse files
authored
Auto-detect captureOutput based on output column existence (#459)
* Auto-detect captureOutput based on output column existence Instead of requiring explicit `Queue.captureOutput` configuration, the Processor now auto-detects whether to capture output by checking if the `output` column exists on the `queued_jobs` table. Explicit config still takes precedence. The result is cached per worker lifetime so the schema check only happens once. * Fix CI: handle mock table schema in captureOutput() and suppress deprecation in test Wrap getSchema()->hasColumn() in try/catch to handle mock tables in tests (and any edge case in production). Suppress E_USER_DEPRECATED in InfoCommandTest that deliberately uses old config keys.
1 parent b8b9e5d commit 798f9af

2 files changed

Lines changed: 38 additions & 3 deletions

File tree

src/Queue/Processor.php

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ class Processor {
8686
*/
8787
protected ?QueuedJob $currentJob = null;
8888

89+
/**
90+
* @var bool|null
91+
*/
92+
protected ?bool $captureOutput = null;
93+
8994
/**
9095
* @param \Queue\Console\Io $io
9196
* @param \Psr\Log\LoggerInterface $logger
@@ -225,7 +230,7 @@ protected function runJob(QueuedJob $queuedJob, string $pid): void {
225230
]);
226231
EventManager::instance()->dispatch($event);
227232

228-
$captureOutput = (bool)Configure::read('Queue.captureOutput');
233+
$captureOutput = $this->captureOutput();
229234
if ($captureOutput) {
230235
$maxOutputSize = (int)(Configure::read('Queue.maxOutputSize') ?: 65536);
231236
$this->io->enableOutputCapture($maxOutputSize);
@@ -361,6 +366,30 @@ protected function getTaskConf(): array {
361366
return $this->taskConf;
362367
}
363368

369+
/**
370+
* Whether to capture task output into the DB.
371+
*
372+
* Auto-detects based on `output` column existence if not explicitly configured.
373+
*
374+
* @return bool
375+
*/
376+
protected function captureOutput(): bool {
377+
if ($this->captureOutput === null) {
378+
$configured = Configure::read('Queue.captureOutput');
379+
if ($configured !== null) {
380+
$this->captureOutput = (bool)$configured;
381+
} else {
382+
try {
383+
$this->captureOutput = $this->QueuedJobs->getSchema()->hasColumn('output');
384+
} catch (Throwable) {
385+
$this->captureOutput = false;
386+
}
387+
}
388+
}
389+
390+
return $this->captureOutput;
391+
}
392+
364393
/**
365394
* Signal handling to queue worker for clean shutdown
366395
*
@@ -372,7 +401,7 @@ protected function exit(int $signal): void {
372401
if ($this->currentJob) {
373402
$failureMessage = 'Worker process terminated by signal (SIGTERM) - job execution interrupted due to timeout or manual termination';
374403
$capturedOutput = null;
375-
if ((bool)Configure::read('Queue.captureOutput')) {
404+
if ($this->captureOutput()) {
376405
$maxOutputSize = (int)(Configure::read('Queue.maxOutputSize') ?: 65536);
377406
$capturedOutput = $this->io->getOutputAsText($maxOutputSize);
378407
$this->io->disableOutputCapture();
@@ -395,7 +424,7 @@ protected function abort(int $signal = 1): void {
395424
if ($this->currentJob) {
396425
$failureMessage = 'Worker process aborted by signal (' . $signal . ') - job execution interrupted';
397426
$capturedOutput = null;
398-
if ((bool)Configure::read('Queue.captureOutput')) {
427+
if ($this->captureOutput()) {
399428
$maxOutputSize = (int)(Configure::read('Queue.maxOutputSize') ?: 65536);
400429
$capturedOutput = $this->io->getOutputAsText($maxOutputSize);
401430
$this->io->disableOutputCapture();

tests/TestCase/Command/InfoCommandTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,14 @@ public function testExecuteWithNewConfigNames(): void {
5454
Configure::write('Queue.defaultworkerretries', 2);
5555
Configure::write('Queue.workertimeout', 120);
5656

57+
// Suppress deprecation warnings triggered by Config methods reading old keys
58+
$errorLevel = error_reporting();
59+
error_reporting($errorLevel & ~E_USER_DEPRECATED);
60+
5761
$this->exec('queue info');
5862

63+
error_reporting($errorLevel);
64+
5965
$output = $this->_out->output();
6066

6167
// Check that new config names are displayed with old names noted

0 commit comments

Comments
 (0)