An example of how to implement order delay processing in PHP

Source: Internet
Author: User
recently in the business of the need to implement the customer orders after the order timeout after the automatic cancellation function, just started to confirm several methods:Client-to-time request cancellation service-side timing query There is no need to cancel the order, and then batch processing, after the order to create a timer, delay processing use redisOr memcacheStore, set expiration time, and automatically delete.

Comprehensive consideration of the above method, the first one to exclude, because if the customer put the app background prohibit or network connection is forbidden, then cannot send to the server request, the order will always be unhandled state, the second method uses more, but there is the problem of accuracy, there is a need to confirm the timing of the task cycle, Temporarily listed as a post-repair method; The problem with the fourth method is that if the deletion is a physical deletion, the data cannot be statistically processed (you can, of course, have a database such as MySQL for long-term storage and then use the method two timing processing) when you save Redis.

Finally prepare to use method three.

To confirm the use of Method 3, due to the use of the PHP development language, so want to implement the timer function need to use Swoole or workerman . Due to the Swoole C development of the extension framework, the performance is certainly better, it is selected Swoole .

Pre-preparation

    • Using Swoole first needs to install the extension on the server Swoole , the installation method and the installation of other extensions are similar, you can refer to this article

    • Detect if the extension is installed properly after installation, view phpinfo or PHP-m , if present Swoole , the installation is successful

    • SwooleOfficial documentation with timer related documents

Start testing

We create a swoole_test.php file and a log.txt file (for testing) with the swoole_test.php following code:

<?phpswoole_timer_after ((), 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 on the Web page to access this PHP file, the results are as follows:

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

A burst of heart ...

The original timer only in the cli mode, then this idea is afraid of GG, is it fell here, there is no other way? When I want to cry without tears, suddenly an epiphany, a word flashed to my mind: Python !

Yes, we can't just rely on PHP ah, and Python this magic language, we know Python that the os method in the module os.system can execute the command line, then can not be implemented in the mode to cli run the file just now swoole_test.php .

After a burst of excitement, I think the test is feasible

We know that all are self-contained Linux Python , but different versions of Python different versions, some of them are Python2.6 , the version is too low, so need to install a high version, here I choose Python3 , pay attention not to cover the system comes with Python2 . The following are the approximate installation steps:

    • wget Http://python.org/ftp/python/...tar XF python-3.6.0.tar.xzcd python-3.6.0./configure--prefix=/usr/local/ Python3make && make Installln-s/usr/local/python3/bin/python3/usr/bin/python3

Next the terminal input: Python3 , if it appears

The installation succeeds.

After installation Python3 , we 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 ") #请使用自己系统的绝对路径print (ret)

Then we do at the terminal: /usr/bin/python3 /home/app/test.py , note: This is just the execution of the PHP file, but the contents of the file echo will not be output in the terminal, this time to use the newly created log.txt file. After executing the Python file, we go to log file check and find that the content has been written, so the use Python is possible PHP cli mode. ┗| ' O′|┛ AW ~

There will be students wondering, you use the Python implementation PHP of the cli pattern, but how to remote access through the Web? This is the time to use the PHP exec method, we know that the PHP exec method is the same as the Python os.system method can execute command-line commands, so we can create a new test.php file, the content is as follows:

<?php$program= "/usr/bin/python3/home/app/nongyephp/test.py"; #注意使用绝对路径echo "begin<br>";(exec ($program)); echo "end<br>";d ie;

Then we access the file through the Web page test.php . The results are as follows:

Then go to log file check, found also write log, so this method is feasible!

Do here in the heart flattered, but old feel as if what is wrong, finally finally realize a very silly problem: since PHP can directly have command line function, why superfluous with Python the use Python of the function? It's not the pants, the fart superfluous?

Again big scold oneself is the silly force n times, I silently modified the test.php file content:

<?phpecho "begin<br>"; $program = "/usr/local/php7/bin/php/home/app/nongyephp/swoole_test.php"; #注意使用绝对路径 (EXEC ($program)); echo "end<br>";d ie;

In the direct access to the test.php file, feedback results and the help Python of the same, so that you can eliminate Python that step, directly use PHP the exec function to execute the PHP file.

End

After the test has found that this method can create a timer and remote use through the web, but there is a problem, if you use the same as I mentioned in the Web simulation will find that the page refresh is to wait until test.php the end of execution, In other words, if we set the time of the timer to 30 minutes and wait 30 minutes to have feedback, this will not work, so we need to use asynchronous access, such as Web-based ajax technology and other asynchronous technologies, and not repeat

Tail

    • These are just my thoughts and implementation steps to solve the problem, to the real development may not choose this way, because there is no performance testing, and for process Control and threading control does not have much in-depth understanding, so in the future to do the order automatically canceled or will choose Method 2 Bar.

    • In fact, the above method can completely Python eliminate that step, I did not remove the reason is to write my implementation experience, because I think the development period may really encounter this superfluous way, in short, to think more, look at the code, find the solution can optimize, here feel very far away, mutual encouragement Bar

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.