Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Fixes job batch serialization for PostgreSQL (#36061) #36081

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
35 changes: 33 additions & 2 deletions src/Illuminate/Bus/DatabaseBatchRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Closure;
use DateTimeInterface;
use Illuminate\Database\Connection;
use Illuminate\Database\PostgresConnection;
use Illuminate\Database\Query\Expression;
use Illuminate\Support\Str;

Expand Down Expand Up @@ -102,7 +103,7 @@ public function store(PendingBatch $batch)
'pending_jobs' => 0,
'failed_jobs' => 0,
'failed_job_ids' => '[]',
'options' => serialize($batch->options),
'options' => $this->serialize($batch->options),
'created_at' => time(),
'cancelled_at' => null,
'finished_at' => null,
Expand Down Expand Up @@ -283,10 +284,40 @@ protected function toBatch($batch)
(int) $batch->pending_jobs,
(int) $batch->failed_jobs,
json_decode($batch->failed_job_ids, true),
unserialize($batch->options),
$this->unserialize($batch->options),
CarbonImmutable::createFromTimestamp($batch->created_at),
$batch->cancelled_at ? CarbonImmutable::createFromTimestamp($batch->cancelled_at) : $batch->cancelled_at,
$batch->finished_at ? CarbonImmutable::createFromTimestamp($batch->finished_at) : $batch->finished_at
);
}

/**
* Serialize the given value.
*
* @param mixed $value
* @return string
*/
protected function serialize($value)
{
$serialized = serialize($value);

return $this->connection instanceof PostgresConnection
? base64_encode($serialized)
: $serialized;
}

/**
* Unserialize the given value.
*
* @param string $serialized
* @return mixed
*/
protected function unserialize($serialized)
{
if ($this->connection instanceof PostgresConnection && ! Str::contains($serialized, [':', ';'])) {
$serialized = base64_decode($serialized);
}

return unserialize($serialized);
}
}
67 changes: 67 additions & 0 deletions tests/Bus/BusBatchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Database\Capsule\Manager as DB;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\PostgresConnection;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\CallQueuedClosure;
use Mockery as m;
Expand Down Expand Up @@ -339,6 +340,72 @@ public function test_chain_can_be_added_to_batch()
$this->assertInstanceOf(CarbonImmutable::class, $batch->createdAt);
}

public function test_options_serialization_on_postgres()
{
$pendingBatch = (new PendingBatch(new Container, collect()))
->onQueue('test-queue');

$connection = m::spy(PostgresConnection::class);

$connection->shouldReceive('table')->andReturnSelf()
->shouldReceive('where')->andReturnSelf();

$repository = new DatabaseBatchRepository(
new BatchFactory(m::mock(Factory::class)), $connection, 'job_batches'
);

$repository->store($pendingBatch);

$connection->shouldHaveReceived('insert')
->withArgs(function ($argument) use ($pendingBatch) {
return unserialize(base64_decode($argument['options'])) === $pendingBatch->options;
});
}

/**
* @dataProvider serializedOptions
*/
public function test_options_unserialize_on_postgres($serialize, $options)
{
$factory = m::mock(BatchFactory::class);

$connection = m::spy(PostgresConnection::class);

$connection->shouldReceive('table->where->first')
->andReturn($m = (object) [
'id' => '',
'name' => '',
'total_jobs' => '',
'pending_jobs' => '',
'failed_jobs' => '',
'failed_job_ids' => '[]',
'options' => $serialize,
'created_at' => null,
'cancelled_at' => null,
'finished_at' => null,
]);

$batch = (new DatabaseBatchRepository($factory, $connection, 'job_batches'));

$factory->shouldReceive('make')
->withSomeOfArgs($batch, '', '', '', '', '', '', $options);

$batch->find(1);
}

/**
* @return array
*/
public function serializedOptions()
{
$options = [1, 2];

return [
[serialize($options), $options],
[base64_encode(serialize($options)), $options],
];
}

protected function createTestBatch($queue, $allowFailures = false)
{
$repository = new DatabaseBatchRepository(new BatchFactory($queue), DB::connection(), 'job_batches');
Expand Down