summaryrefslogtreecommitdiff
path: root/scripts/reset-failed-tasks
blob: 1f3b6d53c6e151a6221800dc16cbcd8aac3fc82d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/bin/bash
# Reset FAILED and CANCELLED tasks to PENDING and delete their preserved workspaces.
# Usage: reset-failed-tasks [--dry-run]

set -euo pipefail

DB_PATH="${CLAUDOMATOR_DB:-/site/doot.terst.org/data/claudomator.db}"
DRY_RUN=false
[[ "${1:-}" == "--dry-run" ]] && DRY_RUN=true

# Collect preserved sandbox dirs before resetting so we can clean them up.
SANDBOX_DIRS=$(sqlite3 "$DB_PATH" "
  SELECT DISTINCT e.sandbox_dir
  FROM executions e
  JOIN tasks t ON t.id = e.task_id
  WHERE t.state IN ('FAILED','CANCELLED')
    AND e.sandbox_dir IS NOT NULL
    AND e.sandbox_dir != '';
")

TASK_COUNT=$(sqlite3 "$DB_PATH" "SELECT COUNT(*) FROM tasks WHERE state IN ('FAILED','CANCELLED');")

echo "Tasks to reset: $TASK_COUNT"

if [[ "$DRY_RUN" == "true" ]]; then
  echo "[dry-run] Would reset $TASK_COUNT task(s) to PENDING."
  if [[ -n "$SANDBOX_DIRS" ]]; then
    echo "[dry-run] Workspaces to delete:"
    echo "$SANDBOX_DIRS"
  else
    echo "[dry-run] No preserved workspaces to delete."
  fi
  exit 0
fi

sqlite3 "$DB_PATH" "UPDATE tasks SET state = 'PENDING' WHERE state IN ('FAILED','CANCELLED');"
echo "Reset $TASK_COUNT task(s) to PENDING."

DELETED=0
while IFS= read -r dir; do
  [[ -z "$dir" ]] && continue
  if [[ -d "$dir" ]]; then
    rm -rf "$dir"
    echo "Deleted workspace: $dir"
    DELETED=$((DELETED + 1))
  fi
done <<< "$SANDBOX_DIRS"

echo "Deleted $DELETED workspace(s)."