Fixed bug that unable to serialize Channel in Crontab. (#5578)

This commit is contained in:
张城铭 2023-03-28 15:47:15 +08:00 committed by GitHub
parent df3c2eaff0
commit f60b720e2b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 71 additions and 0 deletions

View File

@ -8,6 +8,10 @@
- [#5572](https://github.com/hyperf/hyperf/pull/5572) Update Http Server to use new WritableConnection implementation.
## Fixed
- [#5578](https://github.com/hyperf/hyperf/pull/5578) Fixed bug that unable to serialize `Channel` in `Crontab`.
# v3.0.13 - 2023-03-26
## Added

View File

@ -45,6 +45,39 @@ class Crontab
$this->running = new Channel(1);
}
public function __serialize(): array
{
return [
"\x00*\x00name" => $this->name,
"\x00*\x00type" => $this->type,
"\x00*\x00rule" => $this->rule,
"\x00*\x00singleton" => $this->singleton,
"\x00*\x00mutexPool" => $this->mutexPool,
"\x00*\x00mutexExpires" => $this->mutexExpires,
"\x00*\x00onOneServer" => $this->onOneServer,
"\x00*\x00callback" => $this->callback,
"\x00*\x00memo" => $this->memo,
"\x00*\x00executeTime" => $this->executeTime,
"\x00*\x00enable" => $this->enable,
];
}
public function __unserialize(array $data): void
{
$this->name = $data["\x00*\x00name"] ?? $this->name;
$this->type = $data["\x00*\x00type"] ?? $this->type;
$this->rule = $data["\x00*\x00rule"] ?? $this->rule;
$this->singleton = $data["\x00*\x00singleton"] ?? $this->singleton;
$this->mutexPool = $data["\x00*\x00mutexPool"] ?? $this->mutexPool;
$this->mutexExpires = $data["\x00*\x00mutexExpires"] ?? $this->mutexExpires;
$this->onOneServer = $data["\x00*\x00onOneServer"] ?? $this->onOneServer;
$this->callback = $data["\x00*\x00callback"] ?? $this->callback;
$this->memo = $data["\x00*\x00memo"] ?? $this->memo;
$this->executeTime = $data["\x00*\x00executeTime"] ?? $this->executeTime;
$this->enable = $data["\x00*\x00enable"] ?? $this->enable;
$this->running = new Channel(1);
}
public function getName(): ?string
{
return $this->name;

View File

@ -0,0 +1,34 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace HyperfTest\Crontab;
use Hyperf\Crontab\Crontab;
use PHPUnit\Framework\TestCase;
/**
* @internal
* @coversNothing
*/
class CrontabTest extends TestCase
{
public function testCrontab()
{
$crontab = clone ((new Crontab())->setName('test')->setRule('* * * * *')->setMemo('test')->setSingleton(true)->setMutexPool('default')->setOnOneServer(true)->setEnable(false));
$serialized = "O:22:\"Hyperf\\Crontab\\Crontab\":11:{s:7:\"\x00*\x00name\";s:4:\"test\";s:7:\"\x00*\x00type\";s:8:\"callback\";s:7:\"\x00*\x00rule\";s:9:\"* * * * *\";s:12:\"\x00*\x00singleton\";b:1;s:12:\"\x00*\x00mutexPool\";s:7:\"default\";s:15:\"\x00*\x00mutexExpires\";i:3600;s:14:\"\x00*\x00onOneServer\";b:1;s:11:\"\x00*\x00callback\";N;s:7:\"\x00*\x00memo\";s:4:\"test\";s:14:\"\x00*\x00executeTime\";N;s:9:\"\x00*\x00enable\";b:0;}";
$this->assertEquals($serialized, serialize($crontab));
$unserializeCrontab = unserialize($serialized);
$this->assertEquals($crontab, $unserializeCrontab);
}
}