dongliao1949 2017-08-03 13:11
浏览 42
已采纳

Laravel Queue - 记得财产状况?

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
    }
}
  • 写回答

1条回答 默认 最新

  • dongyun3805 2017-10-02 06:50
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥50 汇编语言除法溢出问题
  • ¥65 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波