From 340bcab17492295929333860e149fcf5c5e2cd9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A6=82=E6=9E=9C=E7=9A=84=E5=A6=82=E6=9E=9C?= Date: Tue, 22 Dec 2020 10:40:50 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E9=87=8A=E6=94=BE=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/easyswoole | 2 +- src/Command/DefaultCommand/Install.php | 4 ++-- src/Command/Utility.php | 11 +++++++++-- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/bin/easyswoole b/bin/easyswoole index 93ad4673..3d4d9ab0 100755 --- a/bin/easyswoole +++ b/bin/easyswoole @@ -4,7 +4,7 @@ use EasySwoole\EasySwoole\Command\CommandRunner; use EasySwoole\Command\Caller; $file = null; -foreach ([ __DIR__ . '/../../../autoload.php', __DIR__ . '/../../vendor/autoload.php' ] as $file) { +foreach ([ __DIR__ . '/../../../autoload.php', __DIR__ . '/../../vendor/autoload.php',__DIR__ . '/../vendor/autoload.php' ] as $file) { if (file_exists($file)) { require $file; break; diff --git a/src/Command/DefaultCommand/Install.php b/src/Command/DefaultCommand/Install.php index aa7b3c7b..4264552d 100644 --- a/src/Command/DefaultCommand/Install.php +++ b/src/Command/DefaultCommand/Install.php @@ -33,8 +33,8 @@ public function exec(): ?string unlink(EASYSWOOLE_ROOT . '/easyswoole'); } file_put_contents(EASYSWOOLE_ROOT . '/easyswoole', file_get_contents(__DIR__ . '/../../Resource/easyswoole')); - Utility::releaseResource(__DIR__ . '/../../Resource/Http/Index._php', EASYSWOOLE_ROOT . '/App/HttpController/Index.php'); - Utility::releaseResource(__DIR__ . '/../../Resource/Http/Router._php', EASYSWOOLE_ROOT . '/App/HttpController/Router.php'); + Utility::releaseResource(__DIR__ . '/../../Resource/Http/Index._php', EASYSWOOLE_ROOT . '/App/HttpController/Index.php',true); + Utility::releaseResource(__DIR__ . '/../../Resource/Http/Router._php', EASYSWOOLE_ROOT . '/App/HttpController/Router.php',true); Utility::releaseResource(__DIR__ . '/../../Resource/Config._php', EASYSWOOLE_ROOT . '/dev.php'); Utility::releaseResource(__DIR__ . '/../../Resource/Config._php', EASYSWOOLE_ROOT . '/produce.php'); Utility::releaseResource(__DIR__ . '/../../Resource/bootstrap._php', EASYSWOOLE_ROOT . '/bootstrap.php'); diff --git a/src/Command/Utility.php b/src/Command/Utility.php index 69d816fb..4d9d3d27 100644 --- a/src/Command/Utility.php +++ b/src/Command/Utility.php @@ -43,12 +43,12 @@ static function displayItem($name, $value) return "\e[32m" . str_pad($name, 30, ' ', STR_PAD_RIGHT) . "\e[34m" . $value . "\e[0m"; } - public static function releaseResource($source, $destination) + public static function releaseResource($source, $destination,$confirm = false) { + $filename = basename($destination); clearstatcache(); $replace = true; if (is_file($destination)) { - $filename = basename($destination); echo Color::danger("{$filename} has already existed, do you want to replace it? [ Y / N (default) ] : "); $answer = strtolower(trim(strtoupper(fgets(STDIN)))); if (!in_array($answer, ['y', 'yes'])) { @@ -56,6 +56,13 @@ public static function releaseResource($source, $destination) } } if ($replace) { + if($confirm){ + echo Color::danger("do you want to release {$filename}? [ Y / N (default) ] : "); + $answer = strtolower(trim(strtoupper(fgets(STDIN)))); + if (!in_array($answer, ['y', 'yes'])) { + return; + } + } File::copyFile($source, $destination); } } From 2790e6a23fc9edf4551ddcf734731bb27a36e43f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A6=82=E6=9E=9C=E7=9A=84=E5=A6=82=E6=9E=9C?= Date: Tue, 22 Dec 2020 11:28:10 +0800 Subject: [PATCH 2/2] =?UTF-8?q?Crontab=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Crontab/CronRunner.php | 47 +++++++++++++++++++------------------- src/Crontab/Crontab.php | 3 ++- 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/src/Crontab/CronRunner.php b/src/Crontab/CronRunner.php index 4f886ff3..26ee4c49 100644 --- a/src/Crontab/CronRunner.php +++ b/src/Crontab/CronRunner.php @@ -23,34 +23,30 @@ public function run($arg) $tasks = $arg; /** @var Table $table */ $table = Crontab::getInstance()->infoTable(); - /* - * 先清空一遍规则 - */ + //先清空一遍规则,禁止循环的时候删除key + $keys = []; foreach ($table as $key => $value) { + $keys[] = $key; + } + foreach ($keys as $key){ $table->del($key); } //这部分的解析,迁移到Crontab.php做 foreach ($tasks as $taskName => $cronTaskClass) { - /** - * @var $cronTaskClass AbstractCronTask - */ + /** @ @var $cronTaskClass AbstractCronTask */ $taskName = $cronTaskClass::getTaskName(); $taskRule = $cronTaskClass::getRule(); $nextTime = CronExpression::factory($taskRule)->getNextRunDate()->getTimestamp(); - $table->set($taskName, ['taskRule' => $taskRule, 'taskRunTimes' => 0, 'taskNextRunTime' => $nextTime, 'isStop' => 0]); + $table->set($taskName, ['taskRule' => $taskRule, 'taskRunTimes' => 0, 'taskNextRunTime' => $nextTime, 'currentRunTime'=>0,'isStop' => 0]); $this->tasks[$taskName] = $cronTaskClass; } $this->cronProcess(); - Timer::getInstance()->loop(29 * 1000, function () { + //60无法被8整除。 + Timer::getInstance()->loop(8 * 1000, function () { $this->cronProcess(); }); } - public function onShutDown() - { - // TODO: Implement onShutDown() method. - } - private function cronProcess() { $table = Crontab::getInstance()->infoTable(); @@ -58,18 +54,21 @@ private function cronProcess() if ($task['isStop']) { continue; } - $taskRule = $task['taskRule']; - $nextRunTime = CronExpression::factory($task['taskRule'])->getNextRunDate(); - $distanceTime = $nextRunTime->getTimestamp() - time(); - if ($distanceTime < 30) { - Timer::getInstance()->after($distanceTime * 1000, function () use ($taskName, $taskRule) { - $table = Crontab::getInstance()->infoTable(); - $nextRunTime = CronExpression::factory($taskRule)->getNextRunDate(); - $table->incr($taskName, 'taskRunTimes', 1); - $table->set($taskName, ['taskNextRunTime' => $nextRunTime->getTimestamp()]); - TaskManager::getInstance()->async($this->tasks[$taskName]); - }); + $nextRunTime = CronExpression::factory($task['taskRule'])->getNextRunDate()->getTimestamp(); + if($task['taskNextRunTime'] != $nextRunTime){ + $table->set($taskName,['taskNextRunTime'=>$nextRunTime]); + } + if(($nextRunTime == $task['taskNextRunTime']) && $nextRunTime == $task['currentRunTime']){ + //本轮已经创建过任务 + continue; } + $table->set($taskName,['currentRunTime'=>$nextRunTime]); + $distanceTime = $nextRunTime- time(); + Timer::getInstance()->after($distanceTime * 1000, function () use ($taskName) { + $table = Crontab::getInstance()->infoTable(); + $table->incr($taskName, 'taskRunTimes', 1); + TaskManager::getInstance()->async($this->tasks[$taskName]); + }); } } } diff --git a/src/Crontab/Crontab.php b/src/Crontab/Crontab.php index e7500303..d55b8161 100644 --- a/src/Crontab/Crontab.php +++ b/src/Crontab/Crontab.php @@ -27,7 +27,8 @@ function __construct() $this->table = new Table(1024); $this->table->column('taskRule', Table::TYPE_STRING, 35); $this->table->column('taskRunTimes', Table::TYPE_INT, 8); - $this->table->column('taskNextRunTime', Table::TYPE_INT, 8); + $this->table->column('taskNextRunTime', Table::TYPE_INT, 10); + $this->table->column('currentRunTime', Table::TYPE_INT, 10); $this->table->column('isStop', Table::TYPE_INT, 1); $this->table->create(); }