Move celery task into own task.py
authorElrond <elrond+mediagoblin.org@samba-tng.org>
Wed, 21 Mar 2012 10:39:52 +0000 (11:39 +0100)
committerElrond <elrond+mediagoblin.org@samba-tng.org>
Wed, 21 Mar 2012 11:12:07 +0000 (12:12 +0100)
Move the actual celery task from processing/__init__.py
into its own .../task.py. That way it can be imported as
needed.

mediagoblin/processing/__init__.py
mediagoblin/processing/task.py [new file with mode: 0644]
mediagoblin/submit/views.py

index 1c84c557246053b4cf564c03a750e9bdde0b0c17..ecdfa8a9473e57afc38ee5cb687df42a1735a6a5 100644 (file)
 
 import logging
 
-from celery.task import Task
-
-from mediagoblin.db.util import ObjectId, atomic_update
+from mediagoblin.db.util import 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
@@ -38,58 +34,6 @@ def create_pub_filepath(entry, filename):
              filename])
 
 
-################################
-# Media processing initial steps
-################################
-
-class ProcessMedia(Task):
-    """
-    DEPRECATED -- This now resides in the individual media plugins
-
-    Pass this entry off for processing.
-    """
-    def run(self, media_id):
-        """
-        Pass the media entry off to the appropriate processing function
-        (for now just process_image...)
-        """
-        entry = mgg.database.MediaEntry.one(
-            {'_id': ObjectId(media_id)})
-
-        # Try to process, and handle expected errors.
-        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:
-            _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.save()
-
-    def on_failure(self, exc, task_id, args, kwargs, einfo):
-        """
-        If the processing failed we should mark that in the database.
-
-        Assuming that the exception raised is a subclass of
-        BaseProcessingFail, we can use that to get more information
-        about the failure and store that for conveying information to
-        users about the failure, etc.
-        """
-        entry_id = args[0]
-        mark_entry_failed(entry_id, exc)
-
-
 def mark_entry_failed(entry_id, exc):
     """
     Mark a media entry as having failed in its conversion.
diff --git a/mediagoblin/processing/task.py b/mediagoblin/processing/task.py
new file mode 100644 (file)
index 0000000..901d293
--- /dev/null
@@ -0,0 +1,78 @@
+# GNU MediaGoblin -- federated, autonomous media hosting
+# 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
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Affero General Public License for more details.
+#
+# 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 import mg_globals as mgg
+from mediagoblin.db.util import ObjectId
+from mediagoblin.media_types import get_media_manager
+from mediagoblin.processing import mark_entry_failed, BaseProcessingFail
+
+_log = logging.getLogger(__name__)
+
+
+################################
+# Media processing initial steps
+################################
+
+class ProcessMedia(Task):
+    """
+    DEPRECATED -- This now resides in the individual media plugins
+
+    Pass this entry off for processing.
+    """
+    def run(self, media_id):
+        """
+        Pass the media entry off to the appropriate processing function
+        (for now just process_image...)
+        """
+        entry = mgg.database.MediaEntry.one(
+            {'_id': ObjectId(media_id)})
+
+        # Try to process, and handle expected errors.
+        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:
+            _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.save()
+
+    def on_failure(self, exc, task_id, args, kwargs, einfo):
+        """
+        If the processing failed we should mark that in the database.
+
+        Assuming that the exception raised is a subclass of
+        BaseProcessingFail, we can use that to get more information
+        about the failure and store that for conveying information to
+        users about the failure, etc.
+        """
+        entry_id = args[0]
+        mark_entry_failed(entry_id, exc)
index 1e145e9d7df962fe5a064531bb77d1f48b80d2bc..feda0cb2b44c82a7f3298ba3f7c3bcca0a563600 100644 (file)
@@ -33,7 +33,8 @@ from mediagoblin.tools.translate import pass_to_ugettext as _
 from mediagoblin.tools.response import render_to_response, redirect
 from mediagoblin.decorators import require_active_login
 from mediagoblin.submit import forms as submit_forms, security
-from mediagoblin.processing import mark_entry_failed, ProcessMedia
+from mediagoblin.processing import mark_entry_failed
+from mediagoblin.processing.task import ProcessMedia
 from mediagoblin.messages import add_message, SUCCESS
 from mediagoblin.media_types import get_media_type_and_manager, \
     InvalidFileType, FileTypeNotSupported