Log errors in celery tasks
authorCharles Connell <charles@connells.org>
Sat, 4 Jan 2014 01:45:53 +0000 (20:45 -0500)
committerCharles Connell <charles@connells.org>
Sat, 4 Jan 2014 01:45:53 +0000 (20:45 -0500)
karmaworld/apps/document_upload/models.py
karmaworld/apps/document_upload/tasks.py
karmaworld/apps/document_upload/views.py
karmaworld/apps/notes/search.py
karmaworld/apps/notes/tasks.py

index 54b44cef69681a90ca439b53c2cb1296ae7716c3..a46984d7668a95c743c45eae0ad113cf409bcf3f 100644 (file)
@@ -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 ^"
index 9c1adac0d06a7955c92e8a60900e2a36f6b2c556..a09467dbd430a9581bf0109fc7e53b2aeee41168 100644 (file)
@@ -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())
 
index a46be7874332e724b4b722bc6ea8ef9b76ec8c9d..58a83e2341fd01991152d6231bbf68ab75194f97 100644 (file)
@@ -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']
 
index 48394d00c27ac7a0f1b6f9c54e25e501c1d24967..ad62c6b1b850ccd2439add8bc0481c78f01aa0dc 100644 (file)
@@ -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))
index 1419ee670e39e100e5637b4d29ab9845af09835e..fe5534b1bdbb5f621a7fe53f4d76510171907ce2 100644 (file)
@@ -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):