Laravel's turn - remembering the state of ownership?

If the job failed, it will be returned to the queue. Is there a way to remember the value of a property in a job class when reprocessing a job?

For instance:

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
    }
}
+4
source share
1 answer

If you want to restart the failed process again at the same moment. you can do something like that.

Here the object is in memory when the task is restarted so that the data is available.

I did not test it by running it, but I 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();
    }
}}

An example 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
}

. api, 200, . . , api desigend api.

+1

Source: https://habr.com/ru/post/1682941/


All Articles