【问题标题】:Permanently forget the job if failed如果失败则永久忘记工作
【发布时间】:2022-08-24 09:59:04
【问题描述】:

我有一个长期运行的工作,如果失败,我想完全永久删除它,以便不重试或尝试工作。

这是我尝试的

//queue.php

  \'database-large-reports-on-web-server\' => [
            \'driver\' => \'database\',
            \'table\' => \'jobs\',
            \'queue\' => \'ten_minuite_queue_web_server\',
            \'retry_after\' => 1800,
        ],

这是我派遣工作的方式

OrdersExportJob::dispatch()->onConnection(\'database-large-reports-on-web-server\');
// myJob
class OrdersExportJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
   
    public $timeout = 400;
    public $tries = 1;

    public function handle() { ... }
}

//My supervisor configuration

[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/ymyapp/artisan queue:work database-large-reports-on-web-server
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
user=root
numprocs=8
redirect_stderr=true
stdout_logfile=/var/log/worker.log
stopwaitsecs=9999

结果:该作业在失败后继续重新尝试。

    标签: laravel


    【解决方案1】:

    失败时手动删除作业怎么样?

    class OrdersExportJob implements ShouldQueue
    {
        use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    
        // ....
    
        public function failed(Throwable $exception)
        {
            $this->delete();
        }
    

    delete()use Illuminate\Contracts\Queue\Job 的一部分,如果使用 Illuminate\Queue\InteractsWithQueue 特征,则可以访问

    【讨论】:

    【解决方案2】:

    您应该从您的工作中删除 $tries 选项,如果您不想重新尝试您的工作,则不需要它。

    【讨论】:

      【解决方案3】:

      尝试为每个作业添加一个唯一的 id,并存储您在数据库中看到的每个 id 的列表。
      然后,丢弃列表中的所有作业。

      【讨论】:

        【解决方案4】:

        如果您在作业中抛出异常并且$maxExceptions = 1 作业将失败并且不会被重新尝试。

        class OrdersExportJob implements ShouldQueue
        {
            use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
           
            public $maxExceptions = 1;
        
            public function handle() { ... }
        }
        

        当您想停止而不重试时。

        throw new \RuntimeException('Stop the job and go to the failed queue');
        

        然后,您将使用以下命令查看失败队列中的作业:

        php artisan queue:failed
        

        【讨论】:

          猜你喜欢
          • 2011-09-18
          • 2019-01-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-04-05
          相关资源
          最近更新 更多