MAC Gearman

1.安装Gearman

 brew install gearman

启动Gearman: gearmand -d

2.安装PHP Gearman扩展

 brew install php55-gearman

3.使用mmoreram/GearmanBundle(https://github.com/mmoreram/GearmanBundle)

a. composer 下载GearmanBundle:"mmoreram/gearman-bundle": "dev-master"

b. 在AppKernel.php注册Bundle:

new Doctrine\Bundle\DoctrineCacheBundle\DoctrineCacheBundle(),
new Mmoreram\GearmanBundle\GearmanBundle(),

c.在config.yml里面增加配置

doctrine_cache:
    providers:
        gearman_cache:
            type: file_system
            namespace: doctrine_cache.ns.gearman

gearman:
   # Bundles will parsed searching workers
   bundles:
      # Name of bundle
      AppBundle:

         # Bundle name
         name: AppBundle

         # Bundle search can be enabled or disabled
         active: true

         # If any include is defined, Only these namespaces will be parsed
         # Otherwise, full Bundle will be parsed
         # 不需要包含的文件夹不需要写在下面   
         include:
            - Workers

         # Namespaces this Bundle will ignore when parsing
         ignore:
            - DependencyInjection
            - Resources

   # default values
   # All these values will be used if are not overwritten in Workers or jobs
   defaults:

      # Default method related with all jobs
      # do // deprecated as of pecl/gearman 1.0.0. Use doNormal
      # doNormal
      # doBackground
      # doHigh
      # doHighBackground
      # doLow
      # doLowBackground
      method: doNormal

      # Default number of executions before job dies.
      # If annotations defined, will be overwritten
      # If empty, 0 is defined by default
      iterations: 150

      # Default amount of time in seconds required for the execution to run.
      # This is useful if using a tool such as supervisor which may expect a command to run for a
      # minimum period of time to be considered successful and avoid fatal termination.
      # If empty, no minimum time is required
      minimum_execution_time: null

      # Default maximum amount of time in seconds for a worker to remain idle before terminating.
      # If empty, the worker will never timeout
      timeout: null

      # execute callbacks after operations using Kernel events
      callbacks: true

      # Prefix in all jobs
      # If empty name will not be modified
      # Useful for rename jobs in different environments
      job_prefix: null

      # Autogenerate unique key in jobs/tasks if not set
      # This key is unique given a Job name and a payload serialized
      generate_unique_key: true

      # Prepend namespace when callableName is built
      # By default this variable is set as true
      workers_name_prepend_namespace: true

   # Server list where workers and clients will connect to
   # Each server must contain host and port
   # If annotations defined, will be full overwritten
   #
   # If servers empty, simple localhost server is defined by default
   # If port empty, 4730 is defined by default
   servers:
      localhost:
         host: 127.0.0.1
         port: 4730

d.注册Workers

<?php

namespace AppBundle\Workers;

use Mmoreram\GearmanBundle\Driver\Gearman;

/**
 * @Gearman\Work(
 *     iterations = 0,
 *     minimumExecutionTime = 0,
 *     timeout = 20,
 *     description = "Worker test description",
 *     defaultMethod = "doBackground",
 *     servers = {
 *         { "host": "127.0.0.1", "port": 4730 },
 *     }
 * )
 */
class AppWorker
{
    /**
     * Test method to run as a job
     *
     * @param \GearmanJob $job Object with job parameters
     *
     * @return boolean
     *
     * @Gearman\Job(
     *     iterations = 0,
     *     minimumExecutionTime =0,
     *     timeout = 30,
     *     defaultMethod = "doBackground",
     *     description = "This is a description"
     * )
     */
    public function testA(\GearmanJob $job)
    {
        $result = file_put_contents(__DIR__.'/SecurityController.php',$job->workload());
        echo $result.PHP_EOL;

        return true;
    }

    /**
     * Test method to run as a job
     *
     * @param \GearmanJob $job Object with job parameters
     *
     * @return boolean
     *
     * @Gearman\Job(
     *     iterations = 0,
     *     minimumExecutionTime =0,
     *     timeout = 30,
     *     defaultMethod = "doBackground",
     *     description = "This is a description"
     * )
     */
    public function testB(\GearmanJob $job)
    {
        echo "test B ".PHP_EOL;

        return true;
    }
}

e.启动workers

app/console gearman:worker:list

app/console gearman:worker:execute callname(callname上一个命令可以生成)

这个命令会显示所有的workers,配合supervisord使用更方便

f.Client端的用法

        $gearman = $this->get('gearman');
        $fileContent = file_get_contents(__DIR__.'/SecurityController.php');

//        $result = $gearman->doBackgroundJob('AppBundleWorkersAppWorker~test',$fileContent);

        $gearman
            ->addTaskBackground('AppBundleWorkersAppWorker~testA', $fileContent)
            ->addTaskBackground('AppBundleWorkersAppWorker~testB', 'value3')
            ->runTasks();

注意事项: worker和client必须放在一个project里面,但是server端可以随便部署在任何地方,不要求在同一台机器上。

更多的用法请参照GearmanBundle的文档.

你可能感兴趣的:(PHP,gearman,symfony2)