How to use Laravel's queue mechanism? What scenarios require queues?

Posted by millsy007 on Fri, 03 Dec 2021 14:53:54 +0100

previously on

I have been in the community for several years and haven't written an article for a long time. Today, I happened to rub the hot spot and write about how to use Laravel's queue mechanism? What scenarios need to use queues for my understanding

Why?

First, we need to know why we use queues and what happens if we don't use queues! What are the advantages and disadvantages

We can give several simple scenarios.

Mail sending

What problems do you usually face when sending mail??

  • Send slowly
  • fail in send
  • The sending frequency is too high, rejected by the service provider, or entered the dustbin

What are the benefits of using queues

  • Improve client response

    When sending, we do not process it immediately, but throw it to the server, and the queue is managed and scheduled. You can customize the option to send immediately or delay according to the configuration

  • Improve fault tolerance

    In the process of sending, we may encounter that the target is rejected. For example, most people will encounter admin@qq.comn Send the scenario of error 502.
    In this scenario, we can understand this scenario as an event. In the process of sending mail, we can trigger and build the following events

    • fail in send

    • Mail record warehousing

    • Code exception

    • Mail sending success callback

    • Sending failed. Try again

      Sending this email may lead to multiple time-consuming tasks. In fact, we can also build multiple queue services. Each queue manages its own things and decouples them well

      Through the Laravel queue, you can set immediate sending, delayed sending and retry sending

  • Controllable transmission frequency

    Developers who have used bulk mail will inevitably encounter a problem, that is, if we directly send bulk mail, that is, send a large number of mail at the same time. Then the mail service provider is likely to reject our mail or enter the mail into the dustbin and be identified as an advertisement
    Then, delayed sending is used here. We can configure the frequency reasonably according to the known mail waiting for delivery in the current queue service, or switch the mail configuration to achieve controllable frequency.

    For example, set a configuration to send 10 times a minute, and so on.
    Similarly, we can decouple configuration, frequency control and transmission control

other

Of course, we will use it in many cases

  • Download excel from server
  • Server side asynchronous multitasking big data processing
  • Error message processing

How to use Laravel queues

Here is just a list of the general direction of use and how to use it better. The code may not be able to run, mainly understanding logic
We use Redis as the driver here

The driver is set to Redis

> .env
QUEUE_CONNECTION=redis
> stay config/queue.php Can be found in

Quickly create queues and post tasks

# Create task
php artisan make:job ProcessPodcast

Automatically generate app/Jobs/EmailJob.php

class EmailJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $data;

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

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $service = new EmailService();

        // ... check for currently available mailers
        // Here you can customize it. In this method, you can get the currently available configuration according to your own configuration
        $mailer = $service->getMailer();

        // ... get the data currently to be sent
        $data = $this->data;

        $service->send($mailer, $data);
    }
}

Some common operations

These operations can be found in the documentation

Call send

# Delay 2 minutes to send
# The Crontab package is used here (but it comes with Laravel)
EmailJob::dispatch()->delay(now()->addMinutes(2));

# Send now (not queued)
EmailJob::dispatchNow();

The default queue used here is the default queue, which can be modified to the specified queue service

public function __construct(array $data)
{
    # Using emailQueue
    $this->onQueue('emailQueue');
    $this->data = $data;
}

Set the number of retries in case of failure

# Retry 5 times
public $tries = 5;

Set timeout

/**
* Determine how long the task should time out
*
* @return \DateTime
*/
public function retryUntil()
{
    return now()->addMinutes(10);
}

Start our queue

If onQueue is not configured, it can be configured without the - queue parameter

php artisan queue:work --queue=emailQueue

Decoupling with Events

Laravel Event is also implemented through queues

# Create Event
php artisan make:event FailEvent

class FailEvent
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    protected $data;
    protected $tag;

    /**
     * @param array $data Delivered data
     * @param string $tag Things to operate
     */
    public function __construct(array $data, string $tag = 'system')
    {
        $this->data = $data;
        $this->tag = $tag;
    }
}

# Create listener 
php artisan make:listener FailListener
class FailListener
{
    /**
    * Handle the event.
    * 
    * @param  object  $event
    * @return void
    */
    public function handle(FailEvent $event)
    {
        $this->{$event->tag}($event->data);
    }

    /**
     * Handling system exceptions
     * DateTime: 2021/12/3 11:02 morning
     * @param array $data
     */
    public function system(array $data)
    {

    }

    /**
    * Mail handling exception
    * DateTime: 2021/12/3 11:02 morning
    */
    public function email()
    {

    }

}

# app/Providers/EventServiceProvider.php
protected $listen = [
    FailEvent::class => [
        FailListener::class,
    ],
]

# Delivery
event(new FailEvent(['error' = 'Abnormal information'], 'email'));

other

In fact, most of Laravel helped me implement the whole process. You can try to use redis to implement a controllable queue. Master redis related data types
Here is a brief list of data types used in the above modes in Redis

  • List

    It can be used to complete the queue function of out of stack and into stack

  • Hash

    It can be used to store serialized events or jobs__ For the data passed in by construct, try not to serialize the whole class

    It can also store Mailer data

  • Sorted Set

    You can set the time to the score in the Sorted Set. By sorting the scores, you can find the queue task we want to execute recently

Of course, there are many uses of Redis, which can meet your own needs.

There is no perfect solution in the world, only the one that is most suitable for you. If you encounter problems at work, try to learn to draw inferences from one instance, and reasonably use various tools and design solutions to achieve them.
The code is only a microcosm of the final. We should learn to understand each language and each framework. It is only the implementation of a scheme. It is invincible to master it

Reference articles

Laravel queue document

Topics: Laravel