modify
This commit is contained in:
+12
-10
@@ -41,6 +41,16 @@ class Crontab extends BaseObject
|
|||||||
|
|
||||||
private int $execute_number = 0;
|
private int $execute_number = 0;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function increment(): static
|
||||||
|
{
|
||||||
|
$this->execute_number += 1;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array|Closure
|
* @return array|Closure
|
||||||
*/
|
*/
|
||||||
@@ -70,7 +80,7 @@ class Crontab extends BaseObject
|
|||||||
*/
|
*/
|
||||||
public function getTickTime(): int
|
public function getTickTime(): int
|
||||||
{
|
{
|
||||||
return $this->tickTime;
|
return $this->tickTime * 1000;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -186,18 +196,10 @@ class Crontab extends BaseObject
|
|||||||
/**
|
/**
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function execute(CrontabProcess $process): void
|
public function execute(): void
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$this->warning('execute crontab.');
|
|
||||||
|
|
||||||
call_user_func($this->handler, $this->params);
|
call_user_func($this->handler, $this->params);
|
||||||
$this->execute_number += 1;
|
|
||||||
if ($this->execute_number >= $this->max_execute_number) {
|
|
||||||
$process->clear($this->getName());
|
|
||||||
} else if (!$this->isLoop()) {
|
|
||||||
$process->clear($this->getName());
|
|
||||||
}
|
|
||||||
} catch (\Throwable $throwable) {
|
} catch (\Throwable $throwable) {
|
||||||
$this->addError($throwable->getMessage());
|
$this->addError($throwable->getMessage());
|
||||||
} finally {
|
} finally {
|
||||||
|
|||||||
@@ -26,11 +26,57 @@ class CrontabProcess extends Process
|
|||||||
public array $names = [];
|
public array $names = [];
|
||||||
|
|
||||||
|
|
||||||
|
public array $scores = [];
|
||||||
|
public array $timers = [];
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param \Swoole\Process $process
|
* @param \Swoole\Process $process
|
||||||
*/
|
*/
|
||||||
public function onHandler(\Swoole\Process $process): void
|
public function onHandler(\Swoole\Process $process): void
|
||||||
{
|
{
|
||||||
|
$this->readBySocket($process);
|
||||||
|
Timer::tick(1000, function () {
|
||||||
|
$startTime = time();
|
||||||
|
|
||||||
|
$time = $this->scores[$startTime];
|
||||||
|
unset($this->scores[$startTime]);
|
||||||
|
foreach ($time as $value) {
|
||||||
|
Coroutine::create(function (Crontab $value, int $startTime) {
|
||||||
|
$this->dispatch($value, $startTime);
|
||||||
|
}, $value, $startTime);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Crontab $value
|
||||||
|
* @param int $startTime
|
||||||
|
* @throws \Exception
|
||||||
|
*/
|
||||||
|
private function dispatch(Crontab $value, int $startTime)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$value->increment()->execute();
|
||||||
|
if ($value->getExecuteNumber() < $value->getMaxExecuteNumber()) {
|
||||||
|
$this->addTask($value, $startTime + $value->getTickTime());
|
||||||
|
} else if ($value->isLoop()) {
|
||||||
|
$this->addTask($value, $startTime + $value->getTickTime());
|
||||||
|
}
|
||||||
|
} catch (\Throwable $exception) {
|
||||||
|
$this->application->error($exception->getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param \Swoole\Process $process
|
||||||
|
* @throws \Exception
|
||||||
|
*/
|
||||||
|
public function readBySocket(\Swoole\Process $process)
|
||||||
|
{
|
||||||
|
Coroutine::create(function (\Swoole\Process $process) {
|
||||||
try {
|
try {
|
||||||
$content = $process->read();
|
$content = $process->read();
|
||||||
|
|
||||||
@@ -45,6 +91,7 @@ class CrontabProcess extends Process
|
|||||||
} finally {
|
} finally {
|
||||||
$this->onHandler($process);
|
$this->onHandler($process);
|
||||||
}
|
}
|
||||||
|
}, $process);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -76,8 +123,13 @@ class CrontabProcess extends Process
|
|||||||
if (!isset($this->names[$name])) {
|
if (!isset($this->names[$name])) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Timer::exists($this->names[$name]) && Timer::clear($this->names[$name]);
|
$timers = $this->timers[$name];
|
||||||
unset($this->names[$name]);
|
|
||||||
|
$search = array_search($name, $this->scores[$timers]);
|
||||||
|
if ($search !== false) {
|
||||||
|
unset($this->scores[$timers][$search]);
|
||||||
|
}
|
||||||
|
unset($this->timers[$name], $this->names[$name]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -89,22 +141,35 @@ class CrontabProcess extends Process
|
|||||||
/** @var Crontab $content */
|
/** @var Crontab $content */
|
||||||
$content = unserialize($content);
|
$content = unserialize($content);
|
||||||
|
|
||||||
|
$ticker = intval($content->getTickTime() * 1000) + time();
|
||||||
|
|
||||||
|
$this->addTask($content, $ticker);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Crontab $content
|
||||||
|
* @param $ticker
|
||||||
|
*/
|
||||||
|
private function addTask(Crontab $content, $ticker)
|
||||||
|
{
|
||||||
$name = $content->getName();
|
$name = $content->getName();
|
||||||
if (isset($this->names[$name])) {
|
if (isset($this->names[$name])) {
|
||||||
Timer::clear($this->names[$name]);
|
unset($this->names[$content->getName()]);
|
||||||
|
|
||||||
|
$search = array_search($content->getName(), $this->scores);
|
||||||
|
unset($this->scores[$search]);
|
||||||
}
|
}
|
||||||
if ($content->isLoop()) {
|
|
||||||
$this->names[$name] = Timer::tick(intval($content->getTickTime() * 1000), function ($content) {
|
if (!isset($this->scores[$ticker])) {
|
||||||
var_dump(Timer::class);
|
$this->scores[$ticker] = [];
|
||||||
// $content->execute($this);
|
|
||||||
}, $content);
|
|
||||||
} else {
|
|
||||||
$this->names[$name] = Timer::after(intval($content->getTickTime() * 1000), function ($content) {
|
|
||||||
var_dump(Timer::class);
|
|
||||||
// $content->execute($this);
|
|
||||||
}, $content);
|
|
||||||
}
|
}
|
||||||
var_dump($this->names);
|
|
||||||
|
$this->timers[$content->getName()] = $ticker;
|
||||||
|
$this->scores[$ticker][] = $content->getName();
|
||||||
|
$this->names[$content->getName()] = $content;
|
||||||
|
|
||||||
|
ksort($this->scores, SORT_NUMERIC);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user