C
C
chelkaz2016-03-29 23:43:38
Laravel
chelkaz, 2016-03-29 23:43:38

How to check if an event is in the queue?

I created an event and a listener according to the documentation, at the time of creating a new entry in the database. Everything works and notifications come to the mail. But how to check that it is not executed together with the user at the time of adding, but deferred?
Made it this way:
Listener

namespace App\Listeners;

use App\Events\ElementWasAdded;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Mail;

class EmailElementConfirmation implements ShouldQueue
{
    use InteractsWithQueue;

    public function __construct()
    {
        //
    }

    public function handle(ElementWasAdded $event)
    {
        //
        $element = $event->element;
        $section = $event->section;
        Mail::send('emails.ElementWasAdded', array('element' => $element, 'section' => $section), function ($message) {
            $message->from('[email protected]', 'Заголовок письма');
            $message->to('[email protected]');
            $message->subject('Добавлен элемент');
        });
    }
}

Event
namespace App\Events;

use App\Events\Event;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

class ElementWasAdded extends Event
{
    use SerializesModels;

    public $element;
    public $section;
    public function __construct($element, $section)
    {
        //
        $this->element = $element;
        $this->section = $section;
    }

    public function broadcastOn()
    {
        return [];
    }
}

And in the controller
if($table->save()) {
                $element_id = $table->id;
                $section_id = "reviews_section";
                Event::fire(new ElementWasAdded($element_id, $section_id));
            }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Silm, 2016-03-30
@Silm

Creating an email sending event with a delay of 5 seconds:

Mail::later(5, 'emails.welcome', $data, function($message)
{
  $message->to('[email protected]', 'John Smith')->subject('Welcome!');
});

What driver are you using? If, for example. database, you will see how events appear in the table, then disappear. How are you handling the queue? If you run the listener, it writes to the console about the completed task.

D
Dmitry Evgrafovich, 2016-03-31
@Tantacula

Stop the queue listener, run the script that adds the task to the queue, check the jobs table for the existence of the task (if you are using a database for the task queue).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question