These are changes for issue #405, add email comment notification.
[mediagoblin.git] / mediagoblin / processing.py
index 7dd5cc7dacf06f47eae3ec075761253d2c279617..1c84c557246053b4cf564c03a750e9bdde0b0c17 100644 (file)
@@ -1,5 +1,5 @@
 # GNU MediaGoblin -- federated, autonomous media hosting
-# Copyright (C) 2011 MediaGoblin contributors.  See AUTHORS.
+# Copyright (C) 2011, 2012 MediaGoblin contributors.  See AUTHORS.
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU Affero General Public License as published by
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+import logging
+
 from celery.task import Task
 
-from mediagoblin.db.util import ObjectId
+from mediagoblin.db.util import ObjectId, atomic_update
 from mediagoblin import mg_globals as mgg
 
 from mediagoblin.tools.translate import lazy_pass_to_ugettext as _
 
 from mediagoblin.media_types import get_media_manager
 
+_log = logging.getLogger(__name__)
 
 THUMB_SIZE = 180, 180
 MEDIUM_SIZE = 640, 640
@@ -57,14 +60,21 @@ class ProcessMedia(Task):
         try:
             #__import__(entry.media_type)
             manager = get_media_manager(entry.media_type)
+            _log.debug('Processing {0}'.format(entry))
             manager['processor'](entry)
         except BaseProcessingFail, exc:
             mark_entry_failed(entry._id, exc)
             return
         except ImportError, exc:
-            mark_entry_failed(entry[u'_id'], exc)
+            _log.error(
+                'Entry {0} failed to process due to an import error: {1}'\
+                    .format(
+                    entry.title,
+                    exc))
+
+            mark_entry_failed(entry._id, exc)
 
-        entry['state'] = u'processed'
+        entry.state = u'processed'
         entry.save()
 
     def on_failure(self, exc, task_id, args, kwargs, einfo):
@@ -98,21 +108,22 @@ def mark_entry_failed(entry_id, exc):
     if isinstance(exc, BaseProcessingFail):
         # Looks like yes, so record information about that failure and any
         # metadata the user might have supplied.
-        mgg.database['media_entries'].update(
+        atomic_update(mgg.database.MediaEntry,
             {'_id': entry_id},
-            {'$set': {u'state': u'failed',
-                      u'fail_error': exc.exception_path,
-                      u'fail_metadata': exc.metadata}})
+            {u'state': u'failed',
+             u'fail_error': exc.exception_path,
+             u'fail_metadata': exc.metadata})
     else:
+        _log.warn("No idea what happened here, but it failed: %r", exc)
         # Looks like no, so just mark it as failed and don't record a
         # failure_error (we'll assume it wasn't handled) and don't record
         # metadata (in fact overwrite it if somehow it had previous info
         # here)
-        mgg.database['media_entries'].update(
+        atomic_update(mgg.database.MediaEntry,
             {'_id': entry_id},
-            {'$set': {u'state': u'failed',
-                      u'fail_error': None,
-                      u'fail_metadata': {}}})
+            {u'state': u'failed',
+             u'fail_error': None,
+             u'fail_metadata': {}})
 
 
 class BaseProcessingFail(Exception):