簡體   English   中英

使用數據庫驅動程序在 Laravel 隊列中獲取作業 ID

[英]Getting Job Id in a Laravel queue with Database driver

I have a laravel job which I create from a controller and some times i want to delete those jobs as the timings will be rescheduled It is a notification job which sends notification one hour before a class and if the class is rescheduled i need to delete the工作並插入新工作

我的 controller 代碼如下

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

作業 class 如下圖所示

<?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,以便我可以將其保存在數據庫中,如果需要重新安排,我可以刪除與該 class 相關的所有作業 ID,然后發送新的時間表

我可以在這里布置場景

例如,我正在安排課程

從 1 月 20 日到 1 月 25 日的每天上午 10 點,我都有一個 class,我正在安排一個剩余郵件發送作業,該作業將在 class 前 1 小時觸發

但在某些情況下,課程將被重新安排,為此我需要重新安排剩余時間或刪除並重新分配工作

您需要使用此 function $this->job->getJobId();

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,例如在執行之前將其出列。

$log = UpdateLog::create();
UpdateProduct::dispatch($log)->delay(now()->addMinutes(10));
$regex = 'UpdateLog.*?id\\\\";i:'.$log->id.';';
//delete or get job before start
DB::table('jobs')->where('payload','REGEXP',$regex)->delete();

我的工作示例

//...
class UpdateProduct implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * @var UpdateLog
     */
    protected $log;

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM