文档章节

MAC Gearman

l
 light_
发布于 2016/05/10 15:20
字数 647
阅读 87
收藏 1

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的文档.

© 著作权归作者所有

共有 人打赏支持
l
粉丝 1
博文 8
码字总数 2973
作品 0
芜湖
程序员
私信 提问
mac os 10.13 php7.1 gearman 安装

需要从源码编译安装 安装gearman https://github.com/gearman/gearmand/releases 下载1.18版本 根据readme里的步骤进行安装 https://github.com/gearman/gearmand #大写V gearmand -V 安装p...

云墨雪
05/13
0
0
用 Gearman 分发 PHP 应用程序的工作负载

尽管一个 Web 应用程序的大部分内容都与表示有关,但它的价值与竞争优势却可能体现在若干专有服务或算法方面。如果这类处理过于复杂或拖沓,最好是进行异步执行,以免 Web 服务器对传入的请求...

红薯
2010/02/02
3K
3
并发利器 Gearman (一) 尝试

PHP 没有提供直接的并发功能。要实现并发,必须: function asynsend(){ $fp = fsockopen('localhost', 80, &$errno, &$errstr, 5); if(!$fp){ echo "$errstr ($errno) /n"; } fputs($fp, "GE......

BearCatYN
2015/04/14
288
0
Gearman异步服务安装使用

一、简介 gearman异步处理服务。 官网地址:http://gearman.org/ 二. 安装1,安装管理软件仓库,添加gearman服务地址源 2.安装GearmanJobServer、Dev Tools、Perform Upgrade 3.如果没有PECL,...

Jpchina
2017/08/07
0
0
CentOS7.X下安装Gearman-1.1.17及其PHP-7.X扩展

CentOS下安装Gearman及其PHP扩展 本文由原官方文档翻译而来 安装Gearman 安装前的准备 安装gearmand 源码地址 源码下载 创建gearman用户 启动gearman并设置开机启动 安装php扩展 安装php的g...

qq2233466866
06/11
0
0

没有更多内容

加载失败,请刷新页面

加载更多

apache顶级项目(二) - B~C

apache顶级项目(二) - B~C https://www.apache.org/ Bahir Apache Bahir provides extensions to multiple distributed analytic platforms, extending their reach with a diversity of s......

晨猫
今天
1
0
day152-2018-11-19-英语流利阅读

“超级食物”竟然是营销噱头? Daniel 2018-11-19 1.今日导读 近几年来,超级食物 superfoods 开始逐渐走红。不难发现,越来越多的轻食餐厅也在不断推出以超级食物为主打食材的健康料理,像是...

飞鱼说编程
今天
7
0
SpringBoot源码:启动过程分析(二)

接着上篇继续分析 SpringBoot 的启动过程。 SpringBoot的版本为:2.1.0 release,最新版本。 一.时序图 一样的,我们先把时序图贴上来,方便理解: 二.源码分析 回顾一下,前面我们分析到了下...

Jacktanger
昨天
3
0
Apache防盗链配置,Directory访问控制,FilesMatch进行访问控制

防盗链配置 通过限制referer来实现防盗链的功能 配置前,使用curl -e 指定referer [root@test-a test-webroot]# curl -e "http://www.test.com/1.html" -x127.0.0.1:80 "www.test.com/1.jpg......

野雪球
昨天
5
0
RxJava threading

因为Rx针对异步系统设计,并且Rx也自然支持多线程,所以新的Rx开发人员有时会假设Rx默认是多线程的。在其他任何事情之前,重要的是澄清Rx默认是单线程的。 除非另有说明,否则每次调用onNex...

woshixin
昨天
2
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部