<?php
declare (strict_types = 1);
namespace app\command;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use Workerman\Worker;
class Power extends Command
{
/**
* @var int
*/
protected $timer;
/**
* @var int|float
*/
protected $interval = 2; //每隔2秒执行一次
protected function configure()
{
// 指令配置
$this->setName('plan')
->addArgument('status', Argument::REQUIRED, 'start/stop/reload/status/connections')
->addOption('d', null, Option::VALUE_NONE, 'daemon(守护进程)方式启动')
->addOption('i', null, Option::VALUE_OPTIONAL, '多长时间执行一次')
->setDescription('开启/关闭/重启 定时任务');
}
protected function init(Input $input, Output $output)
{
global $argv;
if ($input->hasOption('i'))
$this->interval = floatval($input->getOption('i'));
$argv[1] = $input->getArgument('status') ?: 'start';
if ($input->hasOption('d')) {
$argv[2] = '-d';
} else {
unset($argv[2]);
}
}
protected function execute(Input $input, Output $output)
{
$output->writeln('timer');
$this->init($input, $output);
$task = new Worker();
$task->count = 1;
//start
//end
$task->onWorkerStart = [$this, 'start'];
$task->runAll();
}
public function stop()
{
\Workerman\Lib\Timer::del($this->timer);
}
public function start()
{
$last = time();
$task = [2 => $last];
$this->timer = \Workerman\Lib\Timer::add($this->interval, function () use (&$task) {
//每隔2秒执行一次
try {
$now = time();
foreach ($task as $sec => $time) {
if ($now - $time >= $sec) {
echo 'task exec do.....'."\r\n";
//每隔$sec秒执行一次
$task[$sec] = $now;
}
}
} catch (\Throwable $e) {
}
});
}
}
//遍历加载文件
// 加载所有Applications/*/start.php,以便启动所有服务
foreach(glob(__DIR__.'/app/worker/start*.php') as $start_file)
{
require_once $start_file;
}