From: Charles Connell Date: Sat, 4 Jan 2014 01:45:53 +0000 (-0500) Subject: Log errors in celery tasks X-Git-Tag: release-20150131~311 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=6b9fc1d21a3fa90d0f964fbe8e7d792ea45cebcb;p=oweals%2Fkarmaworld.git Log errors in celery tasks --- diff --git a/karmaworld/apps/document_upload/models.py b/karmaworld/apps/document_upload/models.py index 54b44ce..a46984d 100644 --- a/karmaworld/apps/document_upload/models.py +++ b/karmaworld/apps/document_upload/models.py @@ -25,7 +25,6 @@ class RawDocument(Document): def convert_to_note(self): """ polymorph this object into a note.models.Note object """ - print "begin convert_to_note" note = Note( course=self.course, name=self.name, @@ -38,13 +37,9 @@ class RawDocument(Document): note.save() for tag in self.tags.all(): note.tags.add(tag) - print "finish convert_to_note" return note def save(self, *args, **kwargs): - print "`RawDocument.save()`" super(RawDocument, self).save(*args, **kwargs) if not self.is_processed: - print "\t document not processed yet, doing that now" tasks.process_raw_document.delay(self) - print "\t this arrow should point to the word now ^" diff --git a/karmaworld/apps/document_upload/tasks.py b/karmaworld/apps/document_upload/tasks.py index 9c1adac..a09467d 100644 --- a/karmaworld/apps/document_upload/tasks.py +++ b/karmaworld/apps/document_upload/tasks.py @@ -1,14 +1,19 @@ #!/usr/bin/env python # -*- coding:utf8 -*- # Copyright (C) 2013 FinalsClub Foundation +import traceback from celery import task +from celery.utils.log import get_task_logger from karmaworld.apps.notes.gdrive import convert_raw_document +logger = get_task_logger(__name__) + @task() def process_raw_document(raw_document): """ Process a RawDocument instance in to a Note instance """ - print "="*80 - print "this line should be deferred and only printed by celery" - convert_raw_document(raw_document) + try: + convert_raw_document(raw_document) + except: + logger.error(traceback.format_exc()) diff --git a/karmaworld/apps/document_upload/views.py b/karmaworld/apps/document_upload/views.py index a46be78..58a83e2 100644 --- a/karmaworld/apps/document_upload/views.py +++ b/karmaworld/apps/document_upload/views.py @@ -16,7 +16,6 @@ def save_fp_upload(request): if r_d_f.is_valid(): raw_document = r_d_f.save(commit=False) - print request.POST.keys() time_a = datetime.datetime.now() raw_document.fp_file = request.POST['fp_file'] diff --git a/karmaworld/apps/notes/search.py b/karmaworld/apps/notes/search.py index 48394d0..ad62c6b 100644 --- a/karmaworld/apps/notes/search.py +++ b/karmaworld/apps/notes/search.py @@ -55,6 +55,7 @@ def note_to_dict(note): def add_document(note): if note.text: + logger.info("Indexing {n}".format(n=note)) index.add_document(note.id, note_to_dict(note), variables={0: note.thanks}) else: logger.warn("Note {n} has no text, will not add to IndexDen".format(n=note)) diff --git a/karmaworld/apps/notes/tasks.py b/karmaworld/apps/notes/tasks.py index 1419ee6..fe5534b 100644 --- a/karmaworld/apps/notes/tasks.py +++ b/karmaworld/apps/notes/tasks.py @@ -2,14 +2,14 @@ # -*- coding:utf8 -*- # Copyright (C) 2013 FinalsClub Foundation +import traceback from celery import task +from celery.utils.log import get_task_logger from karmaworld.apps.notes.models import Note import twitter -import logging import gdshortener -logging.basicConfig() -logger = logging.getLogger(__name__) +logger = get_task_logger(__name__) @task(name="tweet_note") def tweet_note(): @@ -21,25 +21,28 @@ def tweet_note(): logger.warn("No twitter secrets found, not running tweet_note") return - api = twitter.Api(consumer_key=secrets.CONSUMER_KEY, - consumer_secret=secrets.CONSUMER_SECRET, - access_token_key=secrets.ACCESS_TOKEN_KEY, - access_token_secret=secrets.ACCESS_TOKEN_SECRET) + try: + api = twitter.Api(consumer_key=secrets.CONSUMER_KEY, + consumer_secret=secrets.CONSUMER_SECRET, + access_token_key=secrets.ACCESS_TOKEN_KEY, + access_token_secret=secrets.ACCESS_TOKEN_SECRET) - newest_notes = Note.objects.all().order_by('-uploaded_at')[:100] - for n in newest_notes: - if not n.tweeted: - update = tweet_string(n) - logger.info("Tweeting: " + update) + newest_notes = Note.objects.all().order_by('-uploaded_at')[:100] + for n in newest_notes: + if not n.tweeted: + update = tweet_string(n) + logger.info("Tweeting: " + update) - # Mark this tweeted before we actually tweet it - # to be extra safe against double tweets - n.tweeted = True - n.save() + # Mark this tweeted before we actually tweet it + # to be extra safe against double tweets + n.tweeted = True + n.save() - api.PostUpdate(tweet_string(n)) + api.PostUpdate(tweet_string(n)) - break + break + except: + logger.error(traceback.format_exc()) def tweet_string(note):