|
1 | 1 | import logging |
2 | 2 | import ssl |
| 3 | +from typing import Any |
3 | 4 |
|
4 | 5 | from celery import Celery # type: ignore[import-untyped] |
5 | 6 | from settings_library.celery import CelerySettings |
|
8 | 9 | _logger = logging.getLogger(__name__) |
9 | 10 |
|
10 | 11 |
|
| 12 | +def _celery_configure(celery_settings: CelerySettings) -> dict[str, Any]: |
| 13 | + base_config = { |
| 14 | + "broker_connection_retry_on_startup": True, |
| 15 | + "result_expires": celery_settings.CELERY_RESULT_EXPIRES, |
| 16 | + "result_extended": True, |
| 17 | + "result_serializer": "json", |
| 18 | + "task_send_sent_event": True, |
| 19 | + "task_track_started": True, |
| 20 | + "worker_send_task_events": True, |
| 21 | + } |
| 22 | + if celery_settings.CELERY_REDIS_RESULT_BACKEND.REDIS_SECURE: |
| 23 | + base_config["redis_backend_use_ssl"] = {"ssl_cert_reqs": ssl.CERT_NONE} |
| 24 | + return base_config |
| 25 | + |
| 26 | + |
11 | 27 | def create_app(celery_settings: CelerySettings) -> Celery: |
12 | 28 | assert celery_settings |
13 | 29 |
|
14 | | - app = Celery( |
| 30 | + return Celery( |
15 | 31 | broker=celery_settings.CELERY_RABBIT_BROKER.dsn, |
16 | 32 | backend=celery_settings.CELERY_REDIS_RESULT_BACKEND.build_redis_dsn( |
17 | 33 | RedisDatabase.CELERY_TASKS, |
18 | 34 | ), |
| 35 | + **_celery_configure(celery_settings), |
19 | 36 | ) |
20 | | - app.conf.broker_connection_retry_on_startup = True |
21 | | - # NOTE: disable SSL cert validation (https://github.com/ITISFoundation/osparc-simcore/pull/7407) |
22 | | - if celery_settings.CELERY_REDIS_RESULT_BACKEND.REDIS_SECURE: |
23 | | - app.conf.redis_backend_use_ssl = {"ssl_cert_reqs": ssl.CERT_NONE} |
24 | | - app.conf.result_expires = celery_settings.CELERY_RESULT_EXPIRES |
25 | | - app.conf.result_extended = True # original args are included in the results |
26 | | - app.conf.result_serializer = "json" |
27 | | - app.conf.task_send_sent_event = True |
28 | | - app.conf.task_track_started = True |
29 | | - app.conf.worker_send_task_events = True # enable tasks monitoring |
30 | | - |
31 | | - return app |
|
0 commit comments