crontab定时任务组件如何支持command

lx3689589

像laravel里面command的Kernel,直接写command里面任务,如php webman test:test 这样

1076 6 0
6个回答

lx3689589

试了可以在task里面可以这样写
new Crontab('/1 ', function () {
system("php webman test:test");
});

不知道有没更好的办法

  • 暂无评论
luohonen

webman是常驻内存的,crontab是随着webman启动就会自动启动的,又不需要command

  • 暂无评论
北月
  1. 创建 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;
    }
    }
  2. 创建 app/process/Task.php 独立任务文件

<?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秒执行一次
        new Crontab('*/5 * * * * *', function(){
            echo date('Y-m-d H:i:s')."\n";
            global $cli;
            $command = $cli->find('test:test');
            // 如果该命令有参数或选项,需要传入
            // $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);
        });
    }
}

虽然不是很优雅,你可以给 webman/console 提交一个 PR ,比如写一个函数来获取 $cli 命令行应用实例。

  • 暂无评论
不败少龙
<?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服务配置';

    protected function configure()
    {
        $this
        // 命令的名称 ("php console_command" 后面的部分)
        //->setName('model:create')
        // 运行 "php console_command list" 时的简短描述
        ->setDescription('Create new model')
        // 运行命令时使用 "--help" 选项时的完整命令描述
        ->setHelp('This command allow you to create models...')
        // 配置一个参数
        ->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服务配置信息如下:');
        $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');
    }
}
  • 暂无评论
artisan

command的逻辑封装到service里,command 、crontab、controller等等都可以调用

  • 暂无评论
yzh52521
  • 暂无评论
年代过于久远,无法发表回答
×
🔝