Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Queue - remembering property state?

If a job failed, it will be pushed back to the Queue. Is there a way to remember the value of property in the job class when processing job again?

For example:

class MailJob extends Job
{
    public $tries = 3;

    public $status;


    public function __construct()
    {
        $this->status = false; // set to false
    }


    /**
     * Execute the job.
     */
    public function handle()
    {
        $this->status = true;
        // Assume job has failed, it went back to the Queue.
        // status should be true when this job start processing again
    }
}
like image 687
I'll-Be-Back Avatar asked Oct 29 '22 04:10

I'll-Be-Back


1 Answers

If you want to rerun the failed process again on the same moment. you can do something like this.

Here the object is in memory while rerunning the job so data will be available.

I haven't verified it by running it , but hope it will work

class MailJob extends Job{
public $tries = 3;
public $status;


public function __construct()
{
    $this->status = false; // set to false
}


/**
 * Execute the job.
 */
public function handle()
{
    $this->status = true;
    // Assume job has failed, it went back to the Queue.
    // status should be true when this job start processing again

    $failed = processFailedisConfirm();

    if $failed == true && $this->tries > -1 {
         $this->tries = $this->tries - 1;
         $this->handel();
    }
}}

Example of processFailedisConfirm could be

public function processFailedisConfirm(){

     // Core Process to be done in the Job
     $state = (Do Some Api Call); // Here just example, you may send email
                                  // Or can do the core Job Process
                                  // And depending on the Response of the 
                                  // Process return true or false

     // Is Job failed or not ?
     if ( $state == "200" ){
     return false; // Job is not failed
     } else {
     return true; // Job is failed
}

Logic of process is failed or not is depened on the operation you are doing. As i am doing an api call if i get response of 200 my process is successfull. Otherwise process is failed. This just example, sucess reponse of different api can be differnt as desigend by api designer.

like image 162
Manish Champaneri Avatar answered Nov 09 '22 12:11

Manish Champaneri