php - PHP 队列停止使用 Twilio 无效的电话号码
问题描述
我正在使用 PHP/Laravel 通过 Twilio 发送批量电子邮件。我不能使用 Twilio 通知,因为每个电话号码都会使用不同的文本。如果所有电话号码都有效,则一切正常,但如果出现任何无效电话号码,我的队列因错误而停止。尝试捕获块或失败的功能根本不起作用
我的代码:
public function handle()
{
$message = $this->client->messages->create(
$this->number,
[
'from' => $this->from,
'body' => $this->msg
]
);
}
(我也尝试将上面的函数代码包装在 try catch 块中,但没有运气)然后我尝试添加失败的函数
public function failed(\Exception $exception)
{
// Send user notification of failure, etc...
}
仍然没有运气。
我收到以下错误:
array:4 [
"code" => 21211
"message" => "The 'To' number is not a valid phone number."
"more_info" => "https://www.twilio.com/docs/errors/21211"
"status" => 400
]
我正在使用以下队列命令
php artisan queue:work --sleep=3 --tries=3 &
收到 Twilio 错误后,如果我停止命令并再运行 2 次,则只有失败的短信移动到 failed_jobs 表并继续执行下一个作业。使用 laravel 7.x
任何帮助高度赞赏。
=================================
这是完整的工作
class BulkSms implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
private $number;
private $msg;
private $client;
private $from;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($number, $from, $msg, $client)
{
$this->number = $number;
$this->from = $from;
$this->msg = $msg;
$this->client = $client;
}
/**
* Execute the job.
* @return void
*/
public function handle()
{
$message = $this->client->messages->create(
$this->number,
[
'from' => $this->from,
'body' => $this->msg
]
);
}
public function failed(\Exception $exception)
{
// Send user notification of failure, etc...
}
}
上面是从控制器调用的,例如
foreach ($candidateSMS as $number) {
BulkSms::dispatch($mobile, $this->from, $msgBody, $client)->delay(now()->addSecond(.2));
}
解决方案
推荐阅读
- javascript - 使用axios时如何停止javascript中的无限循环?
- unity3d - Oculus Quest 单通道和多通道不起作用?
- java - JDBC(Microsoft SQL Server):找不到适合 url 的驱动程序
- python - 熊猫根据一列中值的条件一次覆盖多列中的值
- wpf - 每个监视器的 WPF DPI 感知对话框定位问题
- azure - 从 CI/CD devops 管道构建工件中排除一些文件和文件夹
- delphi - Indy 服务器替换特定字符
- excel - 从多个范围复制行,直到单元格值为空白,粘贴到另一个工作表
- c# - 如果出现错误,如何通过 Outlook 自动向文档控制器发送电子邮件
- c# - Net core 2.2:在 HttpClient 超时时使用 OperationCanceledException 而不是 TaskCanceledException