Skip to content

Commit

Permalink
Merge pull request #309 from tioncico/3.x
Browse files Browse the repository at this point in the history
crontab新增run命令,立即执行一次
  • Loading branch information
kiss291323003 authored Apr 13, 2020
2 parents 2de5268 + 63bb475 commit 031c789
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/Bridge/BridgeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class BridgeCommand extends Event
const CRON_INFO = 201;
const CRON_STOP = 202;
const CRON_RESUME = 203;
const CRON_RUN = 204;
const CONFIG_INFO = 301;
const CONFIG_SET = 302;

Expand Down
35 changes: 26 additions & 9 deletions src/Bridge/DefaultCommand/Crontab.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ static function initCommand(BridgeCommand $command)
$command->set(BridgeCommand::CRON_INFO, [Crontab::class, 'info']);
$command->set(BridgeCommand::CRON_STOP, [Crontab::class, 'stop']);
$command->set(BridgeCommand::CRON_RESUME, [Crontab::class, 'resume']);
$command->set(BridgeCommand::CRON_RUN, [Crontab::class, 'run']);
}

static function info(Package $package,Package $response)
static function info(Package $package, Package $response)
{
$info = \EasySwoole\EasySwoole\Crontab\Crontab::getInstance()->infoTable();
$data = [];
Expand All @@ -32,40 +33,56 @@ static function info(Package $package,Package $response)
$response->setArgs($data);
}

static function stop(Package $package,Package $response)
static function stop(Package $package, Package $response)
{
$contabName = $package->getArgs();
$info = \EasySwoole\EasySwoole\Crontab\Crontab::getInstance()->infoTable();
$crontab = $info->get($contabName);
if (empty($crontab)) {
$response->setArgs("crontab is not found.");
$response->setArgs("crontab:{$contabName} is not found.");
return false;
}
if ($crontab['isStop'] == 1) {
$response->setArgs("crontab is already stop.");
$response->setArgs("crontab:{$contabName} is already stop.");
return false;
}

$info->set($contabName, ['isStop' => 1]);
$response->setArgs("crontab:test is stop suceess.");
$response->setArgs("crontab:{$contabName} is stop suceess.");
return true;
}

static function resume(Package $package,Package $response)
static function resume(Package $package, Package $response)
{
$contabName = $package->getArgs();
$info = \EasySwoole\EasySwoole\Crontab\Crontab::getInstance()->infoTable();
$crontab = $info->get($contabName);
if (empty($crontab)) {
$response->setArgs("crontab is not found.");
$response->setArgs("crontab:{$contabName} is not found.");
return false;
}
if ($crontab['isStop'] == 0) {
$response->setArgs("crontab is running.");
$response->setArgs("crontab:{$contabName} is running.");
return false;
}
$info->set($contabName, ['isStop' => 0]);
$response->setArgs("crontab:test resume suceess.");
$response->setArgs("crontab:{$contabName} resume suceess.");
return true;
}

static function run(Package $package, Package $response)
{
$contabName = $package->getArgs();
$result = \EasySwoole\EasySwoole\Crontab\Crontab::getInstance()->rightNow($contabName);
if ($result === false) {
$response->setArgs("crontab:{$contabName} is not found.");
return false;
}
if ($result<=0){
$response->setArgs("crontab:{$contabName} run error.");
return false;
}
$response->setArgs("crontab:{$contabName} run success");
return true;
}

Expand Down
22 changes: 22 additions & 0 deletions src/Command/DefaultCommand/Crontab.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public function exec(array $args): ?string
case 'resume':
$result = $this->resume($args);
break;
case 'run':
$result = $this->run($args);
break;
default:
$result = $this->help($args);
break;
Expand Down Expand Up @@ -83,6 +86,24 @@ protected function resume($args)
return $data;
}

protected function run($args)
{
$taskName = array_shift($args);
$package = new Package();
$package->setCommand(BridgeCommand::CRON_RUN);
$package->setArgs($taskName);
$package = Bridge::getInstance()->send($package);
if($package->getStatus() !== Package::STATUS_SUCCESS){
return $package->getArgs();
}
if (empty($package->getArgs())) {
return "run error";
}
$data = $package->getArgs();
$data .= "\n" . $this->show();
return $data;
}

protected function show()
{
$package = new Package();
Expand Down Expand Up @@ -111,6 +132,7 @@ public function help(array $args): ?string
php easyswoole crontab show
php easyswoole crontab stop taskName
php easyswoole crontab resume taskName
php easyswoole crontab run taskName
";
}

Expand Down

0 comments on commit 031c789

Please sign in to comment.