Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Player626 committed Dec 22, 2020
2 parents 829d848 + 2790e6a commit e2a9ba7
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 30 deletions.
2 changes: 1 addition & 1 deletion bin/easyswoole
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/Command/DefaultCommand/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
11 changes: 9 additions & 2 deletions src/Command/Utility.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,26 @@ 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'])) {
$replace = false;
}
}
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);
}
}
Expand Down
47 changes: 23 additions & 24 deletions src/Crontab/CronRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,53 +23,52 @@ 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();
foreach ($table as $taskName => $task) {
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]);
});
}
}
}
3 changes: 2 additions & 1 deletion src/Crontab/Crontab.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down

0 comments on commit e2a9ba7

Please sign in to comment.