Show More
@@ -1,58 +1,72 | |||||
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 logging | |||
|
18 | ||||
17 | import celery |
|
19 | import celery | |
18 | import tg |
|
20 | import tg | |
19 |
|
21 | |||
20 |
|
22 | |||
21 | class CeleryConfig(object): |
|
23 | class CeleryConfig(object): | |
22 | CELERY_IMPORTS = ['kallithea.lib.celerylib.tasks'] |
|
24 | CELERY_IMPORTS = ['kallithea.lib.celerylib.tasks'] | |
23 | CELERY_ACCEPT_CONTENT = ['json'] |
|
25 | CELERY_ACCEPT_CONTENT = ['json'] | |
24 | CELERY_RESULT_SERIALIZER = 'json' |
|
26 | CELERY_RESULT_SERIALIZER = 'json' | |
25 | CELERY_TASK_SERIALIZER = 'json' |
|
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 | def celery_config(config): |
|
40 | def celery_config(config): | |
29 | """Return Celery config object populated from relevant settings in a config dict, such as tg.config""" |
|
41 | """Return Celery config object populated from relevant settings in a config dict, such as tg.config""" | |
30 |
|
42 | |||
31 | celery_config = CeleryConfig() |
|
43 | celery_config = CeleryConfig() | |
32 |
|
44 | |||
33 | PREFIXES = """ADMINS BROKER CASSANDRA CELERYBEAT CELERYD CELERYMON CELERY EMAIL SERVER""".split() |
|
45 | PREFIXES = """ADMINS BROKER CASSANDRA CELERYBEAT CELERYD CELERYMON CELERY EMAIL SERVER""".split() | |
34 | LIST_PARAMS = """CELERY_IMPORTS CELERY_ACCEPT_CONTENT""".split() |
|
46 | LIST_PARAMS = """CELERY_IMPORTS CELERY_ACCEPT_CONTENT""".split() | |
35 |
|
47 | |||
36 | for config_key, config_value in sorted(config.items()): |
|
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 | celery_key = config_key.replace('.', '_').upper() |
|
51 | celery_key = config_key.replace('.', '_').upper() | |
38 | if celery_key.split('_', 1)[0] not in PREFIXES: |
|
52 | if celery_key.split('_', 1)[0] not in PREFIXES: | |
39 | continue |
|
53 | continue | |
40 | if not isinstance(config_value, str): |
|
54 | if not isinstance(config_value, str): | |
41 | continue |
|
55 | continue | |
42 | if celery_key in LIST_PARAMS: |
|
56 | if celery_key in LIST_PARAMS: | |
43 | celery_value = config_value.split() |
|
57 | celery_value = config_value.split() | |
44 | elif config_value.isdigit(): |
|
58 | elif config_value.isdigit(): | |
45 | celery_value = int(config_value) |
|
59 | celery_value = int(config_value) | |
46 | elif config_value.lower() in ['true', 'false']: |
|
60 | elif config_value.lower() in ['true', 'false']: | |
47 | celery_value = config_value.lower() == 'true' |
|
61 | celery_value = config_value.lower() == 'true' | |
48 | else: |
|
62 | else: | |
49 | celery_value = config_value |
|
63 | celery_value = config_value | |
50 | setattr(celery_config, celery_key, celery_value) |
|
64 | setattr(celery_config, celery_key, celery_value) | |
51 | return celery_config |
|
65 | return celery_config | |
52 |
|
66 | |||
53 |
|
67 | |||
54 | def make_app(): |
|
68 | def make_app(): | |
55 | """Create celery app from the TurboGears configuration file""" |
|
69 | """Create celery app from the TurboGears configuration file""" | |
56 | app = celery.Celery() |
|
70 | app = celery.Celery() | |
57 | app.config_from_object(celery_config(tg.config)) |
|
71 | app.config_from_object(celery_config(tg.config)) | |
58 | return app |
|
72 | return app |
General Comments 0
You need to be logged in to leave comments.
Login now