Skip to content

Commit d3ca488

Browse files
authored
Optimized the serialization of Hyperf\AsyncQueue\JobMessage (#6511)
1 parent 349d1f7 commit d3ca488

File tree

3 files changed

+52
-4
lines changed

3 files changed

+52
-4
lines changed

src/JobMessage.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,29 @@ public function __serialize(): array
2929
$this->job = $this->job->compress();
3030
}
3131

32-
return [$this->job, $this->attempts];
32+
return [
33+
'job' => $this->job,
34+
'attempts' => $this->attempts,
35+
];
3336
}
3437

3538
public function __unserialize(array $data): void
3639
{
37-
[$job, $attempts] = $data;
40+
if (array_is_list($data)) { // Compatible with old version, will be removed at v3.2
41+
$data = [
42+
'job' => $data[0],
43+
'attempts' => $data[1],
44+
];
45+
}
46+
47+
$job = $data['job'];
48+
3849
if ($job instanceof UnCompressInterface) {
3950
$job = $job->uncompress();
4051
}
4152

4253
$this->job = $job;
43-
$this->attempts = $attempts;
54+
$this->attempts = $data['attempts'];
4455
}
4556

4657
public function job(): JobInterface

tests/JobMessageTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,47 @@ public function testJobMessageSerialize()
3232
new DemoJob($id)
3333
);
3434

35+
$serialized = $message->__serialize();
36+
$this->assertArrayHasKey('job', $serialized);
37+
$this->assertArrayHasKey('attempts', $serialized);
38+
39+
$this->assertInstanceOf(MessageInterface::class, $message);
40+
$this->assertInstanceOf(JobInterface::class, $message->job());
41+
$this->assertInstanceOf(JobInterface::class, $message->job());
42+
$this->assertInstanceOf(DemoJob::class, $message->job());
43+
$this->assertSame($id, $message->job()->id);
44+
}
45+
46+
public function testJobMessageSerializeCompatible()
47+
{
48+
$id = rand(0, 9999);
49+
$message = new JobMessage(
50+
new DemoJob($id)
51+
);
52+
53+
$serialized = $message->__serialize();
54+
55+
$serialized = [
56+
'job' => $serialized['job'],
57+
'attempts' => 3,
58+
];
59+
$message->__unserialize($serialized);
60+
61+
$this->assertInstanceOf(MessageInterface::class, $message);
62+
$this->assertInstanceOf(JobInterface::class, $message->job());
63+
$this->assertInstanceOf(JobInterface::class, $message->job());
64+
$this->assertInstanceOf(DemoJob::class, $message->job());
65+
$this->assertSame($id, $message->job()->id);
66+
$this->assertSame(3, $message->getAttempts());
67+
68+
$serialized = [new DemoJob($id), 5];
69+
$message->__unserialize($serialized);
70+
3571
$this->assertInstanceOf(MessageInterface::class, $message);
3672
$this->assertInstanceOf(JobInterface::class, $message->job());
3773
$this->assertInstanceOf(JobInterface::class, $message->job());
3874
$this->assertInstanceOf(DemoJob::class, $message->job());
3975
$this->assertSame($id, $message->job()->id);
76+
$this->assertSame(5, $message->getAttempts());
4077
}
4178
}

tests/RedisDriverTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public function testAsyncQueueJobGenerate()
115115
$driver->push(new DemoJob($id, $model));
116116

117117
$serialized = (string) Context::get('test.async-queue.lpush.value');
118-
$this->assertSame(231, strlen($serialized));
118+
$this->assertSame(248, strlen($serialized));
119119

120120
/** @var JobMessage $class */
121121
$class = $packer->unpack($serialized);

0 commit comments

Comments
 (0)