Show More
@@ -1,58 +1,72 | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | """ |
|
4 | 4 | Kallithea wrapper of Celery |
|
5 | 5 | |
|
6 | 6 | The Celery configuration is in the Kallithea ini file but must be converted to an |
|
7 | 7 | entirely different format before Celery can use it. |
|
8 | 8 | |
|
9 | 9 | We read the configuration from tg.config at module import time. This module can |
|
10 | 10 | thus not be imported in global scope but must be imported on demand in function |
|
11 | 11 | scope after tg.config has been initialized. |
|
12 | 12 | |
|
13 | 13 | To make sure that the config really has been initialized, we check one of the |
|
14 | 14 | mandatory settings. |
|
15 | 15 | """ |
|
16 | 16 | |
|
17 | import logging | |
|
18 | ||
|
17 | 19 | import celery |
|
18 | 20 | import tg |
|
19 | 21 | |
|
20 | 22 | |
|
21 | 23 | class CeleryConfig(object): |
|
22 | 24 | CELERY_IMPORTS = ['kallithea.lib.celerylib.tasks'] |
|
23 | 25 | CELERY_ACCEPT_CONTENT = ['json'] |
|
24 | 26 | CELERY_RESULT_SERIALIZER = 'json' |
|
25 | 27 | CELERY_TASK_SERIALIZER = 'json' |
|
26 | 28 | |
|
27 | 29 | |
|
30 | desupported = set([ | |
|
31 | 'celery.result.dburi', | |
|
32 | 'celery.result.serialier', | |
|
33 | 'celery.send.task.error.emails', | |
|
34 | ]) | |
|
35 | ||
|
36 | ||
|
37 | log = logging.getLogger(__name__) | |
|
38 | ||
|
39 | ||
|
28 | 40 | def celery_config(config): |
|
29 | 41 | """Return Celery config object populated from relevant settings in a config dict, such as tg.config""" |
|
30 | 42 | |
|
31 | 43 | celery_config = CeleryConfig() |
|
32 | 44 | |
|
33 | 45 | PREFIXES = """ADMINS BROKER CASSANDRA CELERYBEAT CELERYD CELERYMON CELERY EMAIL SERVER""".split() |
|
34 | 46 | LIST_PARAMS = """CELERY_IMPORTS CELERY_ACCEPT_CONTENT""".split() |
|
35 | 47 | |
|
36 | 48 | for config_key, config_value in sorted(config.items()): |
|
49 | if config_key in desupported and config_value: | |
|
50 | log.error('Celery configuration setting %r is no longer supported', config_key) | |
|
37 | 51 | celery_key = config_key.replace('.', '_').upper() |
|
38 | 52 | if celery_key.split('_', 1)[0] not in PREFIXES: |
|
39 | 53 | continue |
|
40 | 54 | if not isinstance(config_value, str): |
|
41 | 55 | continue |
|
42 | 56 | if celery_key in LIST_PARAMS: |
|
43 | 57 | celery_value = config_value.split() |
|
44 | 58 | elif config_value.isdigit(): |
|
45 | 59 | celery_value = int(config_value) |
|
46 | 60 | elif config_value.lower() in ['true', 'false']: |
|
47 | 61 | celery_value = config_value.lower() == 'true' |
|
48 | 62 | else: |
|
49 | 63 | celery_value = config_value |
|
50 | 64 | setattr(celery_config, celery_key, celery_value) |
|
51 | 65 | return celery_config |
|
52 | 66 | |
|
53 | 67 | |
|
54 | 68 | def make_app(): |
|
55 | 69 | """Create celery app from the TurboGears configuration file""" |
|
56 | 70 | app = celery.Celery() |
|
57 | 71 | app.config_from_object(celery_config(tg.config)) |
|
58 | 72 | return app |
General Comments 0
You need to be logged in to leave comments.
Login now