Factor out the actual calling of the processing.
[mediagoblin.git] / mediagoblin / submit / lib.py
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
17 import urllib
18 import urllib2
19 import logging
20 from celery import registry
21
22 from mediagoblin import mg_globals
23 from mediagoblin.processing import mark_entry_failed
24 from mediagoblin.processing.task import ProcessMedia
25
26
27 _log = logging.getLogger(__name__)
28
29
30 def run_process_media(entry):
31 process_media = registry.tasks[ProcessMedia.name]
32 try:
33 process_media.apply_async(
34 [unicode(entry.id)], {},
35 task_id=entry.queued_task_id)
36 except BaseException as exc:
37 # The purpose of this section is because when running in "lazy"
38 # or always-eager-with-exceptions-propagated celery mode that
39 # the failure handling won't happen on Celery end. Since we
40 # expect a lot of users to run things in this way we have to
41 # capture stuff here.
42 #
43 # ... not completely the diaper pattern because the
44 # exception is re-raised :)
45 mark_entry_failed(entry.id, exc)
46 # re-raise the exception
47 raise
48
49
50 def handle_push_urls(request):
51 if mg_globals.app_config["push_urls"]:
52 feed_url = request.urlgen(
53 'mediagoblin.user_pages.atom_feed',
54 qualified=True,
55 user=request.user.username)
56 hubparameters = {
57 'hub.mode': 'publish',
58 'hub.url': feed_url}
59 hubdata = urllib.urlencode(hubparameters)
60 hubheaders = {
61 "Content-type": "application/x-www-form-urlencoded",
62 "Connection": "close"}
63 for huburl in mg_globals.app_config["push_urls"]:
64 hubrequest = urllib2.Request(huburl, hubdata, hubheaders)
65 try:
66 hubresponse = urllib2.urlopen(hubrequest)
67 except urllib2.HTTPError as exc:
68 # This is not a big issue, the item will be fetched
69 # by the PuSH server next time we hit it
70 _log.warning(
71 "push url %r gave error %r", huburl, exc.code)
72 except urllib2.URLError as exc:
73 _log.warning(
74 "push url %r is unreachable %r", huburl, exc.reason)