Laravel some issues to be aware of using Message Queuing _php instances

Source: Internet
Author: User
This article mainly introduces you to the LaravelSome issues to be aware of using Message Queuing are LaravelThe sample code introduces the very detailed, the study of Everybody LaravelWith a certain reference learning value, the need for friends below with the small series to see together.

Objective

Message Queuing is an essential module for large Web projects, and Message Queuing can solve problems such as large concurrency and multiple language communication interfaces. In the case of large concurrency, it is possible to encapsulate time-consuming tasks or tasks that cannot be simultaneously massively parallel to the message queue, and the handlers are constantly extracting messages from the message queue and processing them, so that the buffering of message queues can make them no longer block in large concurrency. You can also add multiple processing tasks to get messages from the message queue for processing if the performance is not sufficient. For example, the operation of the database, when the database read, write operations there will be a lock table and other issues, read the problem can be cached and other solutions, write the problem needs message queue to solve. Moreover, in large-scale Web project development, it is not possible to implement a language in many cases, it is necessary to play the advantages of different languages, such as PHP, although in the theoretical sense it can do all the things in web development, but some problems with it to solve the efficiency will be very low, such as real-time socket connection and distributed transaction processing.

Using Laravel's Message Queuing to handle asynchronous tasks, Redis acts as a queue database, Supervisor monitoring scripts are abnormally interrupted and automatically restarted, which is the standard process for Laravel processing queue tasks, but there may be a variety of problems in practice, in order to ensure system reliability, There are several issues to note.

One, execution failure retry number settings

Be sure to set the number of failed retry attempts for the task, avoid infinite failed retries, Laravel default to the failed task table, or write your own execution failure post-processing logic.


PHP Artisan queue:work Redis--tries=3


You need to create a data table by executing the following command first:


PHP Artisan queue:failed-tablephp Artisan Migrate


Second, the handling of abnormal procedures

Sometimes the program execution process will be abnormal, such as relying on other interfaces, request HTTP interface timeout, and so on, if you do not catch the exception, then the current queue will be interrupted can not continue to run, such as to 10,000 users to push content, need to rely on interface push, if the middle of the request hangs, will affect the subsequent push.

The exception here refers to the exception that occurs during the execution of the program, not the permanent process hangs, the program exception does not necessarily lead to the permanent process interruption, and the process is interrupted Supervisor monitoring and restart.

such as capturing exception code snippets:


try {$r = $client->request (' POST ', ', [' query ' = ' = '  client_name ' = '   filemail '  ,   ' Client_ Version ' = ' 1.0 ',   ' client_sequence ' + 0,   ' uid '    = ' 692934013,//119481237   ' r '    = 1508312484,  ],  ' body ' = \guzzlehttp\json_encode ($body),]); $result = $r->getbody ()->getcontents (); $result = Json_decode ($result, true); if ($result [' result '] = = 0) {  info ("SendMail fail:". Json_encode ($result));  $this->pushlog ($task [' id '], $task [' mail_id '], implode (', ', $userIds), Json_encode ($result), 0); } else {  log::warning ("SendMail fail:". Json_encode ($result));  $this->pushlog ($task [' id '], $task [' mail_id '], implode (', ', $userIds), Json_encode ($result), $result [' result ']); }} catch (Requestexception $e) {log::warning (' requestexception '. $e->getmessage ())} catch (Exception $e) {Log::emer Gency (' Exception '. $e->getmessage ());}


Third, modify the code remember to restart Supervisor

Finally, modify the process of processing the queue, remember to restart Supervisor, otherwise the script will not take effect.

Laravel data structure to Redis write queue

Queues are stored in the list type,

The value content is as follows:


{"Job": "Illuminate\\queue\\callqueuedhandler@call", "data": {"CommandName": "App\\jobs\\sendfile", "command": "O:17 : \ "app\\jobs\\sendfile\": 5:{s:23:\ "\u0000app\\jobs\\sendfile\u0000task\"; a:8:{s:5:\ "title\"; s:4:\ "1111\"; s:4:\ "Note\", s:2:\ "11\", s:6:\ "reward\"; s:0:\ "\"; s:7:\ "mail_id\"; s:5:\ "66681\"; s:4:\ "nums\"; i:20;s:8:\ "uid_file\"; s : 33:\ "uidfile\/file-66681-1513058185.txt\", s:5:\ "gcids\"; s:40:\ "1b9dd95645aae8119f7da9b9ff738d52bc8a1bd5\"; s:2 : \ "id\"; i:29;} S:6:\ "\u0000*\u0000job\"; N;s:10:\ "connection\"; N;s:5:\ "Queue\", s:8:\ "sendfile\"; s:5:\ "delay\"; N;} "}," id ":" L0mjsuthbxm4tgijnuh13km9n8diperk "," Attempts ": 1}


Contains the number of failed retries, the queue identity, the class that handles the queue, and the data for the queue, and so on.

Reference links

Laravel Official Document Queue queues:

Https://laravel.com/docs/5.5/queues

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.