【问题标题】:Laravel Job executed immediately in browser instead of running in a background queueLaravel Job 立即在浏览器中执行,而不是在后台队列中运行
【发布时间】:2019-11-23 11:08:45
【问题描述】:

我是 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');
    }
}

已创建作业表,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  

作业的数据库表始终为空。

在我的 .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 来运行此脚本。

【问题讨论】:

    标签: php laravel queue jobs laravel-5.8


    【解决方案1】:

    听起来你有两个问题。

    立即在浏览器中运行

    至于立即运行,如果 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));
    

    运行两次

    我不确定这一点,但是如果你从 Job 中删除 retryUntil() 方法会发生什么?

    另外,我在另一个帖子中发现了类似的问题,但我不知道它是否相关。
    Queued Laravel queued job runs twice after upgrading to Laravel 5.4

    如果这没有帮助,我们可能需要有关您如何开始这项工作的更多信息。您只是访问一个 URL,还是调用此路由的机制可能会运行两次(例如,通过 Ajax)?

    并且您可以将适用的/config/queue.php 配置添加到您的问题中,因为上面提到的线程中有一些迹象表明您的retrytimeout 时间可能会起作用。

    【讨论】:

    • 感谢您的回答。更改 queue.php 中的“默认”行以使用数据库连接就可以了。我会将 queue.php 添加到我的问题中。当我在浏览器中访问 URL 时,它仍然被触发两次。
    • 除了我之前的评论:我为此删除了 retryUntil() 函数。
    • 哈,正要问。你是如何运行队列工作者的——你是否设置了主管,你是从 CLI 手动启动它,它是由调度程序触发的吗?
    • 其实,我想这应该不重要,因为调度首先发生,只是想通盘考虑。
    • 我正在使用 CLI 在我的本地主机(Windows 机器)上手动运行它
    猜你喜欢
    • 1970-01-01
    • 2017-12-02
    • 2019-05-19
    • 1970-01-01
    • 2016-04-04
    • 1970-01-01
    • 2015-10-18
    • 2015-06-20
    • 2022-08-20
    相关资源
    最近更新 更多