Skip to content

Commit

Permalink
Add more logging info.
Browse files Browse the repository at this point in the history
  • Loading branch information
gillesfabio committed Feb 12, 2015
1 parent aa8eca8 commit b0bbeba
Showing 1 changed file with 20 additions and 24 deletions.
44 changes: 20 additions & 24 deletions badgify/recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import logging

from django.contrib.auth.models import User
from django.db import DEFAULT_DB_ALIAS, IntegrityError
from django.db.models import signals
from django.utils.functional import cached_property
Expand Down Expand Up @@ -232,39 +233,34 @@ def create_awards(self, db_read=None, batch_size=None,
obsolete_ids, obsolete_ids_count = self.get_obsolete_user_ids(db_read=db_read)

if obsolete_ids:
removed_ids_done = 0
for user_ids in chunks(obsolete_ids, batch_size):
removed_ids_done += batch_size
actual_count = removed_ids_done if removed_ids_done <= obsolete_ids_count else obsolete_ids_count

logger.debug("→ Badge %s: removing awards (%d / %d users) -- (db read: %s)",
self.slug,
actual_count,
obsolete_ids_count,
db_read)
obsolete_users = User.objects.using(db_read).in_bulk(user_ids).values()

signals.pre_delete.disconnect(sender=Award, dispatch_uid=PRE_DELETE_UID)
Award.objects.filter(user__in=user_ids).delete()
signals.pre_delete.connect(sender=Award, dispatch_uid=PRE_DELETE_UID)

for user in obsolete_users:
logger.debug("→ Badge %s: unawarded user %s (%d) -- (db read: %s)",
self.slug,
user.username,
user.pk,
db_read)

if unawarded_ids:
done_ids = 0
for user_ids in chunks(unawarded_ids, batch_size):
done_ids += batch_size
actual_count = done_ids if done_ids <= unawarded_ids_count else unawarded_ids_count

logger.debug("→ Badge %s: creating awards (%d / %d users) -- (db read: %s)",
self.slug,
actual_count,
unawarded_ids_count,
db_read)

unwarded_users = User.objects.using(db_read).in_bulk(user_ids).values()
objects = [Award(user_id=user_id, badge=self.badge) for user_id in user_ids]

bulk_create_awards(
objects=objects,
batch_size=batch_size,
post_save_signal=post_save_signal)
bulk_create_awards(objects=objects,
batch_size=batch_size,
post_save_signal=post_save_signal)

for user in unwarded_users:
logger.debug("→ Badge %s: awarded user %s (%d) -- (db read: %s)",
self.slug,
user.username,
user.pk,
db_read)


def bulk_create_awards(objects, batch_size=500, post_save_signal=True):
Expand Down

0 comments on commit b0bbeba

Please sign in to comment.