gearman

  1. 这里以在Symfony3.x中使用为例

  2. 在Symfony创建CommandBundle

  3. 在CommandBundle目录创建Command目录

  4. client代码 src/CommandBundle/Command/ClientCommand.php

     <?php
    
     namespace CommandBundle\Command;
    
     use Symfony\Component\Console\Command\LockableTrait;
     use Symfony\Component\Console\Input\InputInterface;
     use Symfony\Component\Console\Output\OutputInterface;
     use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
    
     class ClientCommand extends ContainerAwareCommand
     {
         use LockableTrait;
    
         protected function configure()
         {
             $this->setName('gearman:client:distribute_work')
                 ->setDescription('Distribute work.');
         }
    
         protected function execute(InputInterface $input, OutputInterface $output)
         {
             if (!$this->lock()) {
                 echo 'Command is runing...' . "\n";
                 return false;
             }
             $works = [
                 123,
                 124,
                 125
             ];
             foreach ($works as $work) {
                 $client = new \GearmanClient();
                 $client->addServer('127.0.0.1', 4730);
                 $client->doBackground("work", $work);
             }
             echo 'Distribute work success!' . "\n";
             return true;
         }
     }
    
  5. worker代码 src/CommandBundle/Command/WorkerCommand.php

     <?php
    
     namespace CommandBundle\Command;
    
     use Symfony\Component\Console\Input\InputInterface;
     use Symfony\Component\Console\Output\OutputInterface;
     use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
    
     class WorkerCommand extends ContainerAwareCommand
     {
         protected function configure()
         {
             $this->setName('gearman:worker:do_work')
                 ->setDescription('Create a worker to work!');
         }
    
         protected function execute(InputInterface $input, OutputInterface $output)
         {
             $worker = new \GearmanWorker();
             $worker->addServer('127.0.0.1', 4730);
             $worker->addFunction("work", array($this, "doWork"));
             while ($worker->work()) {
                 if ($worker->returnCode() != GEARMAN_SUCCESS) {
                     echo "错误 状态:" . $worker->returnCode() . "\n";
                     break;
                 }
             };
         }
    
         public function doWork($job)
         {
             $parameter = $job->workload();
             echo $parameter . "\n";
             return true;
         }
     }
    
  6. 查看gearman状态

    1. 安装gearman管理包

       composer require brianlmoon/net_gearman
       # 或者修改
       "require": {
           "brianlmoon/net_gearman": "dev-master"
       }
       composer update
      
    2. 编辑代码

       use Net_Gearman_Manager;
      
       $gearman_manager = new Net_Gearman_Manager('127.0.0.1');
       $status = $gearman_manager->status();
       $gearman_manager->disconnect();
       unset($gearman_manager);
       print_r($status);
      

results matching ""

    No results matching ""