如何为长时间运行的作业设置自定义retry_after |拉拉韦尔 [英] How can i set custom retry_after for long running jobs | laravel
问题描述
问题:如何自定义长时间运行的作业,而不在每次retry_after秒后尝试多次尝试?
Question: How to customize long-running job without attempting multiple time after each retry_after seconds?
我有一份工作需要1到3个小时才能运行,我已经基于laravel文档创建了基于作业的文件,这是我的作业文件.
I have one job which will take 1 to 3 hours to run, I already created job-based on laravel documentation, here is my job file.
<?php
namespace App\Modules\Csv\Jobs;
use App\Jobs\Job;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use League\Csv\Reader;
use Phone;
/**
* A single excel import job, which can be pushed on to a queue
*/
class UploadCsvDataInTable extends Job implements ShouldQueue
{
use InteractsWithQueue, SerializesModels, Dispatchable,Queueable;
public $timeout = 172800;
/**
* The excel to import
*
* @var App\BulkUpload
*/
protected $csvUpload;
/**
* Create a new job instance.
*
* @param App\FeedImport
*
* @return void
*/
public function __construct(CsvUpload $csvUpload)
{
$this->csvUpload = $csvUpload;
}
public function handle()
{
app(CsvUploadService::class)->uploadCsv($this->csvUpload);
}
}
这是Laravel 文档至指定作业超时.
here is Laravel document to specify a timeout for jobs.
这是我如何称职的代码.
here is code for how I am calling that job.
UploadCsvDataInTable::dispatch($csvUpload)->onConnection('redis')->onQueue('low');
我的队列命令:在主管中工作.
my command for queue:work In supervisor.
php artisan queue:work --queue=high,low,default --sleep=3 --tries=3
这是我对队列&的配置地平线
here is my configuration for queue & horizon
// horizon.php
'production' => [
'supervisor-1' => [
'connection' => 'redis',
'queue' => ['high', 'default', 'low'],
'balance' => 'simple',
'processes' => 6,
'tries' => 3,
],
],
//queue.php
'redis' => [
'driver' => 'redis',
'connection' => 'default',
'queue' => env('REDIS_QUEUE', 'default'),
'retry_after' => 90,
'block_for' => null,
],
我可以看到由于retry_,我的工作尝试了多次,并且在按照地平线配置达到3次尝试后,抛出了MaxAttemptsExceededException.
I can see because of retry_after my job is attempting multiple time and after reaching 3 try as per horizon configuration its throwing MaxAttemptsExceededException.
如果我将$ timeout增加到24小时,则由于retry_after多次尝试执行该作业,我的数据库中得到了重复的记录.
if I increase $timeout to 24 hours I am getting duplicate records in my db as retry_after is attempting that job multiple time.
有什么方法可以为此作业设置自定义retry_after吗?
is there any way I can set custom retry_after for this job?
推荐答案
我为长期运行的作业创建了另一个连接,并且对我来说它正常工作.
I have created another connection for long-running jobs and its working properly for me.
在horizontal.php中创建了新的主管连接,以确保长期运行
created new supervisor connection in horizon.php for long running process
'supervisor-long-running' => [
'connection' => 'redis-long-processes',
'queue' => 'long-running',
'balance' => 'simple',
'processes' => 3,
'tries' => 1,
'timeout' => 86000 // should be shorter than retry_after out
]
和queue.php中的新redis连接
and new redis connection in queue.php
'redis-long-processes' => [
'driver' => 'redis',
'connection' => 'default',
'queue' => 'long-running',
'retry_after' => 86400,
'block_for' => null,
],
在database.php中为长时间运行的作业添加了新队列.
in database.php added new queue for long running jobs.
'queue' => [
[
'connection' => 'redis',
'queue' => ['high', 'default', 'low','long-running'],
'balance' => 'simple',
'processes' => 6,
'tries' => 3,
'url' => env('REDIS_URL'),
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', '6379'),
'database' => env('REDIS_CACHE_DB', '1'),
],
],
也不要忘记使用onConnection和onQueue来指定应从哪个队列中执行作业的作业.
also don't forgot to call jobs using onConnection and onQueue to specify from which queue jobs should execute.
UploadDataInTable::dispatch($upload)->onConnection('redis-long-processes')->onQueue('long-running');
这篇关于如何为长时间运行的作业设置自定义retry_after |拉拉韦尔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!