【问题标题】:Getting Job Id in a Laravel queue with Database driver使用数据库驱动程序在 Laravel 队列中获取作业 ID
【发布时间】:2021-01-19 10:50:34
【问题描述】:

我有一个从控制器创建的 laravel 作业,有时我想删除这些作业,因为时间会重新安排 这是一个通知作业,在上课前一小时发送通知,如果课程重新安排,我需要删除作业并插入新作业

我的控制器代码如下

$job = (new OnlineClassRemainderJob($remainder_data))->delay($value);
$id = dispatch($job);
array_push($job_ids, $id);

Job类如下图

<?php

namespace App\Jobs;

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

class OnlineClassRemainderJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $details;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($details)
{
$this->details = $details;

}

/**
* Execute the job.
*
* @return void
*/
public function handle()
{

Mail::to($this->details['email'])
->send(new OnlineClassRemainderMail($this->details));

}
}

请说明我如何获取作业 ID,以便我可以将其保存在数据库中,并且在重新安排的情况下,我可以删除与该类相关的所有作业 ID,然后发送新的安排

我可以在这里布置场景

例如,我正在安排课程

我在 1 月 20 日至 1 月 25 日每天上午 10 点上课,我正在安排剩余邮件发送作业,该作业将在上课前 1 小时触发

但在某些情况下,课程将被重新安排,为此我需要重新安排剩余时间或删除并重新分配作业

【问题讨论】:

    标签: php laravel queue


    【解决方案1】:

    你需要使用这个函数$this-&gt;job-&gt;getJobId();从job中重新运行job id

    class OnlineClassRemainderJob implements ShouldQueue
    {
         use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    
        /**
         * Execute the job.
         *
         * @return void
         */
        public function handle()
        {
           Mail::to($this->details['email'])
                   ->send(new OnlineClassRemainderMail($this->details));
           retrun $this->job->getJobId();
        }
    }
    

    然后

    $job = (new OnlineClassRemainderJob($remainder_data))->delay($value);
    $id = dispatch($job);
    array_push($job_ids, $id);
    

    参考链接https://laravel.com/api/8.x/Illuminate/Contracts/Queue/Job.html#method_getJobId

    【讨论】:

    • 我按照你说的尝试过,但是 $id 包含我传递给工作的 $remainder_data 我没有在工作表中获得工作的 ID
    • @AsishAntony 是 uuid 吗?在数据库中?
    • 我想在数据库中获取作业行的 ID,以便我可以从作业表中删除作业行
    • @AsishAntony 如果作业完成则自动删除
    • 请检查我现在在问题中给出的场景
    猜你喜欢
    • 2017-02-10
    • 2016-03-04
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 2018-01-19
    • 2016-12-30
    • 2017-11-06
    • 2017-03-12
    相关资源
    最近更新 更多