Files
go-sip/internal/store/local_v04_cleanup.go
T

91 lines
4.0 KiB
Go

package store
import (
"database/sql"
"errors"
"fmt"
"log"
)
// LocalTaskConfigEviction reports a narrowly scoped cache eviction. BlockedBy
// is a durable reason to retry later, not permission to discard recovery data.
type LocalTaskConfigEviction struct {
Deleted int64
BlockedBy string
}
// EvictTerminalTaskConfig explicitly removes only a terminal task's derived
// HTTP task-config copy. It never removes command identities, assignments,
// execution snapshots, reservations, uploads, terminal facts or outbox rows:
// publisher confirm is not SaaS application receipt. There is no automatic
// migration or sweep of existing data.
func (s *Store) EvictTerminalTaskConfig(dispatcherID, taskID string) (LocalTaskConfigEviction, error) {
if dispatcherID == "" || taskID == "" {
return LocalTaskConfigEviction{}, ErrLocalTaskUnassigned
}
s.mu.Lock()
defer s.mu.Unlock()
tx, err := s.db.Begin()
if err != nil {
return LocalTaskConfigEviction{}, err
}
defer tx.Rollback()
var tenantID, status, admission string
var removed int
err = tx.QueryRow(`SELECT tenant_id,saas_status,admission_state,removed FROM local_v01_task_assignments
WHERE dispatcher_id=? AND task_id=?`, dispatcherID, taskID).Scan(&tenantID, &status, &admission, &removed)
if errors.Is(err, sql.ErrNoRows) {
return LocalTaskConfigEviction{}, ErrLocalTaskUnassigned
}
if err != nil {
return LocalTaskConfigEviction{}, fmt.Errorf("load task before cache eviction: %w", err)
}
terminal := func(value string) bool { return value == "stopped" || value == "finished" || value == "removed" }
if !terminal(status) || !terminal(admission) || (removed != 0 && status != "removed") {
log.Printf("task config eviction blocked dispatcher_id=%s task_id=%s reason=task_active", dispatcherID, taskID)
return LocalTaskConfigEviction{BlockedBy: "task_active"}, nil
}
checks := []struct {
reason string
query string
args []any
}{
{"execution_unconfirmed", `SELECT EXISTS(SELECT 1 FROM tasks t
LEFT JOIN reservations r ON r.execution_id=t.execution_id
LEFT JOIN local_v01_call_terminals c ON c.execution_id=t.execution_id
LEFT JOIN outbox o ON o.event_id=c.result_event_id
WHERE t.tenant_id=? AND t.task_id=? AND
(t.status!='finished' OR r.reservation_id IS NULL OR r.state!='released' OR
c.execution_id IS NULL OR c.result_event_id IS NULL OR o.status IS NULL OR o.status!='published'))`, []any{tenantID, taskID}},
{"quota_occupied", `SELECT EXISTS(SELECT 1 FROM quotas WHERE scope IN (?,?) AND (reserved_value!=0 OR unknown_value!=0))`,
[]any{LocalTaskQuotaScope(dispatcherID, tenantID, taskID), LocalTenantQuotaScope(dispatcherID, tenantID)}},
{"upload_incomplete", `SELECT EXISTS(SELECT 1 FROM uploads WHERE state='granted')`, nil},
{"outbox_pending", `SELECT EXISTS(SELECT 1 FROM outbox WHERE status!='published')`, nil},
}
for _, check := range checks {
var blocked int
if err := tx.QueryRow(check.query, check.args...).Scan(&blocked); err != nil {
return LocalTaskConfigEviction{}, fmt.Errorf("check %s before task cache eviction: %w", check.reason, err)
}
if blocked != 0 {
log.Printf("task config eviction blocked dispatcher_id=%s task_id=%s reason=%s", dispatcherID, taskID, check.reason)
return LocalTaskConfigEviction{BlockedBy: check.reason}, nil
}
}
result, err := tx.Exec(`DELETE FROM local_v01_config_snapshots WHERE dispatcher_id=? AND resource='task' AND tenant_id=? AND task_id=?`,
dispatcherID, tenantID, taskID)
if err != nil {
return LocalTaskConfigEviction{}, fmt.Errorf("evict terminal task config: %w", err)
}
deleted, err := result.RowsAffected()
if err != nil || deleted > 1 {
return LocalTaskConfigEviction{}, fmt.Errorf("terminal task config rows=%d: %w", deleted, errors.Join(err, ErrCommandConflict))
}
if err := tx.Commit(); err != nil {
return LocalTaskConfigEviction{}, fmt.Errorf("commit terminal task config eviction: %w", err)
}
log.Printf("task config eviction completed dispatcher_id=%s task_id=%s deleted=%d", dispatcherID, taskID, deleted)
return LocalTaskConfigEviction{Deleted: deleted}, nil
}