jeudi 24 mai 2018

Receiving multiple mail using redis queue

I have the following code inside my job handle method. I'm receiving multiple mails. Not sure whether it's because of mail sending failure or other method's.

public function handle()
{
    $this->mailLog->pushHandler(new StreamHandler(storage_path('logs/send-mail/' . $this->db . '/' . date('Y-m-d') . '.log')), Logger::INFO);
     try {
        if(isset($this->mail['file']) && !empty($this->mail['file'])) {
            $result = $this->mgClient->sendMessage($this->mgDomain, $this->mail['data'], $this->mail['file']['attachment']);
                File::deleteDirectory($this->mail['file']['path']);
        } else {
            $result = $this->mgClient->sendMessage($this->mgDomain, $this->mail['data']);
        }
        $this->mailLog->info("Result : " . print_r($result,true));
        $this->updateMailStatusAndMessageId($result);
    } catch (\Exception $e) {
        if(strstr($e->getMessage(), config('constants.MAILGUN_LIMIT_EXCEED'))) {
            $timeToRelase = intVal(str_replace(config('constants.MAILGUN_LIMIT_EXCEED'), "", $e->getMessage())) + 5;
            $this->mailLog->info('Releasing job back to queue after ' . $timeToRelase . ' seconds');
            $this->release($timeToRelase);
        } 
        $this->mailLog->info('SendMail Queue : ' . $e->getMessage());
        Mail::on($this->db)->find($this->mailId)->update(['status'=>'failed']);
    }
}


public function updateMailStatusAndMessageId($result) 
{
    $mail = Mail::on($this->db)->find($this->mailId);
    if($result && isset($result->http_response_body->id)) {
       $attributes =['messageId' => $result->http_response_body->id, 'status' => 'send'];
       $mail->update($attributes);
       $this->mailLog->info('updated status and message id of mail id : ' . $mail->id . " on " . date("Y/m/d H:i:s"));
    }
}

Inside log i'm receiving the following error.

[2018-05-24 06:48:05] lumen.ERROR: 
Symfony\Component\Debug\Exception\FatalThrowableError: Call to a member function update() on null in /var/www/html/communication/app/Jobs/SendMail.php:68
Stack trace:
#0 /var/www/html/communication/app/Jobs/SendMail.php(52): App\Jobs\SendMail->updateMailStatusAndMessageId(Object(stdClass))

Can anyone guide me how to avoid multiple processing of mails.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire