文章内容

2024/12/2 20:17:31,作 者: 黄兵

2024 年 12 月 2 日用户扣费程序异常复盘

2024 年 12 月 2 日用户通过工单反馈扣费异常,截图如下:

经过检查此用户的扣费记录,的确是存在异常扣费情况,账单截图:

通过上图可以看到,每一个小时扣一次费用,直至账户金额扣完。

用户发来工单之后,我立即检查用户账单,发现的确是存在问题,于是我立即恢复用户号码,同时多扣除用户的费用,返还给用户。

检查自动扣费服务,出现如下错误:

calendar.IllegalMonthError: bad month number 13; must be 1-12

通过错误我们可以看到,在计算月份的时候出现了错误,这个月正好是 12 月份,用户扣完费用之后,增加一个月,变成了 13 月,所以出现了以上错误。

具体错误代码:

# 更新用户号码到期时间
days_in_next_month = calendar.monthrange(get_user_end_time.year, get_user_end_time.month + 1)[1]
# 获取下个月到期时间
next_month = get_user_end_time + timedelta(days=days_in_next_month)

这段关键代码触发了以上错误。

解决方案:

修改错误代码:

# 2024 年 12 月 3 日修复:calendar.IllegalMonthError: bad month number 13; must be 1-12
next_month_year = get_user_end_time.year
next_month_month = get_user_end_time.month + 1

# 使用取余计算月份,并根据超出的年数调整年份
# 例如:next_month_year = 2024 , next_month_month = 13
next_month_year += (next_month_month - 1) // 12
next_month_month = (next_month_month - 1) % 12 + 1
# 则 next_month_month = 1 ,  next_month_year = 2025
# 获取下个月到期时间
next_month = get_user_end_time.replace(year=next_month_year, month=next_month_month)
# 更新用户号码到期时间
self.update_user_number_date(phone_number_id=item['id'], user_end_time=next_month)

注释已经写的很详细了,在此不多说。

通过修改以上代码,解决了这个问题。

有的时候,代码平时运行的正常,在一些特殊时间就会出现 Bug。


黄兵个人博客原创。

转载请注明出处:黄兵个人博客 - 2024 年 12 月 2 日用户扣费程序异常复盘

分享到:

发表评论

评论列表