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