php记录一次未支付订单过期处理的办法

订单是我们在日常开发中经常会遇到的一个功能,最近在做业务的时候需要实现客户下单之后订单超时未支付自动取消的功能,刚开始确认了几种方法:

  • 客户端到时间请求取消
  • 服务端定时查询有没有需要取消的订单,然后批量处理
  • 下单后创建定时器,延时处理
  • 使用redis或者memcache存储,设置过期时间,自动删除

上面的方法相信很多人都很熟悉了,虽然不熟悉也看过很多它的知识了吧,所以就不多废话了。今天要说的是第三种方法,为了防止进程进入阻塞,所以需要借助Swoole,workerman这样的框架来开启异步的定时器,今天讲解的是如何利用Swoole来处理订单过期时间。

环境要求

  • 安装swoole扩展 安装请参考swoole官网里面有详细的安装教程
  • 配置php.ini开启proc_open

swoole一次性定时器swoole_timer_after

官网介绍如下:

woole_timer_after函数是一个一次性定时器,执行完成后就会销毁。此函数与PHP标准库提供的sleep函数不同,after是非阻塞的。而sleep调用后会导致当前的进程进入阻塞,将无法处理新的请求。

执行成功返回定时器ID,若取消定时器,可调用 swoole_timer_clear

实现逻辑

image

完整代码

开启定时器

/*
 *开启定时器
 *id 订单id
 */
public function openTimer($id)
    {
        $arr = ['order_id' => $id];
        $json = base64_encode(json_encode($arr));
        //处理订单过期请求的地址
        $path = Config::get('order.past_order_url');
        // 定时器脚本路径
        $address = Config::get('order.timer_url');
        $cmd = "php Timer.php -a 1 -u {$path} -p {$json} -t 1800000";
        $descriptorspec = array(
            0 => ["pipe", "r"],
            1 => ["pipe", "w"],
            2 => ["file", "{$address}/error.txt", "a"]
        );
        proc_open($cmd, $descriptorspec, $pipes, $address, NULL);
        return true;
    }

Timer.php

class Timer
{
    public $request_url = null;

    public $params = [];

    public $time = 1000;

    public function __construct()
    {
        $param_arr = getopt('a:u:p:t:');
        if (!isset($param_arr['a'])) {
            $this->writeLog('【传参错误】操作类型不能为空', 2);
            die;
        }

        if (!isset($param_arr['u'])) {
            $this->writeLog('【传参错误】请求url不能为空', 2);
            die;
        }

        if ((isset($param_arr['t']) && $param_arr['t'] < 1000)) {
            $this->writeLog('【传参错误】时间不能小于1000毫秒', 2);
            die;
        }

        if (isset($param_arr['p']) && !is_string($param_arr['p'])) {
            $this->writeLog('【传参错误】请求参数必须是字符串', 2);
            die;
        }

        $this->request_url = $param_arr['u'];

        isset($param_arr['t']) && $this->time = $param_arr['t'];


        isset($param_arr['p']) && $this->params = ['data' => $param_arr['p']];

        if ($param_arr['a'] == 1) {
            $this->timer_after();
        }
    }

    /**
     * 一次性定时器
     * Created by 李果 En:AdoSir <1334435738@qq.com>
     * @return int
     */
    public function timer_after()
    {
        $id = swoole_timer_after($this->time, function () {
            //逻辑处理更具自己情况。我的解决方案是请求一个地址处理订单过期
            $result = $this->cu($this->request_url, json_encode(['data' => $this->params]));
            $this->writeLog("请求URL返回:code:{$result['httpCode']};response:{$result['response']}", 1);
        });
        $this->writeLog("添加定时器【id】:{$id}【参数】:" . json_encode($this->params), 1);
        return $id;
    }

    /**
     * 发起请求
     * @param $url
     * @param array $jsonStr
     * @return array
     */
    public function cu($url, $jsonStr = [])
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, [
                'Content-Type: application/json; charset=utf-8',
                'Content-Length: ' . strlen($jsonStr),
            ]
        );
        $response = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);

        return ['httpCode' => $httpCode, 'response' => $response];
    }

    /**
     * 写入日志
     * @param $msg
     * @param int $type
     */
    public function writeLog($msg, $type = 1)
    {
        //date_default_timezone_set("Asia/Shanghai");
        $day = date('Ymd');
        $file = $type == 1 ? "./log-{$day}.txt" : "./error-{$day}.txt";
        file_put_contents($file, "\r\n" . date("Y - m - d h:i:s") . ': ' . $msg, FILE_APPEND | LOCK_EX);
    }

}

new Timer();
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • swoole 安装用的是centOSphp安装目录:/usr/local/php php.ini配置文件路径:/u...
    a十二_4765阅读 11,899评论 3 9
  • 我家住在飞机场附近,天天看着飞机起飞和降落。我实在想亲身体会一次坐飞机的感受。 妈妈决定今年...
    静思_de8d阅读 340评论 0 0
  • 人类所有的仇恨,大概源自于欲望与贪婪 单纯善良一类的词,听起来真是讽刺 内心的黑暗,因贪婪和纵容而滋生,因欺骗与伤...
    神伤_难道阅读 569评论 0 0
  • 一 曾经有一段闭封的日子。 尝试过几个周都不跟别人交流。 感觉自己像个原始人,真的。看到以前很好的朋友,一起坐在操...
    木泽_阅读 551评论 0 1