# Gearman Job Worker for PHP

最近同事接觸到需要 worker 來處理一些非同步的事情。諸如訂單、寄發 email 都常用非同步來執行，以往這些事情大多都用排程解決，其實還有更好的 solution。他們使用 [Gearman](http://gearman.org/) 來解決這個問題。好奇心的驅使，就算不是我實作的我也想去玩玩看新的東西，Gearman 可以在系統裡起一個 `Job Worker` 也可以算是 service，這個 job worker 可以在不起一個排程的情況底下，幫你寄信、處理訂單。也就是當你有需要使用 email 在呼叫他就好了。

## 安裝 Gearman

在 CentOS 中可以直接用 `yum` 安裝

```bash
yum install -y gearmand
```

## 安裝 Gearman 依賴包

```bash
yum -y install re2c geoip geoip-data geoip-devel gcc* boost* libgearman*
```

這兩個步驟都很簡單

## 安裝 PHP 的 Gearman extension

```bash
wget http://pecl.php.net/get/gearman-1.1.1.tgz
cd gearman-1.1.1
phpize
./configure
```

![](https://482391049-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M4iaeavZQiIsfq7eCxo%2Fuploads%2Fgit-blob-04a4e9516e28beb681262e4ae641937a8be25551%2Fgearman-1.PNG?alt=media)

```bash
make && make install
```

![](https://482391049-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M4iaeavZQiIsfq7eCxo%2Fuploads%2Fgit-blob-e1c66c67b5dad066c1ee767b09a7138be8ca5ff5%2Fgearman-2.PNG?alt=media)

![](https://482391049-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M4iaeavZQiIsfq7eCxo%2Fuploads%2Fgit-blob-9c86993c64149d8aad05e37231979e72f3753f36%2Fgearman-3.PNG?alt=media)

最後在 `php.ini` 加上 module

```bash
vim /etc/php.ini
```

```ini
extension="gearman.so"
```

儲存後重新啟動

```bash
service httpd restart
```

這樣 php 就支援 gearman 囉

![](https://482391049-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M4iaeavZQiIsfq7eCxo%2Fuploads%2Fgit-blob-8583175552b15c4fdb9eddf2790d4931920f91d3%2Fgearman-4.PNG?alt=media)

## Hello World

不免俗的一定要測試一下的，這邊我就利用官網提供的程式碼做測試。

首先建立一隻 worker.php

```php
<?php $worker= new GearmanWorker();
  $worker->addServer();
  $worker->addFunction("reverse", "my_reverse_function");
  while ($worker->work());

  function my_reverse_function($job)
  {
    return strrev($job->workload());
  }
?>
```

再建立一隻 client.php

```php
<?php $client= new GearmanClient();
  $client->addServer();
  print $client->do("reverse", "Hello World!");
?>
```

然後執行 worker

```bash
php worker.php
```

再執行 client 就可以得到 Hello World 了

```bash
php client.php
```

若要停止這個 job worker 可以用 `Ctrl + C`

這東西還蠻強大的，之前試用過 [Beanstalk](http://beanstalkapp.com/) 覺得 Gearman 相對強大許多。

> Dec 22nd, 2013 4:40:00pm


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://alanmoment.gitbook.io/moment/php/gearman_job_worker_for_php.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
