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.

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

报告相同问题?

悬赏问题

  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因