modify
This commit is contained in:
@@ -44,13 +44,11 @@ class OnPipeMessage extends Callback
|
|||||||
*/
|
*/
|
||||||
private function onCrontabWorker(array $message): string
|
private function onCrontabWorker(array $message): string
|
||||||
{
|
{
|
||||||
if (!isset($message['handler'])) {
|
if (empty($handler = $message['handler'] ?? null)) {
|
||||||
throw new Exception('unknown handler');
|
throw new Exception('unknown handler');
|
||||||
}
|
}
|
||||||
|
/** @var Crontab $handler */
|
||||||
/** @var Crontab $crontab */
|
$handler->increment()->execute();
|
||||||
$crontab = swoole_unserialize($message['handler']);
|
|
||||||
$crontab->increment()->execute();
|
|
||||||
return 'success';
|
return 'success';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+11
-10
@@ -184,16 +184,14 @@ abstract class Crontab extends BaseObject
|
|||||||
{
|
{
|
||||||
$redis = Snowflake::app()->getRedis();
|
$redis = Snowflake::app()->getRedis();
|
||||||
if (!$redis->exists('stop:crontab:' . $this->getName())) {
|
if (!$redis->exists('stop:crontab:' . $this->getName())) {
|
||||||
|
$redis->del('crontab:' . $this->getName());
|
||||||
$redis->del('stop:crontab:' . $this->getName());
|
$redis->del('stop:crontab:' . $this->getName());
|
||||||
return;
|
} else {
|
||||||
}
|
|
||||||
|
|
||||||
$redis->set('crontab:' . ($name = $this->getName()), swoole_serialize($this));
|
$redis->set('crontab:' . ($name = $this->getName()), swoole_serialize($this));
|
||||||
|
|
||||||
$tickTime = time() + $this->getTickTime();
|
$tickTime = time() + $this->getTickTime();
|
||||||
|
|
||||||
$redis->zAdd(Producer::CRONTAB_KEY, $tickTime, $name);
|
$redis->zAdd(Producer::CRONTAB_KEY, $tickTime, $name);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
abstract public function process(): mixed;
|
abstract public function process(): mixed;
|
||||||
@@ -217,21 +215,24 @@ abstract class Crontab extends BaseObject
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $application
|
* @param $application
|
||||||
* @throws Exception
|
* @return bool|int
|
||||||
|
* @throws \Exception
|
||||||
*/
|
*/
|
||||||
public function isRecover($application)
|
public function isRecover($application): bool|int
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
|
$redis = Snowflake::app()->getRedis();
|
||||||
if ($application->isExit()) {
|
if ($application->isExit()) {
|
||||||
return;
|
return $redis->del('crontab:' . $this->getName());
|
||||||
}
|
}
|
||||||
if ($application->isMaxExecute()) {
|
if ($application->isMaxExecute()) {
|
||||||
call_user_func([$application, 'max_execute'], ...$application->params);
|
call_user_func([$application, 'max_execute'], ...$application->params);
|
||||||
|
return $redis->del('crontab:' . $this->getName());
|
||||||
} else {
|
} else {
|
||||||
$application->recover();
|
return $application->recover();
|
||||||
}
|
}
|
||||||
} catch (\Throwable $throwable) {
|
} catch (\Throwable $throwable) {
|
||||||
logger()->addError($throwable, 'throwable');
|
return logger()->addError($throwable, 'throwable');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -35,7 +35,6 @@ class Producer extends Component
|
|||||||
}
|
}
|
||||||
|
|
||||||
$tickTime = time() + $crontab->getTickTime();
|
$tickTime = time() + $crontab->getTickTime();
|
||||||
|
|
||||||
$redis->del('stop:crontab:' . $name, 120);
|
$redis->del('stop:crontab:' . $name, 120);
|
||||||
|
|
||||||
$result = $redis->zAdd(self::CRONTAB_KEY, $tickTime, $name);
|
$result = $redis->zAdd(self::CRONTAB_KEY, $tickTime, $name);
|
||||||
|
|||||||
@@ -67,8 +67,8 @@ class Zookeeper extends Process
|
|||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$params['action'] = 'crontab';
|
$params['action'] = 'crontab';
|
||||||
$params['handler'] = $redis->get('crontab:' . $value);
|
$params['handler'] = swoole_unserialize($redis->get('crontab:' . $value));
|
||||||
$redis->del('crontab:' . $value);
|
|
||||||
$result = $server->sendMessage($params, $workerId = random_int(0, $setting - 1));
|
$result = $server->sendMessage($params, $workerId = random_int(0, $setting - 1));
|
||||||
|
|
||||||
var_dump('send crontab to ' . $workerId . ' ' . intval($result));
|
var_dump('send crontab to ' . $workerId . ' ' . intval($result));
|
||||||
|
|||||||
Reference in New Issue
Block a user