|
10 | 10 | import alembic.command as alembic_command |
11 | 11 | import alembic.config as alembic_config |
12 | 12 | import alembic.migration as alembic_migration |
| 13 | +import redis |
13 | 14 | from calypsso import get_calypsso_app |
14 | 15 | from fastapi import FastAPI, HTTPException, Request, Response, status |
15 | 16 | from fastapi.encoders import jsonable_encoder |
|
56 | 57 | if TYPE_CHECKING: |
57 | 58 | import redis |
58 | 59 |
|
| 60 | + from app.types.factory import Factory |
| 61 | + |
59 | 62 |
|
60 | 63 | # NOTE: We can not get loggers at the top of this file like we do in other files |
61 | 64 | # as the loggers are not yet initialized |
@@ -226,6 +229,66 @@ def initialize_schools( |
226 | 229 | ) |
227 | 230 |
|
228 | 231 |
|
| 232 | +async def run_factories( |
| 233 | + db: AsyncSession, |
| 234 | + settings: Settings, |
| 235 | + hyperion_error_logger: logging.Logger, |
| 236 | +) -> None: |
| 237 | + """Run the factories to create default data in the database""" |
| 238 | + if not settings.USE_FACTORIES: |
| 239 | + return |
| 240 | + |
| 241 | + hyperion_error_logger.info("Startup: Factories enabled") |
| 242 | + # Importing the core_factory at the beginning of the factories. |
| 243 | + factories_list: list[Factory] = [] |
| 244 | + for module in all_modules: |
| 245 | + if module.factory: |
| 246 | + factories_list.append(module.factory) |
| 247 | + hyperion_error_logger.info( |
| 248 | + f"Module {module.root} declares a factory {module.factory.__class__.__name__} with dependencies {module.factory.depends_on}", |
| 249 | + ) |
| 250 | + else: |
| 251 | + hyperion_error_logger.warning( |
| 252 | + f"Module {module.root} does not declare a factory. It won't provide any base data.", |
| 253 | + ) |
| 254 | + |
| 255 | + # We have to run the factories in a specific order to make sure the dependencies are met |
| 256 | + # For that reason, we will run the first factory that has no dependencies, after that we remove it from the list of the dependencies from the other factories |
| 257 | + # And we loop until there are no more factories to run and we use a boolean to avoid infinite loops with circular dependencies |
| 258 | + no_factory_run_during_last_loop = False |
| 259 | + ran_factories: list[type[Factory]] = [] |
| 260 | + while len(factories_list) > 0 and not no_factory_run_during_last_loop: |
| 261 | + no_factory_run_during_last_loop = True |
| 262 | + for factory in factories_list: |
| 263 | + if all(depend in ran_factories for depend in factory.depends_on): |
| 264 | + no_factory_run_during_last_loop = False |
| 265 | + # Check if the factory should be run |
| 266 | + if await factory.should_run(db): |
| 267 | + hyperion_error_logger.info( |
| 268 | + f"Startup: Running factory {factory.__class__.__name__}", |
| 269 | + ) |
| 270 | + try: |
| 271 | + await factory.run(db, settings) |
| 272 | + except Exception as error: |
| 273 | + hyperion_error_logger.fatal( |
| 274 | + f"Startup: Could not run factories: {error}", |
| 275 | + ) |
| 276 | + raise |
| 277 | + else: |
| 278 | + hyperion_error_logger.info( |
| 279 | + f"Startup: Factory {factory.__class__.__name__} is not necessary, skipping it", |
| 280 | + ) |
| 281 | + ran_factories.append(factory.__class__) |
| 282 | + factories_list.remove(factory) |
| 283 | + break |
| 284 | + if no_factory_run_during_last_loop: |
| 285 | + hyperion_error_logger.error( |
| 286 | + "Factories are not correctly configured, some factories are not running.", |
| 287 | + ) |
| 288 | + break |
| 289 | + hyperion_error_logger.info("Startup: Factories have been run") |
| 290 | + |
| 291 | + |
229 | 292 | def initialize_module_visibility( |
230 | 293 | sync_engine: Engine, |
231 | 294 | hyperion_error_logger: logging.Logger, |
@@ -487,7 +550,18 @@ async def init_lifespan( |
487 | 550 | get_db, |
488 | 551 | get_db, |
489 | 552 | ) |
490 | | - |
| 553 | + # We need to run the factories only once across all the workers |
| 554 | + async for db in get_db_dependency(state): |
| 555 | + await initialization.use_lock_for_workers( |
| 556 | + run_factories, |
| 557 | + "run_factories", |
| 558 | + redis_client, |
| 559 | + number_of_workers, |
| 560 | + hyperion_error_logger, |
| 561 | + db=db, |
| 562 | + settings=settings, |
| 563 | + hyperion_error_logger=hyperion_error_logger, |
| 564 | + ) |
491 | 565 | async for db in get_db_dependency(state): |
492 | 566 | await initialization.use_lock_for_workers( |
493 | 567 | init_google_API, |
|
0 commit comments