Changeset 0.32.0 (#206)

This commit is contained in:
2026-03-19 18:50:27 +00:00
parent 6668e546fe
commit b1266b0d7c
283 changed files with 2187 additions and 1055 deletions

View File

@@ -4,7 +4,7 @@ import (
"context"
"fmt"
"git.gobha.me/xcaliber/chat-switchboard/models"
"chat-switchboard/models"
)
type TaskStore struct{}
@@ -149,8 +149,22 @@ func (s *TaskStore) ListAll(ctx context.Context) ([]models.Task, error) {
return s.list(ctx, `ORDER BY created_at DESC`)
}
func (s *TaskStore) ListDue(ctx context.Context, limit int) ([]models.Task, error) {
return s.list(ctx, `WHERE is_active = true AND next_run_at <= NOW() ORDER BY next_run_at ASC LIMIT $1`, limit)
func (s *TaskStore) ClaimDueTask(ctx context.Context) (*models.Task, error) {
t := &models.Task{}
row := DB.QueryRowContext(ctx, `
UPDATE tasks SET next_run_at = NULL
WHERE id = (
SELECT id FROM tasks
WHERE is_active = true AND next_run_at <= NOW()
ORDER BY next_run_at ASC
LIMIT 1
FOR UPDATE SKIP LOCKED
)
RETURNING `+taskColumns)
if err := scanTask(row, t); err != nil {
return nil, err
}
return t, nil
}
// ── Scheduler bookkeeping ──────────────────────
@@ -181,6 +195,25 @@ func (s *TaskStore) CreateRun(ctx context.Context, r *models.TaskRun) error {
).Scan(&r.ID, &r.StartedAt)
}
func (s *TaskStore) CreateRunExclusive(ctx context.Context, taskID string) (*models.TaskRun, error) {
r := &models.TaskRun{}
err := DB.QueryRowContext(ctx, `
INSERT INTO task_runs (task_id, status)
SELECT $1, 'running'
WHERE NOT EXISTS (
SELECT 1 FROM task_runs
WHERE task_id = $1 AND status IN ('running', 'queued')
)
RETURNING id, started_at`, taskID,
).Scan(&r.ID, &r.StartedAt)
if err != nil {
return nil, err
}
r.TaskID = taskID
r.Status = "running"
return r, nil
}
func (s *TaskStore) UpdateRun(ctx context.Context, id, status string, tokensUsed, toolCalls, wallClock int, errMsg string) error {
_, err := DB.ExecContext(ctx, `
UPDATE task_runs SET status = $1, tokens_used = $2, tool_calls = $3,