Added hosted services #186
Some checks failed
Test before pr merge / test-lint (pull_request) Failing after 6s
Build on push / prepare (push) Successful in 8s
Build on push / query (push) Successful in 18s
Build on push / core (push) Successful in 18s
Build on push / dependency (push) Successful in 17s
Build on push / application (push) Successful in 15s
Build on push / translation (push) Successful in 17s
Build on push / database (push) Successful in 18s
Build on push / mail (push) Successful in 24s
Build on push / auth (push) Successful in 14s
Build on push / api (push) Successful in 14s
Some checks failed
Test before pr merge / test-lint (pull_request) Failing after 6s
Build on push / prepare (push) Successful in 8s
Build on push / query (push) Successful in 18s
Build on push / core (push) Successful in 18s
Build on push / dependency (push) Successful in 17s
Build on push / application (push) Successful in 15s
Build on push / translation (push) Successful in 17s
Build on push / database (push) Successful in 18s
Build on push / mail (push) Successful in 24s
Build on push / auth (push) Successful in 14s
Build on push / api (push) Successful in 14s
This commit is contained in:
@@ -1 +1,2 @@
|
||||
from .application_builder import ApplicationBuilder
|
||||
from .host import Host
|
||||
@@ -84,7 +84,7 @@ class ApplicationABC(ABC):
|
||||
Called by custom Application.main
|
||||
"""
|
||||
try:
|
||||
Host.run(self.main)
|
||||
Host.run_app(self.main)
|
||||
except KeyboardInterrupt:
|
||||
pass
|
||||
|
||||
|
||||
@@ -6,27 +6,78 @@ from cpl.dependency.hosted.startup_task import StartupTask
|
||||
|
||||
|
||||
class Host:
|
||||
_loop = asyncio.get_event_loop()
|
||||
_loop: asyncio.AbstractEventLoop | None = None
|
||||
_tasks: dict = {}
|
||||
|
||||
@classmethod
|
||||
def get_loop(cls):
|
||||
def get_loop(cls) -> asyncio.AbstractEventLoop:
|
||||
if cls._loop is None:
|
||||
cls._loop = asyncio.new_event_loop()
|
||||
asyncio.set_event_loop(cls._loop)
|
||||
return cls._loop
|
||||
|
||||
@classmethod
|
||||
def run_start_tasks(cls):
|
||||
provider = get_provider()
|
||||
tasks = provider.get_services(StartupTask)
|
||||
loop = cls.get_loop()
|
||||
|
||||
for task in tasks:
|
||||
if asyncio.iscoroutinefunction(task.run):
|
||||
cls._loop.run_until_complete(task.run())
|
||||
loop.run_until_complete(task.run())
|
||||
else:
|
||||
task.run()
|
||||
|
||||
@classmethod
|
||||
def run(cls, func: Callable, *args, **kwargs):
|
||||
cls.run_start_tasks()
|
||||
def run_hosted_services(cls):
|
||||
provider = get_provider()
|
||||
services = provider.get_hosted_services()
|
||||
loop = cls.get_loop()
|
||||
|
||||
for service in services:
|
||||
if asyncio.iscoroutinefunction(service.start):
|
||||
cls._tasks[service] = loop.create_task(service.start())
|
||||
|
||||
@classmethod
|
||||
async def _stop_all(cls):
|
||||
for service in cls._tasks.keys():
|
||||
if asyncio.iscoroutinefunction(service.stop):
|
||||
await service.stop()
|
||||
|
||||
for task in cls._tasks.values():
|
||||
task.cancel()
|
||||
|
||||
cls._tasks.clear()
|
||||
|
||||
@classmethod
|
||||
def run_app(cls, func: Callable, *args, **kwargs):
|
||||
loop = cls.get_loop()
|
||||
|
||||
cls.run_start_tasks()
|
||||
cls.run_hosted_services()
|
||||
|
||||
async def runner():
|
||||
try:
|
||||
if asyncio.iscoroutinefunction(func):
|
||||
app_task = asyncio.create_task(func(*args, **kwargs))
|
||||
else:
|
||||
loop = asyncio.get_running_loop()
|
||||
app_task = loop.run_in_executor(None, func, *args, **kwargs)
|
||||
|
||||
await asyncio.wait(
|
||||
[app_task, *cls._tasks.values()],
|
||||
return_when=asyncio.FIRST_COMPLETED,
|
||||
)
|
||||
except (KeyboardInterrupt, asyncio.CancelledError):
|
||||
pass
|
||||
finally:
|
||||
await cls._stop_all()
|
||||
|
||||
loop.run_until_complete(runner())
|
||||
|
||||
@classmethod
|
||||
def run(cls, func: Callable, *args, **kwargs):
|
||||
if asyncio.iscoroutinefunction(func):
|
||||
return cls._loop.run_until_complete(func(*args, **kwargs))
|
||||
|
||||
return func(*args, **kwargs)
|
||||
return func(*args, **kwargs)
|
||||
Reference in New Issue
Block a user