baoxiang/cleanup_expired_chests.py

57 lines
1.9 KiB
Python
Raw Normal View History

2025-12-16 18:06:50 +08:00
#!/usr/bin/env python3
"""
清理过期的宝箱
"""
import pymysql
from datetime import datetime
def cleanup_expired_chests():
"""清理过期的宝箱"""
print("=== 清理过期的宝箱 ===")
try:
# 连接到MySQL数据库
connection = pymysql.connect(
host='localhost',
user='root',
password='taiyi1224',
database='baoxiang',
charset='utf8mb4'
)
with connection.cursor() as cursor:
# 查找所有状态为LOCKED或BETTING但已经过期很久的宝箱
cursor.execute("""
SELECT id, title, created_at, countdown_seconds, status
FROM chests
WHERE status IN ('LOCKED', 'BETTING')
AND created_at < DATE_SUB(NOW(), INTERVAL 1 HOUR)
""")
expired_chests = cursor.fetchall()
print(f"找到 {len(expired_chests)} 个可能过期的宝箱")
for chest in expired_chests:
chest_id, title, created_at, countdown_seconds, status = chest
print(f" 宝箱ID: {chest_id}, 标题: {title}, 状态: {status}, 创建时间: {created_at}")
# 将过期的宝箱状态设置为FINISHED
cursor.execute("""
UPDATE chests
SET status = 'FINISHED'
WHERE id = %s
""", (chest_id,))
print(f" -> 已更新为FINISHED状态")
# 提交事务
connection.commit()
print(f"成功清理 {len(expired_chests)} 个过期宝箱")
connection.close()
except Exception as e:
print(f"MySQL数据库连接错误: {e}")
if __name__ == "__main__":
cleanup_expired_chests()