TP6订单待支付超时自动取消

1:安装redis

2:安装think-queue

composer require topthink/think-queue

3:设置配置文件,使用redis进行存储,文件位置config/queue.php

return [
    'default'     => 'redis',
    'connections' => [
        'sync'     => [
            'type' => 'sync',
        ],
        'database' => [
            'type'       => 'database',
            'queue'      => 'default',
            'table'      => 'jobs',
            'connection' => null,
        ],
        'redis'    => [
            'type'       => 'redis',
            'queue'      => 'default',
            'host'       => '127.0.0.1',
            'port'       => 6379,
            'password'   => '',
            'select'     => 0,
            'timeout'    => 0,
            'persistent' => false,
        ],
    ],
    'failed'      => [
        'type'  => 'none',
        'table' => 'failed_jobs',
    ],
];

4:app目录下新建queue文件夹,文件夹下新建Order.php处理类

//订单超时取消
public function overdue(Job $job,$order_no){
    $order = OrderModel::get($order_no,1);  //校验当前订单是否为待支付状态
    if($order->isEmpty()){
        $job->delete();  //非待支付状态,删除执行
    } else {
        $result = OrderModel::overtime($order_no);  //更新订单状态为已过期
        if($result){
            $job->delete();  //更新过期成功,删除执行
        } else {
            if ($job->attempts() > 3) {
                $job->delete();  //累计执行3次均失败,删除执行
            }else{
                $job->release(10);  //间隔10秒执行一次
            }
        }
    }
}

5:订单生成成功后调用延时队列

use think\facade\Queue;

public static function createOrder(){
    $order = ...//执行订单生成存储       
    Queue::later(30 * 60,'app\queue\Order@overdue',$order['order_no']);  //加入队列,并延迟30分钟执行
}

6:生成订单后查看redis队列是否存在新订单数据

7:到期执行订单状态修改

 8:宝塔环境添加进程守护

安装supervisord管理器

添加进程守护管理

 

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
首先,需要在RabbitMQ中创建一个名为“cancel_order”的队列。该队列将用于接收自动取消订单的消息。 然后,需要编写一个Python脚本来将消息发布到该队列中。以下是示例脚本: ```python import pika # 连接到RabbitMQ服务器 connection = pika.BlockingConnection(pika.ConnectionParameters('localhost')) channel = connection.channel() # 创建一个名为“cancel_order”的队列 channel.queue_declare(queue='cancel_order') # 发布一个消息到队列中 channel.basic_publish(exchange='', routing_key='cancel_order', body='cancel order') print("Order cancellation message sent to RabbitMQ") connection.close() ``` 这个脚本将在RabbitMQ中发布一个名为“cancel order”的消息,用于取消订单。 接下来,需要在订单处理系统中添加一个消费者来监听“cancel_order”队列,并自动取消订单。以下是示例代码: ```python import pika # 连接到RabbitMQ服务器 connection = pika.BlockingConnection(pika.ConnectionParameters('localhost')) channel = connection.channel() # 创建一个名为“cancel_order”的队列 channel.queue_declare(queue='cancel_order') # 定义一个回调函数来处理消息 def cancel_order_callback(ch, method, properties, body): print("Cancel order message received") # 在这里添加取消订单的代码 # ... # 监听“cancel_order”队列 channel.basic_consume(queue='cancel_order', on_message_callback=cancel_order_callback, auto_ack=True) # 开始消费消息 print("Waiting for cancellation messages...") channel.start_consuming() ``` 当一个名为“cancel order”的消息被发布到“cancel_order”队列时,消费者将自动调用“cancel_order_callback”函数,并执行取消订单的代码。 最后,可以使用一个定时任务来定期发布自动取消订单的消息到“cancel_order”队列中。这可以通过使用Python的“schedule”库来实现。以下是示例代码: ```python import schedule import time import pika # 连接到RabbitMQ服务器 connection = pika.BlockingConnection(pika.ConnectionParameters('localhost')) channel = connection.channel() # 创建一个名为“cancel_order”的队列 channel.queue_declare(queue='cancel_order') # 发布一个名为“cancel order”的消息到队列中 def publish_cancel_order_message(): channel.basic_publish(exchange='', routing_key='cancel_order', body='cancel order') print("Order cancellation message sent to RabbitMQ") # 每小时发布一个自动取消订单的消息 schedule.every().hour.do(publish_cancel_order_message) # 开始执行定时任务 while True: schedule.run_pending() time.sleep(1) ``` 这个脚本将每小时发布一个名为“cancel order”的消息到“cancel_order”队列中。这将使消费者自动取消超时订单
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

糯麦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值