Skip to content

Commit

Permalink
调整gearman初始测试数据
Browse files Browse the repository at this point in the history
  • Loading branch information
ranLee1 committed Nov 29, 2017
1 parent 719a263 commit 02963ed
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 67 deletions.
165 changes: 109 additions & 56 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,91 +1,144 @@
# Description
# 描述:

This package gives you the possibily to add gearman as native queue back-end service
该包是gearmn服务对laravel的一个适配方案,参照 [laravel-gearman](https://github.com/pafelin/laravel-gearman) 进行了扩展,安装之前确保php gearman扩展安装以及gearman服务已正确安装,具体的安装方案可以参考 [gearman服务安装](https://pengbd3.github.io/2017/11/08/gearman1/)

#Installation
# 安装:

first you need to add it to your composer.json
使用composer安装 `composer require many/gearman`
* 进入`laravel`项目,修改配置文件 `config/app.php` ,首先需要注释掉原有队列服务器提供者的代码:

second, in `config/app.php`, you need to comment out the native queue service provider
//'Illuminate\Queue\QueueServiceProvider::class',

//'Illuminate\Queue\QueueServiceProvider',
然后添加自定义的服务器提供者:

and to put this instead:
'Phphc\Gearman\GearmanServiceProvider::class',

'Pafelin\Gearman\GearmanServiceProvider',
* 接着在 `config/queue.php` 文件中添加:

Then in your config/queue.php file you can add:

'default' => 'gearman',
'connections' => array(
'connections' => array(
//追加内容 start
'gearman' => array(
'driver' => 'gearman',
'host' => 'localserver.6min.local',
'host' => '127.0.0.1',
'queue' => 'default',
'port' => 4730,
'timeout' => 1000 //milliseconds
'timeout' => 300
)
)
)

or, if you have multiple gearman servers:
如果你有多个Gearman服务器:

'default' => 'gearman',
'connections' => array(
'connections' => array(
'gearman' => array(
'driver' => 'gearman',
'hosts' => array(
array('host' => 'localserver.6min.local', 'port' => 4730),
array('host' => 'localserver2.6min.local', 'port' => 4730),
array('host' => '127.0.0.1', 'port' => 4730),
array('host' => '127.0.0.2', 'port' => 4730),
),
'queue' => 'default',
'timeout' => 1000 //milliseconds
'timeout' => 300
)
)

Then in your code you can add code as (this is the native way to add jobs to the queue):

Queue::push('SomeClass', array('message' => 'The data that should be available in the SomeClass@fire method'));

Small hint, you can call Namespaced classes and everything that is written in the docs of laravel for calling custom methods is valid here, too.


# Example:

I add a "service" folder to my app folder and inside I create a file "SendMail.php"
The code of the class is here:

<?php

namespace TaskProcess\Services;
)

* 修改 `laravel` 项目根目录 `.evn` 的配置为:

`QUEUE_DRIVER=gearman`

* 接下来执行发布命令 ` php artisan vendor:publish --tag=gearman --force `
* 然后使用composer 自动加载命令 `composer dump-autoload`
* 启动gearman服务 ` gearmand -d -l /var/run/log/gearmand.log `
* 在终端执行服务端:`php artisan queue:work` 客户端: `php artisan gearman:test`
最终效果如下图:
![success](http://oih4t7o53.bkt.clouddn.com/composer/laravel-gearman/gearman1.jpg)
* 多个终端同时运行 `php artisan queue:work` 可以达到并行处理任务的效果。服务端的进程守护建议使用 `laravel` 官方推荐的 [supervisor]('https://laravel.com/docs/5.5/queues#supervisor-configuration')
# 说明:

在执行命令 ` php artisan vendor:publish --tag=gearman --force ` 导入了两个文件到项目中
- job 脚本:`app/Service/SumServer.php` 和 client 脚本:`app/Console/Commands/Workers.php`
- SumServer.php 核心代码:
```php

public function fire($job, $data)
{
$result=$data[0] + $data[1];
//模拟任务消耗时间
sleep(2);
echo "Client results:{$result}\n";
return $result;
}

```
- Workers.php 核心代码:
```php

public function seedWork()
{
$sum = 0;
//实例化gearman服务的客户端
$client = new GearmanClient();
//默认参数主机地址:127.0.0.1 端口:4730
$config = config('queue.gearman');
if (isset($config['hosts'])) {
foreach ($config['hosts'] as $server) {
$client->addServer($server['host'], $server['port']);
}
} else {
$client->addServer($config['host'], $config['port']);
}

//可以通过setCompleteCallback函数给计算结果返回给客户端
$client->setCompleteCallback(function (GearmanTask $task) use (&$sum) {
$sum = $task->data();
$this->info("Server results:" . $sum . "\n");
});

for ($i = 0; $i < 2; $i++) {
//后期版本会封装单独的类来匹配数据项
$data = [
"displayName" => "App\\Services\\SumServer",
"job" => "App\\Services\\SumServer",
"maxTries" => null,
"timeout" => null,
"data" => [1 + $i, 2 + $i]
];
//添加任务
$client->addTask('default', json_encode($data, 1));
}
//添加任务

$client->runTasks();
}
```
- 当laravel队列监听任务启动时,默认将名为 `default` 的函数 注册到gearman服务中,在终端可以通过命令 `gearadmin --status` 查看,在 `$client->addTask` 添加任务的时候第一个参数为函数名,第二个参数为需要提交到job中的数据。`$data1` 数组的 `displayName` 和 `job` 项是需要处理任务的命名空间名,`data`项才是最终 `SumServer` 接收到的值。
- 值得注意的是 `SumServer` 中的 `fire` 函数包含两个参数 `$job` 和 `$data`接收到客户端的数据 `array(1, 2)`
- 服务端的进程守护建议使用 `laravel` 官方推荐的 [supervisor]('https://laravel.com/docs/5.5/queues#supervisor-configuration')
# 使用方法:

在 `app/Service` 文件夹下新增一个名为: `SendMail.php` 的类

```php
<?php
namespace App\Services;

class SendMail {

public function fire($job, $data)
{
//I send an email to my email address with subject "gearman test" and message whatever comes from gearman
mail('pavel@taskprocess.com', 'gearman test', $data['message']);
}

}

In my routes file I add a new Route


```
在你的web路由中添加:
```php
Route::get('/gearman', function() {
//in a loop I add 3 jobs to gearman with different content. The purpose is to see 3 different emails with 3 different contents
foreach (array(1,2,3) as $row) {
Queue::push('TaskProcess\Services\SendMail', array('message' => 'Message' . $row));
Queue::push('App\Services\SendMail', array('message' => 'Message' . $row));
}
});

```

Finally I just run on my console:

php artisan queue:listen

And I go to check what's on my email

#Bugs

Please if you notice a bug open an issue or submit request.

Hope this package will help you
# Bugs
如果发现有任何问题,请提交issue
32 changes: 25 additions & 7 deletions src/Phphc/Gearman/Commands/Workers.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,38 @@ public function handle()
public function seedWork()
{
$sum = 0;
//实例化gearman服务的客户端
$client = new GearmanClient();
$client->addServers('127.0.0.1:4730'); //默认参数主机地址:127.0.0.1 端口:4730
//默认参数主机地址:127.0.0.1 端口:4730
$config = config('queue.gearman');
if (isset($config['hosts'])) {
foreach ($config['hosts'] as $server) {
$client->addServer($server['host'], $server['port']);
}
} else {
$client->addServer($config['host'], $config['port']);
}

//可以通过setCompleteCallback函数给计算结果返回给客户端
//可以通过setCompleteCallback函数给计算结果返回给客户端
$client->setCompleteCallback(function (GearmanTask $task) use (&$sum) {
$sum = $task->data();
$this->info("计算结果为:" . $sum . "\n");
$this->info("Server results:" . $sum . "\n");
});
//添加5个需要累加和的任务
$data = ["displayName" => "App\\Services\\SumServer", "job" => "App\\Services\\SumServer", "maxTries" => null, "timeout" => null, "data" => [1, 2]];

$client->addTask('default', json_encode($data, 1));
for ($i = 0; $i < 2; $i++) {
//后期版本会封装单独的类来匹配数据项
$data = [
"displayName" => "App\\Services\\SumServer",
"job" => "App\\Services\\SumServer",
"maxTries" => null,
"timeout" => null,
"data" => [1 + $i, 2 + $i]
];
//添加任务
$client->addTask('default', json_encode($data, 1));
}
//添加任务

//运行队列中的任务,do系列不需要runTask()
$client->runTasks();
}
}
2 changes: 1 addition & 1 deletion src/Phphc/Gearman/GearmanServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public function boot()
$this->publishes([
__DIR__.'/Service' => base_path('app/Service'),
__DIR__.'/Commands/Workers.php' => base_path('app/Console/Commands/Workers.php'),
]);
],'gearman');
}
/**
* Register the connectors on the queue manager.
Expand Down
8 changes: 5 additions & 3 deletions src/Phphc/Gearman/Service/SumServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

namespace App\Services;

use Illuminate\Support\Facades\DB;

class SumServer
{

public function fire($job, $data)
{
return $data[0] + $data[1];
$result = $data[0] + $data[1];
//模拟任务消耗时间
sleep(2);
echo "Client results:{$result}\n";
return $result;
}

}

0 comments on commit 02963ed

Please sign in to comment.