Home  >  Article  >  Backend Development  >  Detailed explanation of PHP using gearman for asynchronous email or SMS sending operations

Detailed explanation of PHP using gearman for asynchronous email or SMS sending operations

coldplay.xixi
coldplay.xixiforward
2020-07-10 17:12:322258browse

Detailed explanation of PHP using gearman for asynchronous email or SMS sending operations

The example in this article describes PHP using gearman to perform asynchronous email or SMS sending operations. Share it with everyone for your reference, the details are as follows:

1. Preparation work

1. In order to prevent downtime during business processing, please configure gearman Persistence method.
2. Use gearmanManager to manage our worker scripts to facilitate testing.

Related learning recommendations: PHP programming from entry to proficiency

2. Write test scripts

sendEmail.phpThe code is as follows:

<?php
//注意函数名与文件名相同
function sendEmail($job) {
  $workId = uniqid();

  //workload()获取客户端发送来的序列化数据
  $data = json_decode($job->workload(), true);
  
  //这里模拟处理过程
  //具体的业务,这里应该是请求发送邮件的接口,这里只做演示
  sleep(1);
  
  echo "workId: {$workId} 发送 {$data[&#39;email&#39;]} 成功\n";
}

client.phpThe code is as follows:

<?php
//创建一个客户端
$client = new GearmanClient();
//添加一个job服务
$client->addServer(&#39;127.0.0.1&#39;, 4730);

$cnt = 5000;
$ret = array();

//循环发送5000条邮件
for($i = 0; $i < $cnt; ++$i) {
  //doBackground异步,返回提交任务的句柄
  $ret[$i] = $client->doBackground(&#39;sendEmail&#39;, json_encode(array(
    &#39;email&#39; => "{$i}@qq.com",
    &#39;title&#39; => "邮件标题{$i}",
    &#39;body&#39; => "我是内容{$i}",
  )));
}

3. Modify the configuration information in gearmanManager

My gearmanManager is installed under /data/GearmanManager/

> vi /data/GearmanManager/etc/GearmanManager.ini

Add the following information, we start five processes for sendEmail

[sendEmail]
;指定5个进程
dedicated_count=5
;5个进程都只做sendEmail工作
dedicated_only=1

4. Start gearman

> gearmand -d -q mysql \
--mysql-host=192.168.1.100 \
--mysql-port=3306 \
--mysql-user=gearman \
--mysql-password=123456 \
--mysql-db=gearman \
--mysql-table=gearman_queue &

5. Start gearmanManager

> cd /data/GearmanManager
> ./bin/pecl_manager.php -c /data/GearmanManager/etc/GearmanManager.ini -vvv

##6. Run client.php

> /data/php56/bin/php /data/client.php

When we perform ctrl c on pecl_manager.php and forcefully close the worker, client.php can still send requests normally, but the data is saved in mysql.

When we restart the worker, gearman will reload the unprocessed ones for processing.

My mysql is installed on the host machine, and gearman is installed on the virtual machine. If any friends find that gearman cannot connect to mysql, they can temporarily turn off the win10 firewall and enable the ping response of win10. Show.

The above is the detailed content of Detailed explanation of PHP using gearman for asynchronous email or SMS sending operations. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:jb51.net. If there is any infringement, please contact admin@php.cn delete