##// END OF EJS Templates
celery: introduce make_app instead of creating app at import time...
Mads Kiilerich -
r8042:19313892 default
parent child Browse files
Show More
@@ -1,40 +1,41 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 # This program is free software: you can redistribute it and/or modify
2 # This program is free software: you can redistribute it and/or modify
3 # it under the terms of the GNU General Public License as published by
3 # it under the terms of the GNU General Public License as published by
4 # the Free Software Foundation, either version 3 of the License, or
4 # the Free Software Foundation, either version 3 of the License, or
5 # (at your option) any later version.
5 # (at your option) any later version.
6 #
6 #
7 # This program is distributed in the hope that it will be useful,
7 # This program is distributed in the hope that it will be useful,
8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 # GNU General Public License for more details.
10 # GNU General Public License for more details.
11 #
11 #
12 # You should have received a copy of the GNU General Public License
12 # You should have received a copy of the GNU General Public License
13 # along with this program. If not, see <http://www.gnu.org/licenses/>.
13 # along with this program. If not, see <http://www.gnu.org/licenses/>.
14
14
15 import click
15 import click
16
16
17 import kallithea
17 import kallithea
18 import kallithea.bin.kallithea_cli_base as cli_base
18 import kallithea.bin.kallithea_cli_base as cli_base
19 from kallithea.lib import celerypylons
19
20
20
21
21 @cli_base.register_command(config_file_initialize_app=True)
22 @cli_base.register_command(config_file_initialize_app=True)
22 @click.argument('celery_args', nargs=-1)
23 @click.argument('celery_args', nargs=-1)
23 def celery_run(celery_args):
24 def celery_run(celery_args):
24 """Start Celery worker(s) for asynchronous tasks.
25 """Start Celery worker(s) for asynchronous tasks.
25
26
26 This commands starts the Celery daemon which will spawn workers to handle
27 This commands starts the Celery daemon which will spawn workers to handle
27 certain asynchronous tasks for Kallithea.
28 certain asynchronous tasks for Kallithea.
28
29
29 Any extra arguments you pass to this command will be passed through to
30 Any extra arguments you pass to this command will be passed through to
30 Celery. Use '--' before such extra arguments to avoid options to be parsed
31 Celery. Use '--' before such extra arguments to avoid options to be parsed
31 by this CLI command.
32 by this CLI command.
32 """
33 """
33
34
34 if not kallithea.CELERY_ON:
35 if not kallithea.CELERY_ON:
35 raise Exception('Please set use_celery = true in .ini config '
36 raise Exception('Please set use_celery = true in .ini config '
36 'file before running this command')
37 'file before running this command')
37
38
38 from kallithea.lib import celerypylons
39 app = celerypylons.make_app()
39 cmd = celerypylons.worker.worker(celerypylons.app)
40 cmd = celerypylons.worker.worker(app)
40 return cmd.run_from_argv(None, command='celery-run -c CONFIG_FILE --', argv=list(celery_args))
41 return cmd.run_from_argv(None, command='celery-run -c CONFIG_FILE --', argv=list(celery_args))
@@ -1,58 +1,60 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2
2
3 """
3 """
4 Kallithea wrapper of Celery
4 Kallithea wrapper of Celery
5
5
6 The Celery configuration is in the Kallithea ini file but must be converted to an
6 The Celery configuration is in the Kallithea ini file but must be converted to an
7 entirely different format before Celery can use it.
7 entirely different format before Celery can use it.
8
8
9 We read the configuration from tg.config at module import time. This module can
9 We read the configuration from tg.config at module import time. This module can
10 thus not be imported in global scope but must be imported on demand in function
10 thus not be imported in global scope but must be imported on demand in function
11 scope after tg.config has been initialized.
11 scope after tg.config has been initialized.
12
12
13 To make sure that the config really has been initialized, we check one of the
13 To make sure that the config really has been initialized, we check one of the
14 mandatory settings.
14 mandatory settings.
15 """
15 """
16
16
17 import celery
17 import celery
18 import celery.result as result
18 import celery.result as result
19 import tg
19 import tg
20 from celery.bin import worker
20 from celery.bin import worker
21 from celery.task import task
21 from celery.task import task
22
22
23
23
24 def celery_config(config):
24 def celery_config(config):
25 """Return Celery config object populated from relevant settings in a config dict, such as tg.config"""
25 """Return Celery config object populated from relevant settings in a config dict, such as tg.config"""
26
26
27 # Verify .ini file configuration has been loaded
27 # Verify .ini file configuration has been loaded
28 assert config['celery.imports'] == 'kallithea.lib.celerylib.tasks', 'Kallithea Celery configuration has not been loaded'
28 assert config['celery.imports'] == 'kallithea.lib.celerylib.tasks', 'Kallithea Celery configuration has not been loaded'
29
29
30 class CeleryConfig(object):
30 class CeleryConfig(object):
31 pass
31 pass
32
32
33 celery_config = CeleryConfig()
33 celery_config = CeleryConfig()
34
34
35 PREFIXES = """ADMINS BROKER CASSANDRA CELERYBEAT CELERYD CELERYMON CELERY EMAIL SERVER""".split()
35 PREFIXES = """ADMINS BROKER CASSANDRA CELERYBEAT CELERYD CELERYMON CELERY EMAIL SERVER""".split()
36 LIST_PARAMS = """CELERY_IMPORTS ADMINS ROUTES CELERY_ACCEPT_CONTENT""".split()
36 LIST_PARAMS = """CELERY_IMPORTS ADMINS ROUTES CELERY_ACCEPT_CONTENT""".split()
37
37
38 for config_key, config_value in sorted(config.items()):
38 for config_key, config_value in sorted(config.items()):
39 celery_key = config_key.replace('.', '_').upper()
39 celery_key = config_key.replace('.', '_').upper()
40 if celery_key.split('_', 1)[0] not in PREFIXES:
40 if celery_key.split('_', 1)[0] not in PREFIXES:
41 continue
41 continue
42 if not isinstance(config_value, basestring):
42 if not isinstance(config_value, basestring):
43 continue
43 continue
44 if celery_key in LIST_PARAMS:
44 if celery_key in LIST_PARAMS:
45 celery_value = config_value.split()
45 celery_value = config_value.split()
46 elif config_value.isdigit():
46 elif config_value.isdigit():
47 celery_value = int(config_value)
47 celery_value = int(config_value)
48 elif config_value.lower() in ['true', 'false']:
48 elif config_value.lower() in ['true', 'false']:
49 celery_value = config_value.lower() == 'true'
49 celery_value = config_value.lower() == 'true'
50 else:
50 else:
51 celery_value = config_value
51 celery_value = config_value
52 setattr(celery_config, celery_key, celery_value)
52 setattr(celery_config, celery_key, celery_value)
53 return celery_config
53 return celery_config
54
54
55
55
56 # Create celery app from the TurboGears configuration file
56 def make_app():
57 app = celery.Celery()
57 """Create celery app from the TurboGears configuration file"""
58 app.config_from_object(celery_config(tg.config))
58 app = celery.Celery()
59 app.config_from_object(celery_config(tg.config))
60 return app
General Comments 0
You need to be logged in to leave comments. Login now