php订单到期删除,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

实现逻辑

ea75205403dd?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

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();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值