refactor(desktop-task): extract recovery select query and scan task status
Pull the recovered-task SELECT into a dedicated helper so the column list and lease-expiry filter are testable, add a status column to the scan so logs report the real task state, and log scan/iterate failures with the client and recovery mode for observability. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDesktopTaskRecoverySelectQueryMatchesRecoveredLeaseScanOrder(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
query := desktopTaskRecoverySelectQuery(desktopTaskRecoveryModeStartup)
|
||||
gotColumns := recoverDesktopTaskSelectColumns(query)
|
||||
wantColumns := []string{
|
||||
"desktop_id",
|
||||
"workspace_id",
|
||||
"kind",
|
||||
"status",
|
||||
"active_attempt_id",
|
||||
}
|
||||
|
||||
if len(gotColumns) != len(wantColumns) {
|
||||
t.Fatalf("recover select columns = %v, want %v", gotColumns, wantColumns)
|
||||
}
|
||||
for index := range wantColumns {
|
||||
if gotColumns[index] != wantColumns[index] {
|
||||
t.Fatalf("recover select columns = %v, want %v", gotColumns, wantColumns)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDesktopTaskRecoverySelectQueryLeaseExpiryFiltersExpiredLeases(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
query := desktopTaskRecoverySelectQuery(desktopTaskRecoveryModeLeaseExpiry)
|
||||
|
||||
for _, fragment := range []string{
|
||||
"AND lease_expires_at IS NOT NULL",
|
||||
"AND lease_expires_at < NOW()",
|
||||
"FOR UPDATE",
|
||||
} {
|
||||
if !strings.Contains(query, fragment) {
|
||||
t.Fatalf("recover lease-expiry query missing %q: %s", fragment, query)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func recoverDesktopTaskSelectColumns(query string) []string {
|
||||
re := regexp.MustCompile(`(?is)select\s+(.*?)\s+from\s+desktop_tasks`)
|
||||
match := re.FindStringSubmatch(query)
|
||||
if len(match) != 2 {
|
||||
return nil
|
||||
}
|
||||
|
||||
rawColumns := strings.Split(match[1], ",")
|
||||
columns := make([]string, 0, len(rawColumns))
|
||||
for _, rawColumn := range rawColumns {
|
||||
column := strings.TrimSpace(rawColumn)
|
||||
column = strings.Fields(column)[0]
|
||||
columns = append(columns, column)
|
||||
}
|
||||
return columns
|
||||
}
|
||||
Reference in New Issue
Block a user