[英]Remove delay in Laravel Queue
我是 Laravel-Lumen 框架的粉絲。 這些設計得非常好,我們可以開始實施這些應用程序。 但是有一個小問題。 可能這不是問題,但在我看來這是一個小問題。 讓我解釋一下。
例如,我有一個 model 並且我正在使用彈性搜索。 當創建 model(插入到 db)時,我正在調度一個作業來索引這個 model。
public static function boot()
{
parent::boot();
static::created(function ($model) {
$modelClass = get_class($model);
lgi('>>> model created:' . $modelClass, $model);
dispatch(new ElasticIndexerJob('creted', $model));
});
static::updated(function ($model) {
$modelClass = get_class($model);
lgi('>>> model updated:' . $modelClass, $model);
dispatch(new ElasticIndexerJob('updated', $model));
});
static::deleted(function ($model) {
$modelClass = get_class($model);
lgi('>>> model deleted:' . $modelClass, $model);
dispatch(new ElasticIndexerJob('deleted', $model));
});
}
這將輸出該日志:
[2021-04-18 11:28:36] local.INFO: /app/Models/Traits/Indexable.php:25 [
">>> model updated:App\\Models\\City",
{
"id": 3,
"country_id": 85,
"state_id": 1,
"zip_code": "87506",
"name": "Nambe",
"lat": 35.8890389,
"lng": -106.0657318,
"status": "passive",
"created_at": "2021-04-13 09:38:09",
"updated_at": "2021-04-18 11:28:36"
}
]
時間是11:28:36
。 之后,我正在尋找隊列日志 output。
[2021-04-18 11:28:38][27] Processing: App\Jobs\ElasticIndexerJob
[2021-04-18 11:28:38] local.INFO: /app/Jobs/ElasticIndexerJob.php:32 [
"App\\Models\\City",
"updated",
{
"id": 3,
"country_id": 85,
"state_id": 1,
"zip_code": "87506",
"name": "Nambe",
"lat": 35.8890389,
"lng": -106.0657318,
"status": "passive",
"created_at": "2021-04-13 09:38:09",
"updated_at": "2021-04-18 11:28:36"
}
]
[2021-04-18 11:28:38][27] Processed: App\Jobs\ElasticIndexerJob
隊列 output 時間為11:28:38
。 如您所見,有2 seconds
的延遲(或差異)。 所有隊列(發送郵件、執行其他作業等)都會出現此問題。 發送郵件時這不是問題,但有時我需要實時執行。 我想在調度時立即執行工作。
通常,如果我自己使用 beanstalkd,那么我可以立即收到消息(工作),並且可以用該消息做一些事情。 但是在 Laravel(或流明)中存在延遲,這讓我很惱火。
為什么 Laravel 有延遲,我該如何消除這種延遲?
我在數據庫隊列和 beanstalkd 隊列中對此進行了測試。 發生相同的行為:
QUEUE_CONNECTION=beanstalkd
; QUEUE_CONNECTION=database
注意: lgi()
function 是Log::info()
的一個信封
您是否嘗試過dispatchSync
,我不知道 Lumen 是否可用。 Laravel dispatchSync
我找到了解決方案。 我查看了 Laravel 的源代碼,我在這個文件中看到了該代碼: Illuminate\Queue\Listener
/**
* The amount of seconds to wait before polling the queue.
*
* @var int
*/
protected $sleep = 3;
我進行了更深入的調查,發現:
/**
* Create the command with the listener options.
*
* @param string $connection
* @param string $queue
* @param \Illuminate\Queue\ListenerOptions $options
* @return array
*/
protected function createCommand($connection, $queue, ListenerOptions $options)
{
return array_filter([
$this->phpBinary(),
$this->artisanBinary(),
'queue:work',
$connection,
'--once',
"--name={$options->name}",
"--queue={$queue}",
"--backoff={$options->backoff}",
"--memory={$options->memory}",
"--sleep={$options->sleep}",
"--tries={$options->maxTries}",
], function ($value) {
return ! is_null($value);
});
}
如您所見,有一個 CLI 參數--sleep=[second]
。 我們可以將此值設置為零 (0)。 在該隊列工作人員不等待處理下一個作業之后。
php artisan queue:work --sleep=0.1
我認為這些參數必須在文檔中,但 Laravel 開發人員沒有添加這個。 或者可能是我錯過了這個我不知道。
編輯:如果您使用數據庫作為隊列后端,那么這種用法可以加載您的數據庫。 因為數據庫驅動程序正在向jobs
表發送SELECT
查詢。 我建議使用 beanstalkd 驅動程序。
編輯 2:如果您將--sleep
設置為0
(零),那么它可能會吃掉 cpu。
編輯3:我現在看到了。 可能我必須 go 去看眼科醫生:S https://laravel.com/docs/8.x/queues#worker-sleep-duration
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.