Laravel 作业立即在浏览器中执行,而不是 运行 在后台队列中执行
Laravel Job executed immediately in browser instead of running in a background queue
我是 Laravel 的新手,并尝试创建我的第一个后台任务。
使用的文档:https://laravel.com/docs/master/queues
工作:(ProcessDatabaseImport.php)
<?php
namespace App\Jobs;
use App\Contact;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\File;
class ProcessDatabaseImport implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $file;
/**
* Create a new job instance.
*
* @param String $filePath
* @return void
*/
public function __construct($filePath)
{
// init File object "database/data/contacts.json"
$this->file = base_path($filePath);
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
Log::info('Hello world! file: '.$this->file);
}
/**
* Determine the time at which the job should timeout.
*
* @return \DateTime
*/
public function retryUntil()
{
return now()->addSeconds(30);
}
}
?>
JobController.php:
<?php
namespace App\Http\Controllers;
use App\Jobs\ProcessDatabaseImport;
use Carbon\Carbon;
use Illuminate\Contracts\Queue\Job;
use Illuminate\Support\Facades\Queue;
class JobController extends Controller
{
/**
* Handle Queue Process
*/
public function processQueue()
{
ProcessDatabaseImport::dispatch('database/data/contacts.json')->delay(now()->addMinutes(2));
return view('home');
}
}
工作 table 已创建,php artisan queue:work
已 运行 宁。
现在,当我转到浏览器中的控制器操作时,"handle()" 中的代码直接执行了两次:
日志:
[2019-07-14 13:39:17] local.INFO: Hello world! file: B:\hellodialog\sollicitatieopdracht\database/data/contacts.json
[2019-07-14 13:39:18] local.INFO: Hello world! file: B:\hellodialog\sollicitatieopdracht\database/data/contacts.json
作业的数据库 table 始终为空。
在我的 .env 文件中:
DB_CONNECTION=mysql
QUEUE_CONNECTION=database
queue.php 配置文件:
return [
'default' => env('QUEUE_CONNECTION', 'database'),
'connections' => [
'sync' => [
'driver' => 'sync',
],
'database' => [
'driver' => 'database',
'table' => 'jobs',
'queue' => 'default',
'retry_after' => 90,
],
'beanstalkd' => [
'driver' => 'beanstalkd',
'host' => 'localhost',
'queue' => 'default',
'retry_after' => 90,
'block_for' => 0,
],
'sqs' => [
'driver' => 'sqs',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'prefix' => env('SQS_PREFIX', 'https://sqs.us-east-1.amazonaws.com/your-account-id'),
'queue' => env('SQS_QUEUE', 'your-queue-name'),
'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
],
'redis' => [
'driver' => 'redis',
'connection' => 'default',
'queue' => env('REDIS_QUEUE', 'default'),
'retry_after' => 90,
'block_for' => null,
],
],
'failed' => [
'database' => env('DB_CONNECTION', 'mysql'),
'table' => 'failed_jobs',
],
是不是少了什么?
更新: 当我在 queue.php
中更改默认值时,作业被添加到队列中
作业现在已两次添加到数据库中。我通过在浏览器中访问 URL 来 运行 这个脚本。
听起来你有两个问题。
运行 立即在浏览器中
至于 运行 立即,如果 Laravel 仍在使用默认的 sync
驱动程序,则可能会发生这种情况。检查您的 /config/queue.php
文件并确保正在使用 env()
属性。如果您的 .env
文件中没有设置队列驱动程序,则同步是默认的回退,但您也可以根据需要更改它。
'default' => env('QUEUE_CONNECTION', 'sync'),
如果一切正常,请尝试 运行 php artisan config:clear
清空配置缓存。默认的 sync
驱动程序可能仍被缓存。
或者,您可以尝试明确定义您希望使用的连接。
ProcessDatabaseImport::dispatch('database/data/contacts.json')
->onConnection('database')
->delay(now()->addMinutes(2));
运行两次
我不确定这一点,但是如果您从作业中删除 retryUntil()
方法会怎样?
另外,我在另一个帖子中发现了类似的问题,但我不知道是否相关。
Queued Laravel queued job runs twice after upgrading to Laravel 5.4
如果这没有帮助,我们可能需要更多关于您如何开始工作的信息。您只是访问 URL,还是调用此路由的机制可能是 运行 两次(例如,通过 Ajax)?
并且您可以将适用的 /config/queue.php
配置添加到您的问题中,因为上面提到的线程中有一些迹象表明您的 retry
和 timeout
时间可能会起作用。
我是 Laravel 的新手,并尝试创建我的第一个后台任务。
使用的文档:https://laravel.com/docs/master/queues
工作:(ProcessDatabaseImport.php)
<?php
namespace App\Jobs;
use App\Contact;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\File;
class ProcessDatabaseImport implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $file;
/**
* Create a new job instance.
*
* @param String $filePath
* @return void
*/
public function __construct($filePath)
{
// init File object "database/data/contacts.json"
$this->file = base_path($filePath);
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
Log::info('Hello world! file: '.$this->file);
}
/**
* Determine the time at which the job should timeout.
*
* @return \DateTime
*/
public function retryUntil()
{
return now()->addSeconds(30);
}
}
?>
JobController.php:
<?php
namespace App\Http\Controllers;
use App\Jobs\ProcessDatabaseImport;
use Carbon\Carbon;
use Illuminate\Contracts\Queue\Job;
use Illuminate\Support\Facades\Queue;
class JobController extends Controller
{
/**
* Handle Queue Process
*/
public function processQueue()
{
ProcessDatabaseImport::dispatch('database/data/contacts.json')->delay(now()->addMinutes(2));
return view('home');
}
}
工作 table 已创建,php artisan queue:work
已 运行 宁。
现在,当我转到浏览器中的控制器操作时,"handle()" 中的代码直接执行了两次:
日志:
[2019-07-14 13:39:17] local.INFO: Hello world! file: B:\hellodialog\sollicitatieopdracht\database/data/contacts.json
[2019-07-14 13:39:18] local.INFO: Hello world! file: B:\hellodialog\sollicitatieopdracht\database/data/contacts.json
作业的数据库 table 始终为空。
在我的 .env 文件中:
DB_CONNECTION=mysql
QUEUE_CONNECTION=database
queue.php 配置文件:
return [
'default' => env('QUEUE_CONNECTION', 'database'),
'connections' => [
'sync' => [
'driver' => 'sync',
],
'database' => [
'driver' => 'database',
'table' => 'jobs',
'queue' => 'default',
'retry_after' => 90,
],
'beanstalkd' => [
'driver' => 'beanstalkd',
'host' => 'localhost',
'queue' => 'default',
'retry_after' => 90,
'block_for' => 0,
],
'sqs' => [
'driver' => 'sqs',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'prefix' => env('SQS_PREFIX', 'https://sqs.us-east-1.amazonaws.com/your-account-id'),
'queue' => env('SQS_QUEUE', 'your-queue-name'),
'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
],
'redis' => [
'driver' => 'redis',
'connection' => 'default',
'queue' => env('REDIS_QUEUE', 'default'),
'retry_after' => 90,
'block_for' => null,
],
],
'failed' => [
'database' => env('DB_CONNECTION', 'mysql'),
'table' => 'failed_jobs',
],
是不是少了什么?
更新: 当我在 queue.php
中更改默认值时,作业被添加到队列中作业现在已两次添加到数据库中。我通过在浏览器中访问 URL 来 运行 这个脚本。
听起来你有两个问题。
运行 立即在浏览器中
至于 运行 立即,如果 Laravel 仍在使用默认的 sync
驱动程序,则可能会发生这种情况。检查您的 /config/queue.php
文件并确保正在使用 env()
属性。如果您的 .env
文件中没有设置队列驱动程序,则同步是默认的回退,但您也可以根据需要更改它。
'default' => env('QUEUE_CONNECTION', 'sync'),
如果一切正常,请尝试 运行 php artisan config:clear
清空配置缓存。默认的 sync
驱动程序可能仍被缓存。
或者,您可以尝试明确定义您希望使用的连接。
ProcessDatabaseImport::dispatch('database/data/contacts.json')
->onConnection('database')
->delay(now()->addMinutes(2));
运行两次
我不确定这一点,但是如果您从作业中删除 retryUntil()
方法会怎样?
另外,我在另一个帖子中发现了类似的问题,但我不知道是否相关。
Queued Laravel queued job runs twice after upgrading to Laravel 5.4
如果这没有帮助,我们可能需要更多关于您如何开始工作的信息。您只是访问 URL,还是调用此路由的机制可能是 运行 两次(例如,通过 Ajax)?
并且您可以将适用的 /config/queue.php
配置添加到您的问题中,因为上面提到的线程中有一些迹象表明您的 retry
和 timeout
时间可能会起作用。