|
| 1 | +import os |
| 2 | +from typing import List, Literal, TypedDict, Optional |
| 3 | + |
| 4 | + |
| 5 | +class CeleryConfig(TypedDict): |
| 6 | + task_serializer: Literal['json', 'pickle', 'msgpack'] |
| 7 | + result_serializer: Literal['json', 'pickle', 'msgpack'] |
| 8 | + accept_content: List[Literal['json', 'pickle', 'msgpack']] |
| 9 | + timezone: str |
| 10 | + enable_utc: bool |
| 11 | + |
| 12 | + |
| 13 | +VALID_SERIALIZERS: List[ |
| 14 | + Literal['json', 'pickle', 'msgpack'] |
| 15 | +] = ['json', 'pickle', 'msgpack'] |
| 16 | + |
| 17 | + |
1 | 18 | class Config: |
2 | | - CELERY_BROKER_URL = 'amqp://guest:guest@localhost:5672//' |
3 | | - CELERY_RESULT_BACKEND = 'redis://localhost:6380/0' |
4 | | - CELERY_CONFIG = { |
5 | | - 'task_serializer': 'json', |
6 | | - 'result_serializer': 'json', |
7 | | - 'accept_content': ['json'], |
8 | | - 'timezone': 'UTC', |
9 | | - 'enable_utc': True, |
10 | | - } |
| 19 | + def __init__( |
| 20 | + self, |
| 21 | + celery_broker_url: Optional[str] = None, |
| 22 | + celery_result_backend: Optional[str] = None, |
| 23 | + celery_config: Optional[CeleryConfig] = None, |
| 24 | + ): |
| 25 | + """ |
| 26 | + Initialize configuration with optional overrides. |
| 27 | +
|
| 28 | + Args: |
| 29 | + celery_broker_url: Celery broker URL. Defaults to environment |
| 30 | + variable or 'amqp://guest:guest@localhost:5672//'. |
| 31 | + celery_result_backend: Celery result backend. Defaults to |
| 32 | + environment variable or 'redis://localhost:6380/0'. |
| 33 | + celery_config: Celery configuration dictionary. Defaults to |
| 34 | + environment variable or a dictionary with the following keys |
| 35 | + and values: |
| 36 | + { |
| 37 | + 'task_serializer': 'json', |
| 38 | + 'result_serializer': 'json', |
| 39 | + 'accept_content': ['json'], |
| 40 | + 'timezone': 'UTC', |
| 41 | + 'enable_utc': True |
| 42 | + } |
| 43 | + """ |
| 44 | + # Global serializer from environment variable |
| 45 | + global_serializer = next( |
| 46 | + ( |
| 47 | + serializer for serializer in VALID_SERIALIZERS |
| 48 | + if serializer == os.getenv('FLOWGRID_SERIALIZER', '') |
| 49 | + ), |
| 50 | + '' |
| 51 | + ) |
| 52 | + |
| 53 | + # Celery Broker URL |
| 54 | + self.celery_broker_url = celery_broker_url or os.getenv( |
| 55 | + 'FLOWGRID_CELERY_BROKER_URL', |
| 56 | + 'amqp://guest:guest@localhost:5672//', |
| 57 | + ) |
| 58 | + |
| 59 | + # Celery Result Backend |
| 60 | + self.celery_result_backend = celery_result_backend or os.getenv( |
| 61 | + 'FLOWGRID_CELERY_RESULT_BACKEND', |
| 62 | + 'redis://localhost:6380/0', |
| 63 | + ) |
| 64 | + |
| 65 | + # Celery Configuration |
| 66 | + if celery_config is None: |
| 67 | + self.celery_config: CeleryConfig = { |
| 68 | + 'task_serializer': next( |
| 69 | + ( |
| 70 | + serializer for serializer in VALID_SERIALIZERS |
| 71 | + if serializer == os.getenv('FLOWGRID_TASK_SERIALIZER', '') # noqa E501 |
| 72 | + ), |
| 73 | + global_serializer or 'json' |
| 74 | + ), |
| 75 | + 'result_serializer': next( |
| 76 | + ( |
| 77 | + serializer for serializer in VALID_SERIALIZERS |
| 78 | + if serializer == os.getenv('FLOWGRID_RESULT_SERIALIZER', '') # noqa E501 |
| 79 | + ), |
| 80 | + global_serializer or 'json' |
| 81 | + ), |
| 82 | + 'accept_content': [ |
| 83 | + next( |
| 84 | + ( |
| 85 | + serializer for serializer in VALID_SERIALIZERS |
| 86 | + if serializer == os.getenv('FLOWGRID_ACCEPT_CONTENT', '') # noqa E501 |
| 87 | + ), |
| 88 | + global_serializer or 'json' |
| 89 | + ) |
| 90 | + ], |
| 91 | + 'timezone': os.getenv('FLOWGRID_TIMEZONE', 'UTC'), |
| 92 | + 'enable_utc': os.getenv( |
| 93 | + 'FLOWGRID_ENABLE_UTC', |
| 94 | + 'True', |
| 95 | + ).lower() == 'true' |
| 96 | + } |
| 97 | + else: |
| 98 | + self.celery_config = celery_config |
| 99 | + |
| 100 | + self.validate() |
| 101 | + |
| 102 | + def validate(self) -> None: |
| 103 | + """ |
| 104 | + Validate the configuration values. |
| 105 | +
|
| 106 | + Raises: |
| 107 | + ValueError: If any configuration value is invalid. |
| 108 | + """ |
| 109 | + # Validate Celery broker URL |
| 110 | + if not self.celery_broker_url.startswith(('amqp://', 'amqps://')): |
| 111 | + raise ValueError( |
| 112 | + f'Invalid Celery broker URL: {self.celery_broker_url}' |
| 113 | + ) |
| 114 | + |
| 115 | + # Validate Celery result backend |
| 116 | + if not self.celery_result_backend.startswith( |
| 117 | + ('redis://', 'rediss://') |
| 118 | + ): |
| 119 | + raise ValueError( |
| 120 | + f'Invalid Celery result backend: {self.celery_result_backend}' |
| 121 | + ) |
| 122 | + |
| 123 | + # Validate Celery config |
| 124 | + if self.celery_config['task_serializer'] not in VALID_SERIALIZERS: |
| 125 | + raise ValueError( |
| 126 | + f'Invalid task serializer: {self.celery_config['task_serializer']}' # noqa E501 |
| 127 | + ) |
| 128 | + |
| 129 | + if self.celery_config['result_serializer'] not in VALID_SERIALIZERS: |
| 130 | + raise ValueError( |
| 131 | + f'Invalid result serializer: {self.celery_config['result_serializer']}' # noqa E501 |
| 132 | + ) |
| 133 | + |
| 134 | + # Validate accept_content |
| 135 | + if not all( |
| 136 | + content in VALID_SERIALIZERS |
| 137 | + for content in self.celery_config['accept_content'] |
| 138 | + ): |
| 139 | + raise ValueError( |
| 140 | + f'Invalid accept_content: {self.celery_config['accept_content']}' # noqa E501 |
| 141 | + ) |
| 142 | + |
| 143 | + @classmethod |
| 144 | + def from_env(cls) -> 'Config': |
| 145 | + """ |
| 146 | + Create a configuration instance from environment variables. |
| 147 | +
|
| 148 | + Returns: |
| 149 | + Config: A new configuration instance loaded from environment |
| 150 | + variables. |
| 151 | + """ |
| 152 | + return cls() |
0 commit comments