modify
This commit is contained in:
@@ -48,25 +48,7 @@ class OnPipeMessage extends Callback
|
||||
if (empty($message['handler'] ?? null)) {
|
||||
throw new Exception('unknown handler');
|
||||
}
|
||||
/** @var Crontab $handler */
|
||||
$handler = $message['handler'];
|
||||
defer(function () use ($handler) {
|
||||
if ($handler->isRecover() !== 999) {
|
||||
return;
|
||||
}
|
||||
$redis = Snowflake::app()->getRedis();
|
||||
|
||||
$name = $handler->getName();
|
||||
if (!$redis->exists('stop:crontab:' . $name)) {
|
||||
$redis->set('crontab:' . $name, swoole_serialize($handler));
|
||||
$tickTime = time() + $handler->getTickTime();
|
||||
$redis->zAdd(Producer::CRONTAB_KEY, $tickTime, $name);
|
||||
} else {
|
||||
$redis->del('crontab:' . $name);
|
||||
$redis->del('stop:crontab:' . $name);
|
||||
}
|
||||
});
|
||||
$handler->increment()->execute();
|
||||
$message['handler']->increment()->execute();
|
||||
return 'success';
|
||||
}
|
||||
|
||||
|
||||
+36
-28
@@ -189,10 +189,42 @@ abstract class Crontab extends BaseObject
|
||||
*/
|
||||
public function execute(): void
|
||||
{
|
||||
defer(function () {
|
||||
$this->isRecover();
|
||||
});
|
||||
$this->run();
|
||||
defer(fn() => $this->afterExecute());
|
||||
try {
|
||||
$redis = Snowflake::app()->getRedis();
|
||||
|
||||
$name_md5 = $this->getName();
|
||||
|
||||
$redis->hSet(self::WAIT_END, $name_md5, static::getSerialize($this));
|
||||
|
||||
$params = call_user_func([$this, 'process'], ...$this->params);
|
||||
$redis->hDel(self::WAIT_END, $name_md5);
|
||||
if ($params === null) {
|
||||
return;
|
||||
}
|
||||
write(Json::encode(['name' => $this->name, 'response' => serialize($params)]), 'crontab');
|
||||
} catch (\Throwable $throwable) {
|
||||
logger()->addError($throwable, 'throwable');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function afterExecute()
|
||||
{
|
||||
if ($this->isRecover() !== 999) {
|
||||
return;
|
||||
}
|
||||
$redis = Snowflake::app()->getRedis();
|
||||
|
||||
$name = $this->getName();
|
||||
if (!$redis->exists('stop:crontab:' . $name)) {
|
||||
$redis->set('crontab:' . $name, swoole_serialize($this));
|
||||
$tickTime = time() + $this->getTickTime();
|
||||
$redis->zAdd(Producer::CRONTAB_KEY, $tickTime, $name);
|
||||
} else {
|
||||
$redis->del('crontab:' . $name);
|
||||
$redis->del('stop:crontab:' . $name);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -233,30 +265,6 @@ abstract class Crontab extends BaseObject
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
private function run()
|
||||
{
|
||||
try {
|
||||
$redis = Snowflake::app()->getRedis();
|
||||
|
||||
$name_md5 = $this->getName();
|
||||
|
||||
$redis->hSet(self::WAIT_END, $name_md5, static::getSerialize($this));
|
||||
|
||||
$params = call_user_func([$this, 'process'], ...$this->params);
|
||||
$redis->hDel(self::WAIT_END, $name_md5);
|
||||
if ($params === null) {
|
||||
return;
|
||||
}
|
||||
write(Json::encode(['name' => $this->name, 'response' => serialize($params)]), 'crontab');
|
||||
} catch (\Throwable $throwable) {
|
||||
logger()->addError($throwable, 'throwable');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user