PHP code example of l669 / async-helper
1. Go to this page and download the library: Download l669/async-helper library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
l669 / async-helper example snippets
class SendMailHelper
{
/**
* @param array $mail
* @throws Exception
*/
public static function request($mail)
{
// 在这里发送邮件,或是通过调用第三方提供的服务发送邮件
// 发送失败的时候你抛出了异常,希望被进程捕获,并按设定的规则进行重试
}
}
use l669\AsyncHelper;
class UserController
{
public function register()
{
// 假设这是一个用户注册的请求,用户提交了姓名、邮箱、验证码
// 第一步、校验用户信息
// 第二步、实例化异步助手,这时候会连接 AMQP
$async_helper = new AsyncHelper([
'host' => '127.0.0.1',
'port' => '5672',
'user' => 'root',
'pass' => '123456',
'vhost' => '/'
]);
// 第三步、保存用户信息到数据库
$mail = [
'from' => '[email protected] ',
'to' => '[email protected] ',
'subject' => '恭喜你注册成功',
'body' => '请点击邮件中的链接完成验证....'
];
// 第四步、通过异步助手发送邮件
$async_helper->run('\\SendMailHelper', 'request', [$mail]);
// 这是同步的模式去发送邮件,如果邮件服务响应迟缓或异常,就会直接影响该请求的响应时间,甚至丢失这封重要邮件
// SendMailHelper::request($mail);
}
}
re_once('SendMailHelper.php');
use l669\AsyncHelper;
use l669\CacheHelper;
$cache_helper = new CacheHelper('127.0.0.1', 11211);
while(true){
try{
$async_helper = new AsyncHelper([
'host' => '127.0.0.1',
'port' => '5672',
'user' => 'root',
'pass' => '123456',
'vhost' => '/',
'cacheHelper' => $cache_helper
]);
$async_helper->consume();
}catch(Exception $e){
// 可以在这里记录一些日志
sleep(2);
}
}
// 接着上面的示例来说,这里省略了一些重复的代码,下同
$async_helper->beginTransaction();
try{
$async_helper->run('\\SendMailHelper', 'request', [$mail1]);
$async_helper->run('\\SendMailHelper', 'request', [$mail2]);
$async_helper->run('\\SendMailHelper', 'request', [$mail3]);
$async_helper->commit();
}catch(\Exception $e){
$async_helper->rollback();
}
use l669\CacheHelper;
use l669\AsyncHelper;
$async_helper = new AsyncHelper([
'host' => '127.0.0.1',
'port' => '5672',
'user' => 'root',
'pass' => '123456',
'vhost' => '/',
'cacheHelper' => new CacheHelper('127.0.0.1', 11211),
'retryMode' => AsyncHelper::RETRY_MODE_REJECT, // 阻塞式重试
'maxDuration' => 600 // 最长重试 10 分钟
]);
$send_mail_helper = new \SendMailHelper();
$mail = new \stdClass();
$mail->from = '[email protected] ';
$mail->to = '[email protected] ';
$mail->subject = '恭喜你注册成功';
$mail->body = '请点击邮件中的链接完成验证....';
$async_helper->run($send_mail_helper, 'request', [$mail]);
// 如果方法中需要抛出异常来结束程序,又不希望被异步进程重试,可以抛出以下几种错误码,进程捕获到这些异常后会放弃重试:
// l669\AsyncException::PARAMS_ERROR
// l669\AsyncException::METHOD_DOES_NOT_EXIST
// l669\AsyncException::KNOWN_ERROR
use l669\CacheHelper;
use l669\AsyncHelper;
$async_helper = new AsyncHelper([
'host' => '127.0.0.1',
'port' => '5672',
'user' => 'root',
'pass' => '123456',
'vhost' => 'new',
'cacheHelper' => new CacheHelper('127.0.0.1', 11211),
'queueName' => 'emails.vip', // 给付费的大爷走 VIP 队列
'retryMode' => AsyncHelper::RETRY_MODE_TTL, // 非阻塞式重试
'maxRetries' => 10 // 最多重试 10 次
]);
$mail = new \stdClass();
$mail->from = '[email protected] ';
$mail->to = '[email protected] ';
$mail->subject = '恭喜你注册成功';
$mail->body = '请点击邮件中的链接完成验证....';
$async_helper->run('\\SendMailHelper', 'request', [$mail]);
# 在命令行下启动消费者进程,推荐使用 supervisor 来管理进程
php consume.php