使用Queue进行邮件但laravel 5.4似乎没有响应

时间:2017-05-17 23:18:15

标签: laravel-5.4

我第一次使用队列而且我似乎没有让它工作,laravel似乎没有任何错误。

我正在尝试对用户注册的邮件进行排队,即应立即将用户重定向到仪表板并将电子邮件排队。我怎么知道队列是否工作?在点击注册时我必须等待8秒才能看到仪表板,我在注册时收到电子邮件,但队列似乎失败了。

我在注册时没有在mysql作业表上看到任何队列作业。

以下是我的设置:

使用以下命令创建的作业和作业失败表:

@RunWith(SpringRunner.class)
@ContextConfiguration
public class MockMessageHandlerTests {

    @Autowired
    private SubscribableChannel fileNotFoundChannel;

    @Autowired
    private AbstractEndpoint fileNotFoundEndpoint;

    @Test
    @SuppressWarnings("unchecked")
    public void testMockMessageHandler() {
        this.fileNotFoundEndpoint.stop();

        MessageHandler mockMessageHandler = mock(MessageHandler.class);

        this.fileNotFoundChannel.subscribe(mockMessageHandler);

        GenericMessage<String> message = new GenericMessage<>("test");
        this.fileNotFoundChannel.send(message);

        ArgumentCaptor<Message<?>> messageArgumentCaptor = ArgumentCaptor.forClass(Message.class);

        verify(mockMessageHandler).handleMessage(messageArgumentCaptor.capture());

        assertSame(message, messageArgumentCaptor.getValue());
    }

    @Configuration
    @EnableIntegration
    public static class Config {

        @Bean
        public IntegrationFlow fileNotFoundFlow() {
            return IntegrationFlows.from("fileNotFoundChannel")
                    .<Object>handle((payload, headers) -> {
                        System.out.println(payload);
                        return payload;
                    }, e -> e.id("fileNotFoundEndpoint"))
                    .channel("fileFoundChannel")
                    .get();
        }

    }

}

.ENV

php artisan queue:table
php artisan queue:failed-table
php artisan migrate

RegisterController create()函数

QUEUE_DRIVER=database MAIL_DRIVER=smtp MAIL_HOST=smtp.gmail.com MAIL_PORT=587 MAIL_USERNAME=dummy@gmail.com MAIL_PASSWORD=password MAIL_ENCRYPTION=tls MAIL_FROM_ADDRESS=hello@example.com MAIL_FROM_NAME="App name"

之前的创建函数结束时
return $user;

作业文件:NewRegisteredUser

dispatch(new NewRegisteredUser($user));

邮件文件:RegisteredUserWelcome

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Mail;
use App\User;
use App\Mail\RegisteredUserWelcome;

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

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

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $email = new RegisteredUserWelcome($this->user);
        Mail::to($this->user->email)->queue($email);
    }
}

我在这里遗漏了什么吗?我没有收到错误并且发送了邮件,但在mysql中没有观察到队列作业,注册时间很长。

我也很困惑:https://laravel.com/docs/5.4/queues如何适应这个'script-loader'当我们已经在邮件排队时,为什么我们需要单独排队?

我应该使用什么以及如何使用?我需要做的就是减少用户的等待时间并在场景后发送邮件。此外,我在某些情况下发送多封电子邮件。我想如果我排队多封电子邮件的工作,这会有所帮助。

1 个答案:

答案 0 :(得分:6)

我使用markdown作为邮件模板

我让Queues工作,输出显示在php artisan queue:listen

问题是.env文件没有明确缓存。 php artisan config:clear让它发挥作用。通过简单地延迟工作,可以使队列更有效率。

我将工作延迟了60秒(1分钟),事情确实很顺利。

我将发布我必须创建的所有代码和文件,以使其正常工作。

设置.env文件

QUEUE_DRIVER=database

MAIL_DRIVER=smtp

MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=dummy@gmail.com
MAIL_PASSWORD=password
MAIL_ENCRYPTION="tls or ssl"  (double quotes not required)
MAIL_FROM_ADDRESS="email address to be shown to email receiver" (double quotes not required)
MAIL_FROM_NAME="App Name" (double quotes not required only if you have blank space between the name)

这行代码确保考虑.env文件中的新值

php artisan config:clear

在我的Controller文件中

use Mail;
use App\Jobs\NewprofileCreated;
use App\Mail\ProfileCreated;

控制器代码

dispatch((new NewprofileCreated($user))->delay(60));

使用以下命令

创建的两个文件
php artisan make:job NewprofileCreated
php artisan make:mail ProfileCreated

NewprofileCreated作业文件

use Mail;
use App\User;
use App\Mail\ProfileCreated;

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

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

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
         $email = new ProfileCreated($this->user);
        Mail::to($this->user->email)->queue($email);
    }
}

ProfileCreated邮件文件

class ProfileCreated extends Mailable
{
    use Queueable, SerializesModels;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->markdown('emails.profile_created');
    }
}

profile_created markdown template(文件夹:view / emails / profile_created)

@component('mail::message')

<h1>Your new profile is created</h1>

You have received this email because your profile was created for {{ config('app.name') }}


Thanks,<br>
{{ config('app.name') }}
@endcomponent