hostname is now used as agent_id, and stored in the db

This commit is contained in:
Alexis Métaireau 2023-10-18 21:29:09 +02:00
parent cd243d92dc
commit 54379d04c4
3 changed files with 25 additions and 8 deletions

View file

@ -18,9 +18,10 @@ def log_failure(retry_state):
loglevel = logging.WARNING
logger.log(
loglevel,
"Retrying: attempt %s ended with: %s",
"Retrying: attempt %s ended with: %s %s",
retry_state.attempt_number,
retry_state.outcome,
retry_state.outcome.exception(),
)
@ -33,7 +34,9 @@ class ArgosAgent:
self.wait_time = wait_time
self.agent_id = socket.gethostname()
headers = {"Authorization": f"Bearer {auth}"}
headers = {
"Authorization": f"Bearer {auth}",
}
self._http_client = httpx.AsyncClient(headers=headers)
@retry(after=log_failure, wait=wait_random(min=1, max=2))
@ -64,7 +67,10 @@ class ArgosAgent:
async def _get_and_complete_tasks(self):
# Fetch the list of tasks
response = await self._http_client.get(f"{self.server}/api/tasks")
response = await self._http_client.get(
f"{self.server}/api/tasks",
params={"limit": self.max_tasks, "agent_id": self.agent_id},
)
if response.status_code == httpx.codes.OK:
# XXX Maybe we want to group the tests by URL ? (to issue one request per URL)
@ -88,7 +94,9 @@ class ArgosAgent:
async def _post_results(self, results: List[AgentResult]):
data = [r.model_dump() for r in results]
response = await self._http_client.post(f"{self.server}/api/results", json=data)
response = await self._http_client.post(
f"{self.server}/api/results", params={"agent_id": self.agent_id}, json=data
)
if response.status_code == httpx.codes.CREATED:
logger.error(f"Successfully posted results {response.json()}")

View file

@ -80,6 +80,7 @@ class Result(Base):
id: Mapped[int] = mapped_column(primary_key=True)
task_id: Mapped[int] = mapped_column(ForeignKey("tasks.id"))
task: Mapped["Task"] = relationship(back_populates="results")
agent_id: Mapped[str] = mapped_column(nullable=True)
submitted_at: Mapped[datetime] = mapped_column()
status: Mapped[Literal["success", "failure", "error", "on-check"]] = mapped_column(

View file

@ -14,17 +14,24 @@ route = APIRouter()
# XXX Get the default limit from the config
@route.get("/tasks", response_model=list[Task], dependencies=[Depends(verify_token)])
async def read_tasks(request: Request, db: Session = Depends(get_db), limit: int = 20):
# XXX Let the agents specifify their names (and use hostnames)
tasks = await queries.list_tasks(db, agent_id=request.client.host, limit=limit)
async def read_tasks(
request: Request,
db: Session = Depends(get_db),
limit: int = 20,
agent_id: str = None,
):
agent_id = agent_id or request.client.host
tasks = await queries.list_tasks(db, agent_id=agent_id, limit=limit)
return tasks
@route.post("/results", status_code=201, dependencies=[Depends(verify_token)])
async def create_results(
request: Request,
results: List[AgentResult],
db: Session = Depends(get_db),
config: Config = Depends(get_config),
agent_id: str = None,
):
"""Get the results from the agents and store them locally.
@ -33,9 +40,10 @@ async def create_results(
- If it's an error, determine its severity ;
- Trigger the reporting calls
"""
agent_id = agent_id or request.client.host
db_results = []
for agent_result in results:
result = await queries.create_result(db, agent_result)
result = await queries.create_result(db, agent_result, agent_id)
# XXX Maybe offload this to a queue.
# XXX Use a schema for the on-check value.
# XXX Get all the tasks at once, to limit the queries on the db