Search code examples
laravelqueuejobs

Cancel job in queue (database driver) with late option


I have a system with booking for a house renting with contract (all terms and conditions).

I put in a queue with job, an email with all terms and conditions (date etc..) which is sending when the booking is created and another with delay, to notify the customer 2 days before the stay.

But if I change the booking info (for example the date), I need to cancel the job in queue.

How can I do this ? I use the Database driver.

There is the line in BookingController :

dispatch( new ReminderMailJob( $booking, $booking->customer ) )->delay( Carbon::parse( $booking->ends_at )->subDays( 3 ) );

There is a screen of my database "jobs" table

enter image description here

How can I select one of this line and delete it ?

Thanks in advance


Solution

  • Use Task Scheduling

    Make an artisan command like

    class SendReminers extends Command
    {
        protected $signature = 'booking:send-reminders';
    
        protected $description = 'Send a reminders email to a users';
    
        public function handle()
        {
            Booking::where(*your query*)
              ->each(fn($booking) => ReminderMailJob::dispatch($booking, $booking->customer)->now());
        }
    }
    

    Than schedule task for, example, five minutes

    $schedule->command('booking:send-reminders')->everyFiveMinutes();
    

    And now reminders will be sent only for bookings which are relevant at task execution moment.