Example of method to implement order delay processing in PHP

小云云
Release: 2023-03-21 06:50:02
Original
2538 people have browsed it

Recently, when doing business, I need to implement the function of automatic cancellation after the customer places an order after the order times out and fails to pay. I have just confirmed several methods: The client requests cancellation at the time and checks whether the server has a scheduled time. Orders that need to be canceled are then processed in batches. Create a timer after placing the order. Use redis or memcache for delayed processing. Set the expiration time and delete automatically.

Considering the above methods, the first one is eliminated first, because if the customer disables the APP background or network connection, then the request cannot be sent to the server, and the order will always be in an unprocessed state. ; The second method is more commonly used, but it has accuracy issues and the cycle of scheduled tasks needs to be confirmed, so it is temporarily listed as a backup method; the problem with the fourth method is that if the order is deleted, it will be physically deleted and cannot be counted. Unprocessed data (of course, you can store it in a database like mysql for long-term storage when storing redis, and then use method 2 for regular processing).

Finally prepare to use method three.

When confirming the use of method 3, due to the development language PHP used, if you want to implement the timer function, you need to use Swoole or workerman. Since Swoole is an extension framework developed by C, its performance is definitely better, so I chose Swoole.

Preparation

  • To use Swoole, you first need to install the Swoole extension on the server. The installation method is similar to installing other extensions. You can refer to this article.

  • After installation, check whether the extension is installed normally, check phpinfo or PHP-m, if Swoole appears , it means the installation is successful

  • Swoole The official document has timer related documents

Start testing

We create a swoole_test.php file and a log.txt file (for testing), swoole_test.phpThe code is as follows:

<?php swoole_timer_after(3000, function () {
    append_log(time());
    echo "after 3000ms.\n";
});

function append_log($str) {
    $dir = &#39;log.txt&#39;;
    $fh = fopen($dir, "a");
    fwrite($fh, $str."\n");
    fclose($fh);
}
Copy after login

Then access this PHP file on the web page, the result is as follows: Example of method to implement order delay processing in PHP

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

Example of method to implement order delay processing in PHP

I felt a burst of heart. . .

原来定时器只能在 cli 模式下,那么这个想法怕是要GG了,难道就栽倒这里了吗,难道就没有别的方法了吗?就在我欲哭无泪的时候突然灵光乍现,一个词闪到我的脑海: Python !

对,我们不能单单靠着 PHP 啊,还有 Python 这种神奇的语言呢,我们知道 Python 的 os 模块里的 os.system 方法是可以执行命令行的,那么不就可以实现在 cli 模式下运行刚才的 swoole_test.php 文件了么。

内心一阵激动后,觉得测试是否可行

我们知道 Linux 都是自带 Python 的,但是不同的版本 Python 版本不同,有的自带的是 Python2.6 ,版本过低了,所以需要装一个高版本的,这里我选择 Python3 ,注意不要覆盖系统自带的 Python2 。以下是大致的安装步骤:

  • wget http://python.org/ftp/python/...
    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
    Copy after login

接下来终端输入: Python3 ,如果出现

Example of method to implement order delay processing in PHP

则安装成功。

安装完 Python3 之后,我们新建一个 test.py 文件,内容如下:

#!usr/bin/env python3`
#-*- coding:utf-8 -*-

import os
ret = os.system("/usr/local/php7/bin/php /home/app/swoole_test.php") 
#请使用自己系统的绝对路径
print(ret)
Copy after login

然后我们在终端执行: /usr/bin/python3 /home/app/test.py ,注意:这里只是执行 PHP 文件,但是文件里的 echo 内容是不会在终端输出的,这时候就用到刚才新建的 log.txt 文件了。执行完 Python 文件后,我们去log文件检查下,发现内容已经写入,所以使用 Python 是可以实现 PHPcli 模式的。┗|`O′|┛ 嗷~~

Example of method to implement order delay processing in PHP

到这里就会有同学疑惑了,你这使用 Python 实现了 PHPcli 模式,但是怎么通过web远程访问呢?这个时候就用到PHP的 exec 方法了,我们知道PHP的 exec 方法和Python的 os.system 方法一样是可以执行命令行命令的,所以我们可以新建一个 test.php 文件,内容如下:

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

然后我们通过网页访问 test.php 文件。结果如下:

Example of method to implement order delay processing in PHP

然后去log文件检查,发现也写入日志了,所以这个方法是可行的!

做到这里心里美滋滋的,不过老觉得好像哪里不对,终于终于意识到一个很傻逼的问题: 既然 PHP 可以直接有命令行函数,为啥多此一举借助 Python 然后在用 Python 的函数呢? 这不是脱了裤子放屁多此一举吗?

再大骂自己是傻逼N遍之后,我默默修改了 test.php 文件内容:

<?php
echo "begin<br>";
$program="/usr/local/php7/bin/php /home/app/nongyephp/swoole_test.php";
 #注意使用绝对路径
(exec ($program));
echo "end<br>";
die;
Copy after login

在直接访问 test.php 文件,反馈结果和借助 Python 一样,这样就可以免去 Python 那一步,直接用 PHP 的 exec 函数来执行 PHP 文件。

结尾

测试通过后发现这种方法是可以创建定时器并且通过web远程使用的,不过有个问题,如果用和我上述一样用网页模拟会发现网页刷新是要等 test.php 执行完才会结束,也就是说如果我们把延时器的时间设成30分钟会要等待30分钟才会有反馈信息,这种方式肯定行不通的,所以需要使用异步访问,比如使用web的 ajax 技术和其他异步技术,这里不再赘述

尾巴

  • 以上只是我想到解决问题的想法和实施步骤,到了真正开发可能不会选择这种方式,因为没有经过性能测试,而且对于进程控制和线程控制并没有多深入的了解,所以以后做订单自动取消还是会选择方法2的吧。

  • The above method can actually completely omit the Python step. The reason why I did not remove it is to write down my implementation experience, because I think I may really encounter it during the development. Seeing this superfluous approach, in short, we need to think more, read more code, and find solutions that can be optimized. I feel that I am far behind here, so please share your encouragement

Related recommendations:

Detailed explanation of PHP using redis queue to realize automatic confirmation of receipt of e-commerce orders

Example detailed explanation of the tab switching effect of Vue imitating Taobao order status

PHP implements the RSA signature generation order function using Alipay as an example

The above is the detailed content of Example of method to implement order delay processing in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template