Skip to content

Commit 746fddf

Browse files
committed
fix(bg_jobs): store job argument as a text, increase length cap from 4000 to 32000
Signed-off-by: Maksim Sukharev <antreesy.web@gmail.com>
1 parent 44175e3 commit 746fddf

File tree

4 files changed

+64
-3
lines changed

4 files changed

+64
-3
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
7+
* SPDX-License-Identifier: AGPL-3.0-or-later
8+
*/
9+
10+
namespace OC\Core\Migrations;
11+
12+
use Closure;
13+
use Doctrine\DBAL\Types\Type;
14+
use OCP\DB\ISchemaWrapper;
15+
use OCP\DB\Types;
16+
use OCP\Migration\Attributes\ColumnType;
17+
use OCP\Migration\Attributes\ModifyColumn;
18+
use OCP\Migration\IOutput;
19+
use OCP\Migration\SimpleMigrationStep;
20+
use Override;
21+
22+
#[ModifyColumn(table: 'jobs', name: 'argument', type: ColumnType::TEXT, description: 'Migrate background job arguments to a text column')]
23+
class Version34000Date20260318095645 extends SimpleMigrationStep {
24+
#[Override]
25+
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
26+
/** @var ISchemaWrapper $schema */
27+
$schema = $schemaClosure();
28+
29+
if ($schema->hasTable('jobs')) {
30+
$table = $schema->getTable('jobs');
31+
$argumentColumn = $table->getColumn('argument');
32+
33+
if ($argumentColumn->getType() !== Type::getType(Types::TEXT)) {
34+
$argumentColumn->setType(Type::getType(Types::TEXT));
35+
return $schema;
36+
}
37+
}
38+
39+
return null;
40+
}
41+
}

lib/private/BackgroundJob/JobList.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
use function strlen;
2929

3030
class JobList implements IJobList {
31+
public const MAX_ARGUMENT_JSON_LENGTH = 32000;
32+
3133
/** @var array<string, string> */
3234
protected array $alreadyVisitedParallelBlocked = [];
3335

@@ -49,8 +51,8 @@ public function add(IJob|string $job, mixed $argument = null, ?int $firstCheck =
4951
$class = ($job instanceof IJob) ? get_class($job) : $job;
5052

5153
$argumentJson = json_encode($argument);
52-
if (strlen($argumentJson) > 4000) {
53-
throw new \InvalidArgumentException('Background job arguments can\'t exceed 4000 characters (json encoded)');
54+
if (strlen($argumentJson) > self::MAX_ARGUMENT_JSON_LENGTH) {
55+
throw new \InvalidArgumentException('Background job arguments can\'t exceed ' . self::MAX_ARGUMENT_JSON_LENGTH . ' characters (json encoded)');
5456
}
5557

5658
$query = $this->connection->getQueryBuilder();

tests/lib/BackgroundJob/JobListTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,24 @@ public function testAddRemove(mixed $argument): void {
9999
$this->assertEquals($existingJobs, $jobs);
100100
}
101101

102+
public function testAddAcceptsArgumentUnderMaxLength(): void {
103+
$argument = str_repeat('a', $this->instance::MAX_ARGUMENT_JSON_LENGTH - 100);
104+
$job = new TestJob();
105+
$this->assertFalse($this->instance->has($job, $argument));
106+
$this->instance->add($job, $argument);
107+
108+
$this->assertTrue($this->instance->has($job, $argument));
109+
}
110+
111+
public function testAddRejectsArgumentAboveMaxLength(): void {
112+
$argument = str_repeat('a', $this->instance::MAX_ARGUMENT_JSON_LENGTH + 100);
113+
114+
$this->expectException(\InvalidArgumentException::class);
115+
$this->expectExceptionMessage('Background job arguments can\'t exceed ' . $this->instance::MAX_ARGUMENT_JSON_LENGTH . ' characters (json encoded)');
116+
117+
$this->instance->add(new TestJob(), $argument);
118+
}
119+
102120
#[DataProvider('argumentProvider')]
103121
public function testRemoveDifferentArgument(mixed $argument): void {
104122
$existingJobs = $this->getAllSorted();

version.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// between betas, final and RCs. This is _not_ the public version number. Reset minor/patch level
1212
// when updating major/minor version number.
1313

14-
$OC_Version = [34, 0, 0, 0];
14+
$OC_Version = [34, 0, 0, 1];
1515

1616
// The human-readable string
1717
$OC_VersionString = '34.0.0 dev';

0 commit comments

Comments
 (0)