60 lines
1.7 KiB
Python
60 lines
1.7 KiB
Python
import asyncio
|
|
import logging
|
|
|
|
from aiohttp import web
|
|
from aiohttp.web import Request, Response
|
|
from aiohttp.web_runner import GracefulExit
|
|
|
|
from app.library.config import Config
|
|
from app.library.encoder import Encoder
|
|
from app.library.Events import EventBus, Events
|
|
from app.library.router import route
|
|
|
|
LOG: logging.Logger = logging.getLogger(__name__)
|
|
|
|
|
|
@route("POST", "api/system/shutdown", "system.shutdown")
|
|
async def shutdown_system(request: Request, config: Config, encoder: Encoder, notify: EventBus) -> Response:
|
|
"""
|
|
Get the presets.
|
|
|
|
Args:
|
|
request (Request): The request object.
|
|
config (Config): The config instance.
|
|
encoder (Encoder): The encoder instance.
|
|
notify (EventBus): The event bus instance.
|
|
|
|
Returns:
|
|
Response: The response object.
|
|
|
|
"""
|
|
if config.is_native is not True:
|
|
return web.json_response(
|
|
{"error": "Shutdown is only available in the native mode."},
|
|
status=web.HTTPBadRequest.status_code,
|
|
)
|
|
|
|
app = request.app
|
|
|
|
async def do_shutdown():
|
|
await notify.emit(
|
|
Events.SHUTDOWN,
|
|
data={"app": app},
|
|
title="Application Shutdown",
|
|
message="Shutdown initiated by user request.",
|
|
)
|
|
await asyncio.sleep(0.5)
|
|
await app.shutdown()
|
|
await app.cleanup()
|
|
raise GracefulExit
|
|
|
|
# Schedule shutdown after response
|
|
asyncio.create_task(do_shutdown())
|
|
LOG.info("Shutdown initiated by user request. Stopping the server...")
|
|
return web.json_response(
|
|
data={
|
|
"message": "The application shutting down.",
|
|
},
|
|
status=web.HTTPOk.status_code,
|
|
dumps=encoder.encode,
|
|
)
|