队列已创建但从未执行

问题描述 投票:0回答:1

我正在尝试在Lumen API的背景下触发推送通知和电子邮件。我创建了创建两个队列的WarningUser类:

<?php

namespace App\Utils;

use App\Jobs\ProcessNotification;
use App\Mail\AllMail;
use Illuminate\Support\Facades\Mail;

class WarningUser
{

  public static function send($user, $message, $url, $data = [])
  {
    $url = env('APP_URL_FRONT') . $url;
    dispatch(new ProcessNotification($user, $data, $message, $url));

    $emailData = EmailTexts::texts('pt', $data)[$message];
    Mail::to($user->email)->queue(new AllMail($emailData['title'], $emailData['content']));

    return true;
  }
}

首先,我们有ProcessNotification作业,该作业连接到Firebase并发送通知:

<?php

namespace App\Jobs;

use Kreait\Firebase\Factory;
use Kreait\Firebase\Messaging\Notification;
use Kreait\Firebase\Messaging\CloudMessage;

class ProcessNotification extends Job
{
    protected $user = null;
    protected $data = null;
    protected $message = null;
    protected $url = null;

    public function __construct($user, $data, $message, $url)
    {
        $this->user = $user;
        $this->data = $data;
        $this->message = $message;
        $this->url = $url;
    }

    public function handle()
    {
        if (\is_string($this->user->token) and $this->user->token !== '') {
            $messaging = (new Factory())
                ->withServiceAccount(__DIR__.'/../../private-key.json')
                ->createMessaging();

            $notificationData = NotificationTexts::texts('pt', $this->data)[$this->message];
            $messageAlert = CloudMessage::withTarget('token', $this->user->token)
                ->withNotification(Notification::create($notificationData['title'], $notificationData['content']))
            ->withData([ 'url' => $this->url ]);

            $messaging->send($messageAlert);
        }
    }
}

最后是发送简单电子邮件的AllMail:

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class AllMail extends Mailable implements ShouldQueue
{
    use Queueable, SerializesModels;
    protected $title;
    protected $body;

    public function __construct($title, $body)
    {
        $this->title = $title;
        $this->body = $body;
    }

    public function build()
    {

        return $this
            ->subject($this->title)
            ->view('email')
        ->with([
            'title' => $this->title,
            'body' => $this->body
        ]);
    }
}

我已经在不使用队列的情况下测试了这两个代码,并且它们都可以工作,但是当我放入队列时,它就停止了工作。进程记录在我的数据库(mongodb)中:

enter image description here

但是队列从未处理过,我尝试执行php artisan queue: workphp artisan queue: listen,但两种情况都不起作用。

从未尝试过处理队列,也不会进入failed_jobs

我的config / queue.php如下。我的.env是QUEUE_CONNECTION = database

<?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' => [
        'driver' => env('QUEUE_FAILED_DRIVER', 'database'),
        'database' => env('DB_CONNECTION', 'mongodb'),
        'table' => 'failed_jobs',
    ],
];

有人可以帮我吗?我不再知道错误可能是什么。

PS:永远不会显示浏览器或控制台错误

mongodb queue lumen
1个回答
0
投票

我设法解决了,对于Mongo,我们需要使用更多设置:

queue.php中的连接必须是:

'connections' => [
    'database' => [
        'driver' => 'mongodb',
        'table'  => 'jobs',
        'queue'  => 'default',
        'expire' => 60,
    ],
...

并且我们需要注册与mongo兼容的软件包提供程序:

Jenssegers\Mongodb\MongodbQueueServiceProvider::class,

© www.soinside.com 2019 - 2024. All rights reserved.