Note: This is an example of MixPHP V1
Sending emails is a very common requirement. Since the operation of sending emails is generally time-consuming, we generally use asynchronous processing to improve user experience. , asynchronously usually we use message queue to achieve.
Due to the lack of multi-process development capabilities, the traditional MVC framework usually uses the same script to be executed multiple times to generate multiple processes. Mixphp encapsulates TaskExecutor specifically for multi-process development, and users can develop it very simply. A fully functional and highly available multi-process application.
Recommended: "PHP Video Tutorial"
The following demonstrates the development process of an asynchronous email sending system, involving knowledge points:
- Asynchronous
- Message Queue
- Multiple processes
- Daemon process
How to use message queue to implement asynchronous
PHP usually uses message queue It is implemented using middleware. Commonly used message middleware are:
- redis
- rabbitmq
- kafka
This time we Choose redis to implement asynchronous email sending. There is a list type in the data type of redis, which can implement message queue. Use the following command:
1 2 3 4 5 6 |
|
Architecture design
This example The traditional MVC framework delivers email sending requirements, and MixPHP multi-process performs sending tasks.
Selection of email sending library
In the past, we usually used the email sending library provided by the framework, or downloaded the library shared by other users online. After composer appeared, https: There are a lot of high-quality libraries on //packagist.org/, we just need to choose the best one, in this case swiftmailer.
Since the sending task is executed by MixPHP, swiftmailer is installed in the MixPHP project. Execute the following command in the project root directory to install:
1 |
|
Production Developer development
In the requirement of email sending, the producer refers to the party that delivers the sending task. This party is usually an interface or web page. This part does not necessarily require mixphp development, TP, CI, YII All of these are possible, just post the task information to the message queue in the interface or web page.
Add the following code to the controller of the traditional MVC framework:
Usually using redis in the framework will install a class library for use. This example uses native code for easy understanding.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
Usually in asynchronous development, a message will be responded to the user immediately after the delivery is completed. Of course, the task is not executed at this time.
Consumer Development
In this example we use MixPHP’s multi-process development tool TaskExecutor to complete this requirement. Resident processes are usually used to handle queue consumption, so we Use the TYPE_DAEMON type and MODE_PUSH mode of TaskExecutor.
TaskExecutor's MODE_PUSH mode has two processes:
Left process: Responsible for taking out task data from the message queue and putting it into the middle process.
Medium process: Responsible for performing email sending tasks.
PushCommand.php code is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
|
Test
1. Start the push resident program in the shell.1 2 |
|
At this time, the shell terminal will print:
Successfully received the test email:
MixPHP
GitHub: https://github.com/mix-php/mix
Official website: http://www. mixphp.cn/