如何随意延迟从Laravel的其他工作中派遣工作?

时间:2016-12-01 09:37:30

标签: laravel queue laravel-5.2 jobs

我有两个工作,第一个是向司机推送通知,他们必须选择是否接受行程。第二项工作是将行程授予感兴趣的司机。我想在推送通知后1分钟延迟发送第二份工作。

下面是第一个推送通知的工作:

class NewBookingNotification extends Job implements ShouldQueue, BookingAwardCallback
{
    use InteractsWithQueue, SerializesModels, DispatchesJobs;

    protected $booking;
    protected $admin;

    public function __construct(Booking $booking, User $admin)
    {
        $this->booking = $booking;
        $this->admin = $admin;
    }

    public function handle()
    {
        Log::info('new booking started ' . $this->booking->id);

        $delay = $this->admin->notification_expire_time; // 60 seconds

        echo "handle new booking \n";

        $awardJob = new AwardNotification($this->booking, $this->admin, $this);
        $driversCount = TempBookingDriver::where(['booking_id' => $this->booking->id, 'awarded' => 1])->get()->count('*');

        if ($driversCount < $this->booking->vehicles_quantity) {
            $notifiedDriversCount = NotificationsHelper::pushNewBooking($this->booking, $this->admin);
            if ($notifiedDriversCount > 0) {
                $this->award($delay, $awardJob);
            } else {
                Utils::call($this->admin, $this->booking->id, Utils::getClientName($this->booking));
            }
        } else {
            $this->award($delay, $awardJob);
        }

        if ($this->attempts() > 1)
            Utils::call($this->admin, $this->booking->id, Utils::getClientName($this->booking));
    }

    private function award($delay, $job)
    {
        // queue award job
        Queue::later($delay, $job);
    }

    public function releaseNewBookingJob()
    {
        $this->release();
        Log::info('new booking job released (Log)');
    }

    public function deleteNewBookingJob()
    {
        $this->delete();
        Log::info('new booking job stopped (Log)');
    }

    public function queue($queue, $command)
    {
        $id = $queue->pushOn('default', $command);

        $bookingJob = new BookingJob();
        $bookingJob->booking_id = $this->booking->id;
        $bookingJob->job_id = $id;
        $bookingJob->save();
    }
}

获得驾驶奖的第二份工作:

class AwardNotification extends Job implements ShouldQueue
{
    use InteractsWithQueue, SerializesModels, DispatchesJobs;

    protected $callback;
    protected $booking;
    protected $admin;

    public function __construct(Booking $booking, User $admin, BookingAwardCallback $callback)
    {
        $this->booking = $booking;
        $this->admin = $admin;
        $this->callback = $callback;
    }

    public function handle()
    {
        echo "handle award booking \n";
        $awardedDrivers = NotificationsHelper::pushNewAward($this->booking, $this->admin);
        if ($awardedDrivers > 0) {
            $this->callback->deleteNewBookingJob();
        } else {
            $this->callback->releaseNewBookingJob();
        }
    }
}

现在,问题在于当NewBookingNotification作业尝试对AwardNotification作业进行排队时,它会“失败”并自行释放。当我评论award时,它不会释放自己。有没有其他方式派遣第二份工作?

0 个答案:

没有答案