【问题标题】:Queue in laravel with redis使用 redis 在 laravel 中排队
【发布时间】:2019-07-26 21:52:23
【问题描述】:

我在使用一个应用程序时遇到了问题,我在后端进行了大量的 api 调用。因此,应用程序会出现超时错误。

有人建议我应该使用队列。我曾尝试使用 redis 来做到这一点。该函数进入工作并使用处理程序,但我希望页面加载我给它的数据,而不需要来自 api 的数据,而 api-call 在后台进行。相反,它就像我没有使用队列时一样。我尝试按照教程执行此操作,但他们的做法并不完全相同,我无法对其进行调整,因此它对我有用。

有关我在工作中的工作的信息。我从 csv 中得到 cmets,然后我使用 cmets 中的数字调用 api,我得到一个 8-10 个字段的 json。我需要调用 api 大约 650 次,所以当我想将数据保存到数据库时需要很长时间。我一次使用一个插入来使用“缓存”,所以我不会两次执行相同的调用。

这是我调用工作的控制器。

class ImportController extends Controller
{
public function checkErrors(Request $request)
{
    $this->checkAgainstDocuments($csv_id);
    $supplierErrorIds=$this->checkSupplierErrors($parameters, $company , $csv_id);
    $timesheetErrors=TsData::whereIn('id', $supplierErrorIds)->sortable()->paginate(20);
    return view('show_errors', compact('timesheetErrors', 'company', 'csv_id'));
}

public function checkAgainstDocuments($csv_id)
{
    GetFromDocumentsAPI::dispatch($csv_id)->delay(now()->addMinutes(10));
}
}

这是我使用的工作:

class GetFromDocumentsAPI implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

protected $csv_id;
/**
 * Create a new job instance.
 *
 * @return void
 */
public function __construct($csv_id)
{
    //
    $this->csv_id=$csv_id;
}

/**
 * Execute the job.
 *
 * @return void
 */
public function handle()
{
    $comments = TsData::select('id', 'comments')->where('csv_id', $this->csv_id)->get()->toArray();

    $commentIDs = array();

    foreach ($comments as $comment) {
        preg_match_all('/(\d{5,})/', $comment['comments'], $out, PREG_PATTERN_ORDER);
        foreach ($out as $item) {
            $commentIDs[$comment['id']] = $item;
        }
    }

    $commentIDs = array_filter($commentIDs);

    $apiKey=config('app.apiKey');

    $documentsResponse = array();

    Issue::truncate();

    $arrayTest=[];

    foreach ($commentIDs as $key => $commentID) {
        foreach ($commentID as $item) {
            $issue = Issue::where('id', $item)->first();

            if ($issue === null) {
                try {
                    $url = file_get_contents('https://documents.calibrate.be/issues/' . $item . '.json?key=' . $apiKey);
                    $json = json_decode($url, true);
                } catch (Exception $e) {
                    echo 'Caught exception: ', $e->getMessage(), "\n";
                }



$issue = Issue::Create(['id'=>$item, 'name'=>$json['issue']['subject'], 'projectId'=>$json['issue']['project']['id'], 'priority'=>$json['issue']['priority']['id']]);

        }
    }
}

}

config/queue.php

'default' => env('QUEUE_DRIVER', 'redis'),

'redis' => [
        'driver' => 'redis',
        'connection' => 'default',
        'queue' => env('REDIS_QUEUE', 'default'),
        'retry_after' => 90,
        'block_for' => null,
    ],

【问题讨论】:

  • QUEUE_CONNECTION 使用了同步吗?签到config/queue.php
  • 'default' => env('QUEUE_DRIVER', 'redis'),
  • .env 文件中相同的值呢?
  • BROADCAST_DRIVER=log QUEUE_CONNECTION=sync SESSION_LIFETIME=120 CACHE_DRIVER=redis SESSION_DRIVER=redis QUEUE_DRIVER=redis
  • QUEUE_CONNECTION 应该是redis或数据库或任何你想使用的!

标签: php laravel redis queue


【解决方案1】:

默认情况下,Laravel 使用 sync 驱动来处理队列。

确保将QUEUE_CONNECTION 配置变量设置为databaseredis 或任何其他服务。 在.env文件和config/queue.php文件中都可以设置。

要使用database,请执行

  1. 运行php artisan queue:tablephp artisan migrate。这将创建运行所需的所有表。
  2. 确保队列工作程序在后台运行。您可以通过运行php artisan queue:work 在控制台中执行此操作

要使用redis,请执行

  1. 安装sudo apt-get install redis-server并启动redis服务器$ sudo systemctl enable redis-server.service。 (适用于基于 Linux 的系统)
  2. .envconfig/queue.php中配置和设置redis特定变量

附:进行更改后运行php artisan config:clear 命令,以便将更改反映在缓存中。

【讨论】:

  • 我使用的是redis docker容器所以我觉得有点不一样
  • 对于历史背景:QUEUE_DRIVER 最初在 Laravel 配置中用于定义您要使用的队列,QUEUE_CONNECTION 是后继者。
【解决方案2】:

在 Laravel 5.8 中使用 Redis 作为异步,在 .env 文件集中 QUEUE_CONNECTION=redis

【讨论】:

    猜你喜欢
    • 2021-02-16
    • 2014-12-04
    • 1970-01-01
    • 2022-11-02
    • 2017-03-15
    • 2021-09-16
    • 2019-01-02
    • 1970-01-01
    • 2019-08-22
    相关资源
    最近更新 更多