mirror of
https://framagit.org/framasoft/framaspace/argos.git
synced 2025-04-28 09:52:38 +02:00
🤕 — Fix order of tasks sent to agent
This commit is contained in:
parent
be2b4f2114
commit
a601fccad3
2 changed files with 36 additions and 3 deletions
|
@ -7,6 +7,7 @@
|
|||
- 🔊 — Improve check agent log
|
||||
- 🔒️ — Logging out now invalidate tokens
|
||||
- 📝 — Improve OpenAPI doc
|
||||
- 🤕 — Fix order of tasks sent to agent
|
||||
|
||||
## 0.9.0
|
||||
|
||||
|
|
|
@ -18,17 +18,49 @@ from argos.server.settings import read_config
|
|||
|
||||
async def list_tasks(db: Session, agent_id: str, limit: int = 100):
|
||||
"""List tasks and mark them as selected"""
|
||||
# Process tasks which never has been processed first
|
||||
subquery = (
|
||||
db.query(func.distinct(Task.task_group))
|
||||
.filter(
|
||||
Task.selected_by == None, # noqa: E711
|
||||
((Task.next_run <= datetime.now()) | (Task.next_run == None)), # noqa: E711
|
||||
Task.next_run == None, # noqa: E711
|
||||
)
|
||||
.limit(limit)
|
||||
.subquery()
|
||||
)
|
||||
tasks = db.query(Task).filter(Task.task_group.in_(Select(subquery))).all()
|
||||
|
||||
if len(tasks):
|
||||
now = datetime.now()
|
||||
for task in tasks:
|
||||
task.selected_at = now
|
||||
task.selected_by = agent_id
|
||||
db.commit()
|
||||
return tasks
|
||||
|
||||
# Now we can process tasks normally
|
||||
all_task_groups = (
|
||||
db.query(Task.task_group)
|
||||
.filter(
|
||||
Task.selected_by == None, # noqa: E711
|
||||
Task.next_run <= datetime.now(), # noqa: E711
|
||||
)
|
||||
.order_by(asc(Task.next_run))
|
||||
.all()
|
||||
)
|
||||
# We need to do distinct(Task.task_group) in Python
|
||||
# since distinct(Task.task_group) is not compatible with
|
||||
# an order_by(asc(Task.next_run))
|
||||
task_groups: list[str] = []
|
||||
for row in all_task_groups:
|
||||
if len(task_groups) > limit:
|
||||
break
|
||||
task_group = row.task_group
|
||||
if task_group not in task_groups:
|
||||
task_groups.append(task_group)
|
||||
|
||||
tasks = db.query(Task).filter(Task.task_group.in_(task_groups)).all()
|
||||
|
||||
now = datetime.now()
|
||||
for task in tasks:
|
||||
task.selected_at = now
|
||||
|
@ -406,14 +438,14 @@ async def get_severity_counts(db: Session) -> dict:
|
|||
|
||||
async def reschedule_all(db: Session):
|
||||
"""Reschedule checks of all non OK tasks ASAP"""
|
||||
db.query(Task).filter(Task.severity.in_(["warning", "critical", "unknown"])).update(
|
||||
db.query(Task).filter(Task.severity != "ok").update(
|
||||
{Task.next_run: datetime.now() - timedelta(days=1)}
|
||||
)
|
||||
db.commit()
|
||||
|
||||
|
||||
async def remove_old_results(db: Session, max_results_age: float):
|
||||
"""Remove old results, base on age"""
|
||||
"""Remove old results, based on age"""
|
||||
max_acceptable_time = datetime.now() - timedelta(seconds=max_results_age)
|
||||
deleted = (
|
||||
db.query(Result).filter(Result.submitted_at < max_acceptable_time).delete()
|
||||
|
|
Loading…
Reference in a new issue