Now we have something useful: mediagoblin.celery_setup.from_celery
[mediagoblin.git] / mediagoblin / celery_setup / from_celery.py
CommitLineData
1e48a830
CAW
1# GNU MediaGoblin -- federated, autonomous media hosting
2# Copyright (C) 2011 Free Software Foundation, Inc
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 os
18
19import mongokit
20from paste.deploy.loadwsgi import NicerConfigParser
21
22from mediagoblin import storage
23from mediagoblin.celery_setup import setup_celery_from_config
24from mediagoblin.globals import setup_globals
25
26
27OUR_MODULENAME = 'mediagoblin.celery_setup.from_celery'
28
29
30def setup_self(setup_globals_func=setup_globals):
31 """
32 Transform this module into a celery config module by reading the
33 mediagoblin config file. Set the environment variable
34 MEDIAGOBLIN_CONFIG to specify where this config file is at and
35 what section it uses.
36
37 By default it defaults to 'mediagoblin.ini:app:mediagoblin'.
38
39 The first colon ":" is a delimiter between the filename and the
40 config section, so in this case the filename is 'mediagoblin.ini'
41 and the section where mediagoblin is defined is 'app:mediagoblin'.
42
43 Args:
44 - 'setup_globals_func': this is for testing purposes only. Don't
45 set this!
46 """
47 mgoblin_conf_file, mgoblin_section = os.environ.get(
48 'MEDIAGOBLIN_CONFIG', 'mediagoblin.ini:app:mediagoblin').split(':', 1)
49 if not os.path.exists(mgoblin_conf_file):
50 raise IOError(
51 "MEDIAGOBLIN_CONFIG not set or file does not exist")
52
53 parser = NicerConfigParser(mgoblin_conf_file)
54 parser.read(mgoblin_conf_file)
55 parser._defaults.setdefault(
56 'here', os.path.dirname(os.path.abspath(mgoblin_conf_file)))
57 parser._defaults.setdefault(
58 '__file__', os.path.abspath(mgoblin_conf_file))
59
60 mgoblin_section = dict(parser.items(mgoblin_section))
61 mgoblin_conf = dict(
62 [(section_name, dict(parser.items(section_name)))
63 for section_name in parser.sections()])
64 setup_celery_from_config(
65 mgoblin_section, mgoblin_conf,
66 settings_module=OUR_MODULENAME,
67 set_environ=False)
68
69 connection = mongokit.Connection(
70 mgoblin_section.get('db_host'), mgoblin_section.get('db_port'))
71 db = connection[mgoblin_section.get('db_name', 'mediagoblin')]
72
73 # Set up the storage systems.
74 public_store = storage.storage_system_from_paste_config(
75 mgoblin_section, 'publicstore')
76 queue_store = storage.storage_system_from_paste_config(
77 mgoblin_section, 'queuestore')
78
79 setup_globals_func(
80 db_connection=connection,
81 database=db,
82 public_store=public_store,
83 queue_store=queue_store)
84
85
86if os.environ['CELERY_CONFIG_MODULE'] == OUR_MODULENAME:
87 setup_self()