Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

不规范的数据更新操作 #373

Open
Aubrey-Liu opened this issue Feb 5, 2022 · 6 comments
Open

不规范的数据更新操作 #373

Aubrey-Liu opened this issue Feb 5, 2022 · 6 comments

Comments

@Aubrey-Liu
Copy link
Member

YPPF/app/activity_utils.py

Lines 176 to 193 in c5a2f43

if l <= leftQuota:
Participant.objects.filter(
activity_id=activity.id,
status__in=[Participant.AttendStatus.APPLYING, Participant.AttendStatus.APLLYFAILED]
).update(status=Participant.AttendStatus.APLLYSUCCESS)
activity.current_participants = engaged + l
else:
lucky_ones = sample(range(l), leftQuota)
activity.current_participants = activity.capacity
for i, participant in enumerate(Participant.objects.select_for_update().filter(
activity_id=activity.id,
status__in=[Participant.AttendStatus.APPLYING, Participant.AttendStatus.APLLYFAILED]
)):
if i in lucky_ones:
participant.status = Participant.AttendStatus.APLLYSUCCESS
else:
participant.status = Participant.AttendStatus.APLLYFAILED
participant.save()

这部分更新操作没有包裹在atomic中,并且activity.current_participants在更新后没有save。

@YWolfeee
Copy link
Member

YWolfeee commented Feb 5, 2022

外层函数调用的时候应该atomic了

@Aubrey-Liu
Copy link
Member Author

外层函数调用的时候应该atomic了

嗯嗯确实是这样的,但是这个atomic包裹的访问有点太大了,并且内部进行了异常捕获(该函数内部又调用了bulk_notification_create),可能会向django隐藏问题,导致不能正常回滚。

@YWolfeee
Copy link
Member

YWolfeee commented Feb 7, 2022

收到 @pkuGenuine 看一下是不是确实有这个问题?

@pkuGenuine
Copy link
Contributor

@Aubrey-Lu bulk_notification_create 内部确实有异常捕获,但正常情况下是不应该出现异常的。捕获后会发送微信消息告知管理员。在这个函数里面捕获是为了即使消息出错未能创建,也不影响活动状态的变更。

@Aubrey-Liu
Copy link
Member Author

@Aubrey-Lu bulk_notification_create 内部确实有异常捕获,但正常情况下是不应该出现异常的。捕获后会发送微信消息告知管理员。在这个函数里面捕获是为了即使消息出错未能创建,也不影响活动状态的变更。

确实是这样的,不过按“消息创建出错也不影响活动变更”的思路,其实不应该把它们放在同一个事物中的,倒也不是一个很大的问题,只是感觉有些隐患

@pkuGenuine
Copy link
Contributor

@Aubrey-Lu 也有道理,你想改就改了呗

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants