像laravel里面command的Kernel,直接寫command里面任務(wù),如php webman test:test 這樣
試了可以在task里面可以這樣寫
new Crontab('/1 ', function () {
system("php webman test:test");
});
不知道有沒更好的辦法
webman是常駐內(nèi)存的,crontab是隨著webman啟動(dòng)就會(huì)自動(dòng)啟動(dòng)的,又不需要command
創(chuàng)建 app/command/Test.php
命令文件
<?php
namespace app\command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;
class Test extends Command
{
protected static $defaultName = 'test:test';
protected static $defaultDescription = 'test test';
protected function configure()
{
$this->addArgument('name', InputArgument::OPTIONAL, 'Name description');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$name = $input->getArgument('name');
$output->writeln('Hello test:test');
return self::SUCCESS;
}
}
創(chuàng)建 app/process/Task.php
獨(dú)立任務(wù)文件
<?php
namespace process;
use Workerman\Crontab\Crontab;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
class Task
{
public function onWorkerStart()
{
// 每5秒執(zhí)行一次
new Crontab('*/5 * * * * *', function(){
echo date('Y-m-d H:i:s')."\n";
global $cli;
$command = $cli->find('test:test');
// 如果該命令有參數(shù)或選項(xiàng),需要傳入
// $arguments = [
// 'name' => 'Fabien',
// '--yell' => true,
// ];
$arguments = [];
$greetInput = new ArrayInput($arguments);
$output = new BufferedOutput();
$returnCode = $command->run($greetInput, $output);
$content = $output->fetch();
var_dump($returnCode);
var_dump($content);
});
}
}
雖然不是很優(yōu)雅,你可以給 webman/console
提交一個(gè) PR
,比如寫一個(gè)函數(shù)來獲取 $cli
命令行應(yīng)用實(shí)例。
<?php
namespace app\command;
use GatewayWorker\BusinessWorker;
use GatewayWorker\Gateway;
use GatewayWorker\Register;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Workerman\Worker;
use Symfony\Component\Console\Input\InputArgument;
class GatewayCommand extends \Symfony\Component\Console\Command\Command
{
protected static $defaultName = 'config:gateway';
protected static $defaultDescription = 'gateway服務(wù)配置';
protected function configure()
{
$this
// 命令的名稱 ("php console_command" 后面的部分)
//->setName('model:create')
// 運(yùn)行 "php console_command list" 時(shí)的簡短描述
->setDescription('Create new model')
// 運(yùn)行命令時(shí)使用 "--help" 選項(xiàng)時(shí)的完整命令描述
->setHelp('This command allow you to create models...')
// 配置一個(gè)參數(shù)
->addArgument('name', InputArgument::REQUIRED, 'what\'s model you want to create ?');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
global $argv;
// $argv[2] = 'start';
$argv[3] = '-d';
$output->writeln('gateway服務(wù)配置信息如下:');
$this->startGateWay();
$this->startBusinessWorker();
$this->startRegister();
Worker::runAll();
return self::SUCCESS;
}
private function startBusinessWorker()
{
$worker = new BusinessWorker();
$worker->name = 'BusinessWorker';
$worker->count = 1;
$worker->registerAddress = '127.0.0.1:1237';
$worker->eventHandler = app_path() . "/service/Events.php";
}
private function startGateWay()
{
$gateway = new Gateway("websocket://0.0.0.0:2347");
$gateway->name = 'Gateway';
$gateway->count = 4;
$gateway->lanIp = '127.0.0.1';
$gateway->startPort = 40001;
$gateway->pingInterval = 30;
$gateway->pingNotResponseLimit = 0;
$gateway->pingData = '{"type":"ping"}';
$gateway->registerAddress = '127.0.0.1:1237';//正式 1237 測試 1236
}
private function startRegister()
{
new Register('text://0.0.0.0:1237');
}
}