Rewrite message flag handling to fork out to a subprocess for batch handling

(imported from commit 1ef846f542950cabf32f8b176f5591cf5794a0ff)
This commit is contained in:
Leo Franchi
2013-03-15 15:07:38 -04:00
parent 9977cdde54
commit 6d6c3364dc
4 changed files with 152 additions and 19 deletions

View File

@@ -550,12 +550,41 @@ if settings.USING_RABBITMQ or settings.TEST_SUITE:
process_user_presence_event(event)
def update_message_flags(user_profile, operation, flag, messages, all):
rest_until = None
if all:
# Do the first 450 message updates in-process, as this is a
# bankruptcy request and the user is about to reload. We don't
# want them to see a bunch of unread messages while we go about
# doing the work
first_batch = 450
flagattr = getattr(UserMessage.flags, flag)
all_ums = UserMessage.objects.filter(user_profile=user_profile)
if operation == "add":
umessages = all_ums.filter(flags=~flagattr)
elif operation == "remove":
umessages = all_ums.filter(flags=flagattr)
mids = [m.id for m in umessages.order_by('-id')[:first_batch]]
to_update = UserMessage.objects.filter(id__in=mids)
if operation == "add":
to_update.update(flags=F('flags') | flagattr)
elif operation == "remove":
to_update.update(flags=F('flags') & ~flagattr)
if len(mids) == 0:
return True
rest_until = mids[len(mids) - 1]
event = {'type': 'update_message',
'user_profile_id': user_profile.id,
'operation': operation,
'flag': flag,
'messages': messages,
'all': all}
'until_id': rest_until}
if settings.USING_RABBITMQ:
actions_queue.json_publish("user_activity", event)
else:
@@ -574,25 +603,45 @@ def process_user_presence_event(event):
def process_update_message_flags(event):
user_profile = UserProfile.objects.get(id=event["user_profile_id"])
try:
msg_ids = event["messages"]
flag = getattr(UserMessage.flags, event["flag"])
until_id = event["until_id"]
messages = event["messages"]
flag = event["flag"]
op = event["operation"]
except (KeyError, AttributeError):
return False
if event["all"] == True:
messages = UserMessage.objects.filter(user_profile=user_profile)
else:
messages = UserMessage.objects.filter(user_profile=user_profile,
message__id__in=msg_ids)
# Shell out bankruptcy requests as we split them up into many
# pieces to avoid swamping the db
if until_id and not settings.TEST_SUITE:
update_flags_externally(op, flag, user_profile, until_id)
return True
if op == "add":
messages.update(flags=F('flags') | flag)
elif op == "remove":
messages.update(flags=F('flags') & ~flag)
flagattr = getattr(UserMessage.flags, flag)
msgs = UserMessage.objects.filter(user_profile=user_profile,
message__id__in=messages)
# If we're running in the test suite, don't shell out to manage.py.
# Updates that the manage.py command makes don't seem to be immediately
# reflected in the next in-process sqlite queries.
# TODO(leo) remove when tests switch to postgres
if settings.TEST_SUITE and until_id:
msgs = UserMessage.objects.filter(user_profile=user_profile,
id__lte=until_id)
if op == 'add':
msgs.update(flags=F('flags') | flagattr)
elif op == 'remove':
msgs.update(flags=F('flags') & ~flagattr)
return True
def update_flags_externally(op, flag, user_profile, until_id):
args = ['python', os.path.join(os.path.dirname(__file__), '../..', 'manage.py'),
'set_message_flags', '--for-real', '-o', op, '-f', flag, '-m', user_profile.user.email,
'-u', str(until_id)]
subprocess.Popen(args, stdin=subprocess.PIPE, stdout=None, stderr=None)
def subscribed_to_stream(user_profile, stream):
try:
if Subscription.objects.get(user_profile=user_profile,