Committing extracted and compiled translations
[mediagoblin.git] / mediagoblin / processing / task.py
CommitLineData
eace050a
E
1# GNU MediaGoblin -- federated, autonomous media hosting
2# Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
3#
4# This program is free software: you can redistribute it and/or modify
5# it under the terms of the GNU Affero General Public License as published by
6# the Free Software Foundation, either version 3 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU Affero General Public License for more details.
13#
14# You should have received a copy of the GNU Affero General Public License
15# along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17import logging
18
19from celery.task import Task
20
21from mediagoblin import mg_globals as mgg
22from mediagoblin.db.util import ObjectId
23from mediagoblin.media_types import get_media_manager
24from mediagoblin.processing import mark_entry_failed, BaseProcessingFail
25
26_log = logging.getLogger(__name__)
51eb0267
JW
27logging.basicConfig()
28_log.setLevel(logging.DEBUG)
eace050a
E
29
30
31################################
32# Media processing initial steps
33################################
34
35class ProcessMedia(Task):
36 """
eace050a
E
37 Pass this entry off for processing.
38 """
39 def run(self, media_id):
40 """
41 Pass the media entry off to the appropriate processing function
42 (for now just process_image...)
43 """
44 entry = mgg.database.MediaEntry.one(
45 {'_id': ObjectId(media_id)})
46
47 # Try to process, and handle expected errors.
48 try:
eace050a 49 manager = get_media_manager(entry.media_type)
64712915
JW
50
51 entry.state = u'processing'
52 entry.save()
53
eace050a 54 _log.debug('Processing {0}'.format(entry))
64712915 55
eace050a 56 manager['processor'](entry)
64712915
JW
57
58 entry.state = u'processed'
59 entry.save()
60
51eb0267 61 except BaseProcessingFail as exc:
eace050a
E
62 mark_entry_failed(entry._id, exc)
63 return
64712915 64
51eb0267 65 except ImportError as exc:
eace050a
E
66 _log.error(
67 'Entry {0} failed to process due to an import error: {1}'\
68 .format(
69 entry.title,
70 exc))
71
72 mark_entry_failed(entry._id, exc)
73
2891b2c6
JW
74 except Exception as exc:
75 _log.error('An unhandled exception was raised while'
76 + ' processing {0}'.format(
77 entry))
78
79 mark_entry_failed(entry._id, exc)
80 raise
81
eace050a
E
82 def on_failure(self, exc, task_id, args, kwargs, einfo):
83 """
84 If the processing failed we should mark that in the database.
85
86 Assuming that the exception raised is a subclass of
87 BaseProcessingFail, we can use that to get more information
88 about the failure and store that for conveying information to
89 users about the failure, etc.
90 """
91 entry_id = args[0]
92 mark_entry_failed(entry_id, exc)