首页 文章

如果出现异常,Laravel会停止队列作业

提问于
浏览
0

我想知道如果抛出异常,我怎么能在laravel中停止队列作业 .

public function handle()
{

       try {
          //STEP 1 tasks
       } catch (\Exception $e) {
           Log::error('Error message: '.$e);
       }

       try {
          //STEP 2 tasks
       } catch (\Exception $e) {
           Log::error('Error message: '.$e);
       }


}

现在让我们说 STEP 1 失败并且异常是登录错误 .

我的工作仍然会继续并尝试执行 STEP 2 吗?

我想确保 STEP 1 失败然后 STEP 2 不应该被执行 .

我不想把所有内容放在同一个try catch中 .

2 回答

  • 0

    试试这种方法

    public function handle()
    {
    
          //Assign a variable which determine if step 1 is success
          $step_1_success = true;
           try {
              //STEP 1 tasks
           } catch (\Exception $e) {
                //if catch error occured set it to false
                $step_1_success = false;
               Log::error('Error message: '.$e);
           }
    
           //before running step 2 check if step 1 is success base on the created variable `$step_1_success`
           if ($step_1_success) {
             try {
                //STEP 2 tasks
             } catch (\Exception $e) {
                 Log::error('Error message: '.$e);
             }
          }
    
    
    }
    
  • 0

    如果您只是想知道该工作失败,您可以试试这个 .

    public function failed(Exception $exception)
        {
            // Send user notification of failure, etc...
        }
    

    如果要永久删除作业,可以使用 delete() 方法 . 您的工作将被永久删除 .

    public function handle()
    {
    
           try {
              //STEP 1 tasks
           } catch (\Exception $e) {
               $this->delete();
               Log::error('Error message: '.$e);
           }
    
           try {
              //STEP 2 tasks
           } catch (\Exception $e) {
               Log::error('Error message: '.$e);
           }
    
    
    }
    

相关问题