Skip to content

perf(scheduler): add agent_id filter to SchedulerStorage::list_workflows #1249

@geoffjay

Description

@geoffjay

Context

Surfaced as a non-blocking suggestion in PR #1248 (code review item 4).

`Scheduler::restart_workflows_for_agent` calls `self.storage.list_workflows(None)` which loads all workflows from the database and filters by `agent_id` in Rust:

```rust
// scheduler/mod.rs
let workflows = self.storage.list_workflows(None).await?;
for workflow in workflows {
if workflow.agent_id != agent_id || !workflow.enabled {
continue;
}
...
}
```

`SchedulerStorage::list_workflows` currently only supports filtering by `project_id`, not `agent_id`. This means every agent restart performs a full table scan and discards unrelated rows in the application layer.

Why it matters

Restarts are rare so this is not an immediate problem, but as the workflow count grows this becomes a linear scan per restart. A targeted SQL query with a `WHERE agent_id = ?` clause would keep it constant regardless of total workflow count.

Proposed change

  1. Add a `list_workflows_for_agent` method to `SchedulerStorage`:

```rust
/// Lists all enabled workflows for a specific agent, newest first.
pub async fn list_workflows_for_agent(&self, agent_id: &Uuid) -> Result<Vec> {
let models = workflow_entity::Entity::find()
.filter(
Condition::all()
.add(workflow_entity::Column::AgentId.eq(agent_id.to_string()))
.add(workflow_entity::Column::Enabled.eq(1)),
)
.order_by(workflow_entity::Column::CreatedAt, Order::Desc)
.all(&self.db)
.await?;
models.into_iter().map(model_to_workflow).collect()
}
```

  1. Use it in `Scheduler::restart_workflows_for_agent` instead of `list_workflows(None)`.

Files

  • `crates/orchestrator/src/scheduler/storage.rs` — add `list_workflows_for_agent`
  • `crates/orchestrator/src/scheduler/mod.rs` — update `restart_workflows_for_agent` to use it

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions