Skip to content

Commit

Permalink
Add the option to stop an actor
Browse files Browse the repository at this point in the history
  • Loading branch information
albireox committed Oct 27, 2024
1 parent 34b3e49 commit 7371a8f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
11 changes: 11 additions & 0 deletions src/lvmapi/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

from __future__ import annotations

import os

import taskiq_fastapi
from fastapi import FastAPI, Request

Expand Down Expand Up @@ -66,6 +68,15 @@ async def get_id_route(request: Request):
# Add kubernetes API instance to state.
app.state.kubernetes = Kubernetes()

# Fake states for testing.
app.state.use_fake_states = os.environ.get("LVM_USE_FAKE_STATES", "0") != "0"
app.state.fake_states = {
"wind_alert": False,
"humidity_alert": False,
"door_alert": False,
"is_day": False,
}


@app.get("/")
def root(request: Request):
Expand Down
21 changes: 18 additions & 3 deletions src/lvmapi/routers/actors.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@ async def get_actor_health(request: Request) -> list[HealthResponse]:


@router.get("/ping", summary="Actor ping")
async def ping_route(actors: list[str] | None = None) -> dict[str, bool]:
async def get_ping_route(actors: list[str] | None = None) -> dict[str, bool]:
"""Pings a list of actors."""

return await ping_actors(actors=actors)


@router.get("/restart/{actor}", summary="Restart actor")
async def restart_actor_route(actor: str) -> str:
@router.get("/restart/{actor}", summary="Restart an actor")
async def get_restart_actor_route(actor: str) -> str:
"""Restarts an actor. Scheduled as a task and returns the task ID"""

deployment = config["actors.actor_to_deployment"][actor]
Expand All @@ -84,6 +84,21 @@ async def restart_actor_route(actor: str) -> str:
return task.task_id


@router.get("/stop/{actor}", summary="Stop an actor")
async def get_stop_actor_route(actor: str) -> bool:
"""Stops an actor."""

from lvmapi.app import app

deployment = config["actors.actor_to_deployment"][actor]
if deployment is None:
raise ValueError(f"Actor {actor} does not have a deployment.")

app.state.kubernetes.delete_deployment(deployment)

return True


@router.get("/versions", summary="Get actor versions")
async def get_actor_versions_route(
actor: Annotated[
Expand Down

0 comments on commit 7371a8f

Please sign in to comment.