亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

根據異常類型限制 Laravel 異常電子郵件通知

根據異常類型限制 Laravel 異常電子郵件通知

PHP
胡說叔叔 2023-09-22 16:55:37
有誰知道 Laravel 中針對特定異常限制電子郵件通知的方法嗎?我讓我的應用程序在出現數據庫錯誤時通過檢查QueryException. 這是我在 Laravel 異常處理程序中所做的一個粗略示例:class Handler{    /**     * Report or log an exception.     *     * This is a great spot to send exceptions to Sentry, Bugsnag, etc.     *     * @param  \Exception  $exception     * @return void     */    public function report(Exception $e)    {        if($e instanceof QueryException){            if( App::environment(['production']) ){                Notification::route('mail', '[email protected]')                            ->notify(new DbErrorNotification($e));            }        }        parent::report($e);    }}由于缺乏數據庫中的跟蹤,有沒有一種方法可以通過異常類型來限制數據庫錯誤,這樣如果存在一致的數據庫錯誤,我最終不會收到數千封電子郵件。我查看了Swift Mailer 的反洪水和節流插件,但這些插件會影響全局系統,這是我不想做的。先感謝您
查看完整描述

1 回答

?
瀟瀟雨雨

TA貢獻1833條經驗 獲得超4個贊

有幾種方法可以實現這一目標。在派遣工作之前,您可以添加delay它。例子:


use App\Http\Request;

use App\Jobs\SendEmail;

use App\Mail\VerifyEmail;

use Carbon\Carbon;


/**

?* Store a newly created resource in storage.

?*

?* @param Request $request

?* @return \Illuminate\Http\RedirectResponse

?* @throws \Symfony\Component\HttpKernel\Exception\HttpException

?*/

public function store(Request $request)

{

? ? $baseDelay = json_encode(now());


? ? $getDelay = json_encode(

? ? ? ? cache('jobs.' . SendEmail::class, $baseDelay)

? ? );


? ? $setDelay = Carbon::parse(

? ? ? ? $getDelay->date

? ? )->addSeconds(10);


? ? cache([

? ? ? ? 'jobs.' . SendEmail::class => json_encode($setDelay)

? ? ], 5);

? ? SendEmail::dispatch($user, new VerifyEmail($user))

? ? ? ? ?->delay($setDelayTime);

}

或者,如果您不喜歡某個工作的想法,您也可以通過 推遲它Mail。例子:


Mail::to($user)->later($setDelayTime);

最后通過 Redis 速率限制。例子:


use Illuminate\Support\Facades\Mail;

use Illuminate\Support\Facades\Redis;

/**

?* Execute the job.

?*

?* @return void

?*/

public function handle()

{

? ? Redis::throttle('SendEmail')

? ? ? ? ->allow(1)

? ? ? ? ->every(10)

? ? ? ? ->then(function () {

? ? ? ? ? ? Mail::to($this->user)->send($this->mail);

? ? ? ? }, function () {

? ? ? ? ? ? return $this->release(10);

? ? ? ? });

}

允許每十秒發送一封電子郵件。傳遞給throttle() 方法的字符串SendEmail 是唯一標識受速率限制的作業類型的名稱。您可以將其設置為您想要的任何值。

release() 方法是作業類的繼承成員,它指示 Laravel 在無法獲得鎖的情況下將作業釋放回隊列,并可選擇以秒為單位的延遲。當作業被分派到隊列時,Redis 被指示每十秒僅運行一個 SendEmail 作業。


查看完整回答
反對 回復 2023-09-22
  • 1 回答
  • 0 關注
  • 104 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號