Commit f7435caf7de46515916c0092145fbd80012ab4f6

Authored by Sergio Oliveira
2 parents dcbb8462 51f49c61

Merge pull request #49 from colab/log_scripts

Logged expection in import_email script
src/badger/management/commands/update_badges.py
@@ -6,11 +6,12 @@ from haystack.query import SearchQuerySet @@ -6,11 +6,12 @@ from haystack.query import SearchQuerySet
6 from accounts.models import User 6 from accounts.models import User
7 from badger.models import Badge 7 from badger.models import Badge
8 8
  9 +import logging
9 10
10 class Command(BaseCommand): 11 class Command(BaseCommand):
11 help = "Update the user's badges" 12 help = "Update the user's badges"
12 13
13 - def handle(self, *args, **kwargs): 14 + def update_badges(self):
14 for badge in Badge.objects.filter(type='auto'): 15 for badge in Badge.objects.filter(type='auto'):
15 if not badge.comparison: 16 if not badge.comparison:
16 continue 17 continue
@@ -30,10 +31,14 @@ class Command(BaseCommand): @@ -30,10 +31,14 @@ class Command(BaseCommand):
30 ) 31 )
31 opts = {key: badge.value} 32 opts = {key: badge.value}
32 33
33 - sqs = SearchQuerySet().filter(  
34 - type='user',  
35 - **opts  
36 - ) 34 + sqs = SearchQuerySet().filter(type='user', **opts)
37 35
38 for user in sqs: 36 for user in sqs:
39 badge.awardees.add(User.objects.get(pk=user.pk)) 37 badge.awardees.add(User.objects.get(pk=user.pk))
  38 +
  39 + def handle(self, *args, **kwargs):
  40 + try:
  41 + self.update_badges()
  42 + except Exception as e:
  43 + logging.exception(e)
  44 + raise
src/super_archives/management/commands/import_emails.py
@@ -7,6 +7,7 @@ import os @@ -7,6 +7,7 @@ import os
7 import re 7 import re
8 import sys 8 import sys
9 import mailbox 9 import mailbox
  10 +import logging
10 from optparse import make_option 11 from optparse import make_option
11 12
12 from django.db import transaction 13 from django.db import transaction
@@ -248,7 +249,6 @@ class Command(BaseCommand, object): @@ -248,7 +249,6 @@ class Command(BaseCommand, object):
248 # This anti-pattern is needed to avoid the transations to 249 # This anti-pattern is needed to avoid the transations to
249 # get stuck in case of errors. 250 # get stuck in case of errors.
250 transaction.rollback() 251 transaction.rollback()
251 - os.remove(self.lock_file)  
252 raise 252 raise
253 253
254 count += 1 254 count += 1
@@ -279,8 +279,12 @@ class Command(BaseCommand, object): @@ -279,8 +279,12 @@ class Command(BaseCommand, object):
279 run_lock = file(self.lock_file, 'w') 279 run_lock = file(self.lock_file, 'w')
280 run_lock.close() 280 run_lock.close()
281 281
282 - self.import_emails(archives_path, 282 + try:
  283 + self.import_emails(archives_path,
283 options.get('all'), options.get('exclude_lists')) 284 options.get('all'), options.get('exclude_lists'))
284 -  
285 - os.remove(self.lock_file) 285 + except Exception as e:
  286 + logging.exception(e)
  287 + raise
  288 + finally:
  289 + os.remove(self.lock_file)
286 290