Skip to content

Commit

Permalink
fix: startup and background services being called before extensions a…
Browse files Browse the repository at this point in the history
…re initialized
  • Loading branch information
livioribeiro committed Jan 13, 2025
1 parent adfea1d commit bc19a1d
Showing 1 changed file with 10 additions and 16 deletions.
26 changes: 10 additions & 16 deletions src/selva/web/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,33 +105,27 @@ async def _initialize_middleware(self):
)

async def _lifespan_startup(self):
await self._initialize_extensions()
await self._initialize_middleware()

for hook in self.startup:
await call_with_dependencies(self.di, hook)

for hook in self.background_services:

async def wrapper():
try:
await call_with_dependencies(self.di, hook)
except asyncio.CancelledError:
pass

task = asyncio.create_task(wrapper())
task = asyncio.create_task(call_with_dependencies(self.di, hook))
self._background_services.add(task)

def done_callback(task):
if err := task.exception():
raise err
self._background_services.discard(task)
def done_callback(done):
if err := done.exception():
logger.exception(err, exc_info=err)
self._background_services.discard(done)

task.add_done_callback(done_callback)

await self._initialize_extensions()
await self._initialize_middleware()

async def _lifespan_shutdown(self):
for task in self._background_services:
task.cancel()
if not task.done():
task.cancel()

await self.di.run_finalizers()

Expand Down

0 comments on commit bc19a1d

Please sign in to comment.