Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/Command/WorkerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,7 @@ public function execute(Arguments $args, ConsoleIo $io) {
return $this->clean($io, (bool)$args->getOption('force'));
}

/** @phpstan-ignore-next-line */
return $this->$action($io, $pid);
return (int)$this->$action($io, $pid);
}

/**
Expand Down
17 changes: 17 additions & 0 deletions src/Model/Table/QueuedJobsTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@ class QueuedJobsTable extends Table {
*/
public const DAY = 86400;

/**
* Maximum byte length persisted to the `failure_message` / `output` TEXT
* columns. A larger value is byte-truncated before save so recording a big
* failure (e.g. a database error that echoes a huge query) can never overflow
* the column and crash the worker while it is recording the failure.
*
* @var int
*/
public const FAILURE_MESSAGE_MAX_LENGTH = 65535;

/**
* Terminal `status` value stamped on a job that has exhausted all of its
* retries. Distinguishes a permanently-failed job (which will never run
Expand Down Expand Up @@ -768,6 +778,13 @@ public function markJobDone(QueuedJob $job, ?string $output = null): bool {
* @return bool Success
*/
public function markJobFailed(QueuedJob $job, ?string $failureMessage = null, ?string $output = null): bool {
if ($failureMessage !== null) {
$failureMessage = mb_strcut($failureMessage, 0, static::FAILURE_MESSAGE_MAX_LENGTH);
}
if ($output !== null) {
$output = mb_strcut($output, 0, static::FAILURE_MESSAGE_MAX_LENGTH);
}

$fields = [
'failure_message' => $failureMessage,
'memory' => Memory::usage(),
Expand Down
17 changes: 17 additions & 0 deletions tests/TestCase/Model/Table/QueuedJobsTableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,23 @@ public function testMarkJobFailed() {
$this->assertTrue($this->QueuedJobs->markJobFailed($job));
}

/**
* A failure message larger than the TEXT column must be truncated instead of
* overflowing the column and throwing while recording the failure.
*
* @return void
*/
public function testMarkJobFailedTruncatesOversizedMessage() {
$job = $this->QueuedJobs->createJob('Foo', ['test' => 'data']);
$oversized = str_repeat('x', QueuedJobsTable::FAILURE_MESSAGE_MAX_LENGTH + 5000);

$this->assertTrue($this->QueuedJobs->markJobFailed($job, $oversized));

$stored = $this->QueuedJobs->get($job->id);
$this->assertNotNull($stored->failure_message);
$this->assertLessThanOrEqual(QueuedJobsTable::FAILURE_MESSAGE_MAX_LENGTH, strlen((string)$stored->failure_message));
}

/**
* @return void
*/
Expand Down
Loading