diff --git a/CHANGELOG.md b/CHANGELOG.md index 602fa74..3430cb0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ — 🔊 — Add a warning messages in the logs if there is no tasks in database. (fix #41) - ✨ — Add command to generate example configuration (fix #38) - 📝 — Improve documentation +- ✨ — Add command to warn if it’s been long since last viewing an agent (fix #49) ## 0.1.1 diff --git a/argos/commands.py b/argos/commands.py index cf9d976..8a76256 100644 --- a/argos/commands.py +++ b/argos/commands.py @@ -2,6 +2,7 @@ import asyncio import os from functools import wraps from pathlib import Path +from sys import exit as sysexit from uuid import uuid4 import click @@ -177,6 +178,41 @@ async def cleandb(max_results, max_lock_seconds, config): click.echo(f"{updated} locks released") +@server.command() +@click.option( + "--time-without-agent", + default=5, + help="Time without seeing an agent after which a warning will be issued, in minutes. " + "Default is 5 minutes.", + callback=validate_max_results, +) +@click.option( + "--config", + default="config.yaml", + help="Path of the configuration file. " + "If ARGOS_YAML_FILE environment variable is set, its value will be used instead.", + envvar="ARGOS_YAML_FILE", + callback=validate_config_access, +) +@coroutine +async def watch_agents(time_without_agent, config): + """Watch agents (to run routinely) + + Issues a warning if no agent has been seen by the server for a given time. + """ + # It’s mandatory to do it before the imports + os.environ["ARGOS_YAML_FILE"] = config + + # The imports are made here otherwise the agent will need server configuration files. + from argos.server import queries + + db = await get_db() + agents = await queries.get_recent_agents_count(db, time_without_agent) + if agents == 0: + click.echo(f"No agent has been seen in the last {time_without_agent} minutes.") + sysexit(1) + + @server.command(short_help="Load or reload tasks’ configuration") @click.option( "--config", diff --git a/argos/server/queries.py b/argos/server/queries.py index f9c07f5..e08cf7b 100644 --- a/argos/server/queries.py +++ b/argos/server/queries.py @@ -241,3 +241,11 @@ async def release_old_locks(db: Session, max_lock_seconds: int): ) db.commit() return updated + + +async def get_recent_agents_count(db: Session, minutes: int): + """Get agents seen less than ago""" + max_time = datetime.now() - timedelta(minutes=minutes) + + agents = db.query(Result.agent_id).filter(Result.submitted_at > max_time).distinct() + return agents.count() diff --git a/docs/cli.md b/docs/cli.md index 16057bc..81a18a0 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -83,6 +83,7 @@ Commands: migrate Run database migrations reload-config Load or reload tasks’ configuration start Starts the server (use only for testing or development!) + watch-agents Watch agents (to run routinely) ``` +### Server watch-agents + + + +```man +Usage: argos server cleandb [OPTIONS] + + Clean the database (to run routinely) + + - Removes old results from the database. + - Removes locks from tasks that have been locked for too long. + +Options: + --max-results INTEGER Number of results per task to keep + --max-lock-seconds INTEGER The number of seconds after which a lock is + considered stale, must be higher than 60 (the + checks have a timeout value of 60 seconds) + --config TEXT Path of the configuration file. If ARGOS_YAML_FILE + environment variable is set, its value will be + used instead. + --help Show this message and exit. +``` + + + ### Server reload-config