【问题标题】:Running Task scheduler on Queues Laravel在队列 Laravel 上运行任务调度程序
【发布时间】:2017-10-26 23:59:09
【问题描述】:

//邮件验证文件

class EmailVerification extends Mailable
{

    use Queueable, SerializesModels;

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

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('frontend.email.activation')->with([
            'email_token' => $this->user->email_token,
        ]);
    }
}

//发送验证邮件

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

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

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

我目前正在做这个简单的 laravel 项目。我正在尝试利用队列来实现用户电子邮件激活功能。当我使用 php artisan queue:work 运行它时它工作正常。我现在尝试创建一个任务计划程序,每五分钟执行一次

//这里的代码

protected function schedule(Schedule $schedule)
{
    $schedule->job(new SendVerificationEmail($user))->everyFiveMinutes();
}

但它返回未定义的变量。上面有没有错误,或者有没有更好的方法让Queues自动运行?

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    请阅读:Laravel 5.5 Documentation - Queues#dispatching-jobs

    你不能安排这种工作,你应该派遣它。

    【讨论】:

    • 是的,我确实做到了 ** public function register(Request $request) { $this->validator($request->all())->validate();事件(新注册($user = $this->create($request->all())));调度(新的 SendVerificationEmail($user));返回视图('frontend.others.verification'); }**
    • 基本上计划的作业不能有请求相关的数据。这是因为计划的作业是从您的应用程序和外部进程异步执行的。另一方面,您可以在 Laravel 应用程序中的请求流中进行调度,因此您可以通过一些 $user
    • 好的,谢谢.. 那么你认为我可以如何最好地自动运行队列或在像 heroku 这样的实时服务器上运行队列
    猜你喜欢
    • 2019-01-01
    • 2018-04-16
    • 2019-02-15
    • 2017-11-22
    • 2021-10-09
    • 2021-04-16
    • 2020-11-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多