Detailed explanation on how PHP achieves order delay processing, and detailed explanation on php Order Delay

Source: Internet
Author: User

Detailed explanation on how PHP achieves order delay processing, and detailed explanation on php Order Delay

Business Requirements

Orders are a feature that we often encounter in daily development. Recently, when doing business, we need to implement the function of automatically canceling the order timeout and unpaid payment after the customer places the order. At the beginning, we confirmed several methods:

  • Client-to-time request cancellation
  • The server periodically queries whether there are any orders to be canceled and then processes them in batches.
  • Create a timer after placing the order and handle the delay
  • Use redis or memcache for storage, set expiration time, and automatically delete

Taking into account the above methods, the first method is to exclude it first, because if the customer disables the APP background or network connection, the request cannot be sent to the server, and the Order will remain unprocessed; the second method uses a lot, but there is a problem of accuracy, there is a need to confirm the cycle of the scheduled task, temporarily listed as a post-completion method; the fourth method has a problem: if an order is deleted, it is physically deleted, unable to count unprocessed data (of course, you can store redis databases such as mysql for long-term storage and then use method 2 for regular processing ).

Finally, prepare for use method 3.

When using method 3, because of the PHP development language, you need to use Swoole or workerman to implement the timer function. As Swoole is an extension framework developed by C, Swoole is selected for better performance.

Preparations

  • To use Swoole, you must first install the Swoole extension on the server. The installation method is similar to installing other extensions. For more information, see this article.
  • Check whether the extension is properly installed after installation. Check phpinfo or PHP-m. If Swoole appears, the installation is successful.
  • Swoole official documentation provides timer-related documentation

Start testing

Create a swoole_test.php file and a log.txt file for testing. The swoole_test.php code is as follows:

<?phpswoole_timer_after(3000, function () { append_log(time()); echo "after 3000ms.\n";});function append_log($str) { $dir = 'log.txt'; $fh = fopen($dir, "a"); fwrite($fh, $str."\n"); fclose($fh);}

Then access the PHP file on the webpage. The result is as follows:

Then run PHP:/usr/local/php7/bin/php/home/app/swoole_test.php on the Linux terminal. The result is as follows:

A burst of heart...

In the past, the timer was only in cli mode, so I was afraid that the idea would be GG. Is there any other way to put it down? When I was crying, a word suddenly came to my mind: Python!

Yes, we can't just rely on PHP. We also know the OS in the Python OS module. the system method can execute command lines, so you can run the swoole_test.php file in cli mode.

After a burst of excitement, I felt that the test was feasible.

We know that Linux comes with Python, but different versions of Python are different. Some of them come with Python2.6. The version is too low, so you need to install a higher version. Here I choose Python3, do not overwrite the built-in Python 2. The installation steps are as follows:

  • Wget http://python.org/ftp/python/3.6.0/Python-3.6.0.tar.xz
  • Tar xf Python-3.6.0.tar.xz
  • Cd Python-3.6.0
  • ./Configure -- prefix =/usr/local/python3
  • Make & make install
  • Ln-s/usr/local/python3/bin/python3/usr/bin/python3

Enter Python3 on the terminal.


The installation is successful.

After installing Python3, create a new test. py file with the following content:

#! Usr/bin/env python3' #-*-coding: UTF-8-*-import osret = OS. system ("/usr/local/php7/bin/php/home/app/swoole_test.php") # Use the absolute path of your system, print (ret)

Then run/usr/bin/python3/home/app/test. py on the terminal. Note: Only the PHP file is executed, but the echocontent in the file is not output in logs. The new log.txt file is used. After the Python file is executed, check the log file and find that the content has been written. Therefore, you can use Python to implement the cli mode of PHP. Bytes | `O' | bytes ~~


You may be confused here. You have implemented the cli mode of PHP using Python, but how can you remotely access it through the web? The exec method of PHP is used at this time. We know the exec method of PHP and the OS of Python. the same system method can execute command line commands, so we can create a new test. PHP file with the following content:

<? Php $ program = "/usr/bin/python3/home/app/nongyephp/test. py "; # note the absolute path echo" begin <br> "; (exec ($ program); echo" end <br> "; die;

Then we access the test. php file through the webpage. The result is as follows:


Check the log file and find that the log is also written. Therefore, this method is feasible!

I am very excited here, but I always feel that something is wrong. I finally realized a silly problem: Since PHP can directly have command line functions, why do I need to use Python and then use Python functions? Isn't that an extra effort?

Then I yelled at myself N times, and then I silently modified the content of the test. php file:

<? Phpecho "begin <br>"; $ program = "/usr/local/php7/bin/php/home/app/nongyephp/swoole_test.php "; # use absolute path (exec ($ program); echo "end <br>"; die;

When you directly access the test. PHP file, the feedback result is the same as that with Python, so that you can skip the Python step and directly use the PHP exec function to execute the PHP file.

End

After the test is passed, it is found that the timer can be created and used remotely through the web. However, there is a problem. If we use web page simulation as above, we will find that web page Refresh has to wait for test. php Execution will end. That is to say, if we set the extension time to 30 minutes, it will take 30 minutes for us to get feedback. This method certainly won't work, so we need to use Asynchronous access, for example, we will not go into details here when using web ajax and other asynchronous technologies.

Tail

The above are just the ideas and implementation steps I want to solve the problem. This method may not be used for real development, because it has not been tested by performance, there is not much in-depth understanding of process control and thread control, so method 2 will be selected for automatic cancellation of orders in the future.
In fact, the above method can completely Save the Python step. The reason I did not remove it is to write out my implementation experience, because I think this is a true option during development, in short, we need to think more, read more code, and find a solution that can be optimized. Here we feel that we are far away from each other.

Summary

The above is all the content of this article. I hope the content of this article has some reference and learning value for everyone's learning or work. If you have any questions, please leave a message to us, thank you for your support.

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.