This commit is contained in:
2021-03-24 17:57:08 +08:00
parent 2610267aba
commit 6df0cb4003
11 changed files with 576 additions and 532 deletions
+10 -6
View File
@@ -101,12 +101,11 @@ class OnTask extends Callback
*/ */
private function runTaskHandler($data): ?array private function runTaskHandler($data): ?array
{ {
Coroutine\defer(function () {
fire(Event::SYSTEM_RESOURCE_RELEASES);
logger()->insert();
});
try { try {
$serialize = $this->before($data); $serialize = $this->before($data);
if ($serialize === null) {
throw new Exception('unpack error.');
}
$params = $serialize->getParams(); $params = $serialize->getParams();
if (is_object($params)) { if (is_object($params)) {
$params = get_object_vars($params); $params = get_object_vars($params);
@@ -115,12 +114,17 @@ class OnTask extends Callback
$finish['params'] = $params; $finish['params'] = $params;
$finish['status'] = 'success'; $finish['status'] = 'success';
$finish['info'] = $serialize->onHandler(); $finish['info'] = $serialize->onHandler();
return $finish;
} catch (\Throwable $exception) { } catch (\Throwable $exception) {
$finish['status'] = 'error'; $finish['status'] = 'error';
$finish['info'] = $this->format($exception); $finish['info'] = $this->format($exception);
$this->error($exception, 'Task'); $this->error($exception, 'Task');
}
return $finish; return $finish;
} finally {
fire(Event::SYSTEM_RESOURCE_RELEASES);
logger()->insert();
}
} }
@@ -130,7 +134,7 @@ class OnTask extends Callback
*/ */
protected function before($data): ?Task protected function before($data): ?Task
{ {
if (empty($serialize = unserialize($data))) { if (empty($serialize = swoole_unserialize($data))) {
return null; return null;
} }
if (!($serialize instanceof ITask)) { if (!($serialize instanceof ITask)) {
+1 -1
View File
@@ -105,7 +105,7 @@ class Producer extends Component
} }
$groupId = $consumers['groupId']; $groupId = $consumers['groupId'];
} }
$this->setGroupId($groupId)->setTopic($topic)->delivery(Json::encode($params)); $this->setGroupId($groupId)->setTopic($topic)->delivery(swoole_serialize($params));
} }
+2
View File
@@ -24,6 +24,8 @@ class Struct
*/ */
public function __construct($topic, Message $message) public function __construct($topic, Message $message)
{ {
$message->payload = swoole_unserialize($message->payload);
$this->topic = $topic; $this->topic = $topic;
$this->offset = $message->offset; $this->offset = $message->offset;
$this->part = $message->partition; $this->part = $message->partition;
+1 -1
View File
@@ -31,7 +31,7 @@ class Crontab extends Component
$name = md5($crontab->getName()); $name = md5($crontab->getName());
$redis->set('crontab:' . $name, serialize($crontab)); $redis->set('crontab:' . $name, swoole_serialize($crontab));
$tickTime = time() + $crontab->getTickTime(); $tickTime = time() + $crontab->getTickTime();
+1 -1
View File
@@ -52,7 +52,7 @@ class Async extends Component
$randWorkerId = random_int(0, $server->setting['task_worker_num'] - 1); $randWorkerId = random_int(0, $server->setting['task_worker_num'] - 1);
$server->task(serialize($class), $randWorkerId); $server->task(swoole_serialize($class), $randWorkerId);
} }
} }
+2 -2
View File
@@ -32,7 +32,7 @@ class File extends Component implements ICache
public function set($key, $val): string|int public function set($key, $val): string|int
{ {
if (is_array($val) || is_object($val)) { if (is_array($val) || is_object($val)) {
$val = serialize($val); $val = swoole_serialize($val);
} }
$tmpFile = $this->getCacheKey($key); $tmpFile = $this->getCacheKey($key);
if (!$this->exists($tmpFile)) { if (!$this->exists($tmpFile)) {
@@ -128,7 +128,7 @@ class File extends Component implements ICache
return false; return false;
} }
$content = file_get_contents($tmpFile); $content = file_get_contents($tmpFile);
return unserialize($content); return swoole_unserialize($content);
} }
/** /**
+1 -1
View File
@@ -105,7 +105,7 @@ class Str
*/ */
public static function isSerialize($data, $callBack = null): bool public static function isSerialize($data, $callBack = null): bool
{ {
$false = !empty($data) && unserialize($data) !== false; $false = !empty($data) && swoole_unserialize($data) !== false;
if ($false && is_callable($callBack, true)) { if ($false && is_callable($callBack, true)) {
return call_user_func($callBack, $data); return call_user_func($callBack, $data);
} }
+2 -2
View File
@@ -58,7 +58,7 @@ class CrontabProcess extends Process
foreach ($range as $value) { foreach ($range as $value) {
$crontab = $redis->get('crontab:' . md5($value)); $crontab = $redis->get('crontab:' . md5($value));
$redis->del('crontab:' . md5($value)); $redis->del('crontab:' . md5($value));
if (empty($crontab) || !($crontab = unserialize($crontab))) { if (empty($crontab) || !($crontab = swoole_unserialize($crontab))) {
continue; continue;
} }
Coroutine::create(function (Crontab $value, int $startTime) { Coroutine::create(function (Crontab $value, int $startTime) {
@@ -114,7 +114,7 @@ class CrontabProcess extends Process
$name = md5($crontab->getName()); $name = md5($crontab->getName());
$redis->set('crontab:' . $name, serialize($crontab)); $redis->set('crontab:' . $name, swoole_serialize($crontab));
$tickTime = time() + $crontab->getTickTime(); $tickTime = time() + $crontab->getTickTime();
+1 -1
View File
@@ -316,7 +316,7 @@ class Snowflake
$class = static::createObject($class); $class = static::createObject($class);
$class->setParams($params); $class->setParams($params);
$server->task(serialize($class)); $server->task(swoole_serialize($class));
} }
+1 -1
View File
@@ -87,7 +87,7 @@ class TypesOfValidator extends BaseValidator
if (!is_string($value) || is_numeric($value)) { if (!is_string($value) || is_numeric($value)) {
return $this->addError('The ' . $this->field . ' not is serialize data.'); return $this->addError('The ' . $this->field . ' not is serialize data.');
} }
if (false === unserialize($value)) { if (false === swoole_unserialize($value)) {
return $this->addError('The ' . $this->field . ' not is serialize data.'); return $this->addError('The ' . $this->field . ' not is serialize data.');
} }
return true; return true;
+38
View File
@@ -662,6 +662,44 @@ if (!function_exists('sweep')) {
} }
if (!function_exists('swoole_serialize')) {
/**
* @param $data
* @return string
*/
function swoole_serialize($data): string
{
if (class_exists('swoole_serialize')) {
return \swoole_serialize::pack($data);
} else {
return serialize($data);
}
}
}
if (!function_exists('swoole_unserialize')) {
/**
* @param $data
* @return string
*/
function swoole_unserialize($data): string
{
if (class_exists('swoole_serialize')) {
return \swoole_serialize::unpack($data);
} else {
return unserialize($data);
}
}
}
if (!function_exists('merge')) { if (!function_exists('merge')) {