使用swoole 定时器变更超时未支付订单状态的解决方案

借助 swoole 定时器和 redis 的 zset 来实现的定时检查并过期未支付订单

起源于一个需求:将30分钟内未支付的订单过期处理成已失效状态。

最常规简单的解决方案:在服务器上,跑一个定时任务,去数据表中查询数据,查到未支付的订单,update 一下这些数据的状态,

这些数据也可以存在在 redis 中,大致操作都是这样的。数据量少,服务器没有很多压力的时候,这几种方案不会突出优劣,想用哪个用哪个。

另一种方案: 存储部分: 借助 redis 的 zset 有序集合,订单产生的时候, zadd orders timestamp orderid 将 orderid 保存到对应的

orders 集合中,以时间戳作为他的 score 分值,存储部分是这样的,简单 + 占用空间内存极小。 读取部分: 在 swoole 启动时,设置定时器,每分钟去 orders set 中读取设置的时间之前的数据,个人为了测试方便,设置的读取前一分钟到前三十分钟内的数据。获取到数据之后,根据业务逻辑处理数据,然后 zrem orders orderid 命令从集合中移除对应的 orderid。 个人以为这个方案是内存占用和效率兼具的一个方案。 代码如下:

order.php

<?php$server = new swoole_websocket_server("0.0.0.0", 9502);
// 在定时器中使用协程需要增加此项配置
$server- set(
  [
    'enable_coroutine' =  true
  ]
);
$server- on('workerStart', function ($server, $workerId) {
  $redis = new Swoole\Coroutine\Redis();
  $redis- connect('127.0.0.1', 6379);
  // tick 为持续触发的定时器
  swoole_timer_tick(10000, function() use ($redis) {
    $upperLimitTime = strtotime('-1 minute');
    $lowerLimitTime = strtotime('-30 minute');
    echo '上限时间:' . $upperLimitTime . '下限时间:' . $lowerLimitTime;
    $result = $redis- zrangebyscore('orders', $lowerLimitTime, $upperLimitTime);
    var_dump($result);
    // 根据查询到的 id 进行业务处理,然后 zrem orders orderid 移除处理成功的 orderid 
  });
});
$server- on('message', function (swoole_websocket_server $server, $request) {
  $server- push($request- fd, "hello");
});
$server- start();

测试过程: php order.php 开启 swoole 监听,然后新起终端,在 redis 的 orders 有序集合中不断写入新数据

http://www.bubuko.com/infodetail-3628958.html

提供一种方案,就是延迟队列。

1.使用redis有序集合

2.EasySwoole Redis 携程客户端[

](http://www.easyswoole.com/Cn/Components/Redis/introduction.html)

实现过程大致思路如下:

生成订单id ---> 扔到延迟队列 ---> 延迟队列消费进程不停获取30分钟前(订单未支付)的订单 ---> 处理订单

1.initialize() 中 注册redis连接池、注册延迟队列消费进程

2.扔到延迟队列

$res = $redis->zadd('delay_queue_test',time(),$orderId);
if($res){
	$this->jsonCode('订单添加成功'.$orderId);
}

3.延迟队列消费进程

protected function run($arg)
{
	go(function(){
		while(true){
			//拿到redis
			$redis = $this->redis;
			//从有序集合中拿到60秒(模拟30分钟)以前的订单
			$orderIds = $redis->zRangeByScore('delay_queue_test',0,time()-60,['withscores'=>true]);
			if(empty($orderIds)){
				Coroutine::sleep(1);
				continue;
			}
			//拿到后立马删除
			$redis->zRem('delay_queue_test',...$orderIds);

			foreach($orderIds as $orderId){
				var_dump($orderId);
				//判断此订单30分钟后,是否仍未完成,做相应的处理
			}
		}
	});
}

4.测试 浏览器访问,等待60秒查看

/index/order

posted @ 2021-01-21 22:39  caibaotimes  阅读(347)  评论(0编辑  收藏  举报