Files
imap-copier/internal/store/recovery.go
T
vasyansk fa72f1b323 fix: recover from phantom 'running' state after crash/restart
The run-cancel registry is in-memory; a container restart mid-run leaves
accounts/tasks persisted as 'running' with no goroutine, wedging cancel
(not-in-map -> 409) and blocking remove/re-run.

- startup: ResetRunningOnStartup clears stale 'running' -> 'idle' on boot
- cancel handler: when no live goroutine, ClearStuckAccount + ReconcileTaskStatus
  reset the stuck account (and its task) instead of returning 409

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MMHQTtnQtQqL8muAXHr9kd
2026-07-02 12:57:39 +07:00

45 lines
1.7 KiB
Go

package store
import "context"
// ResetRunningOnStartup clears phantom "running" statuses left behind when the
// process died mid-run (crash, container restart). A fresh process has no
// in-flight goroutines, so any persisted "running" is stale and would otherwise
// wedge the task (the run-guard refuses to start, and accounts can't be edited).
// Returns how many task and account rows were reset.
func (s *Store) ResetRunningOnStartup(ctx context.Context) (tasks int64, accounts int64, err error) {
ct, err := s.Pool.Exec(ctx, `UPDATE accounts SET status='idle' WHERE status='running'`)
if err != nil {
return 0, 0, err
}
accounts = ct.RowsAffected()
ct, err = s.Pool.Exec(ctx, `UPDATE tasks SET status='idle' WHERE status='running'`)
if err != nil {
return 0, accounts, err
}
tasks = ct.RowsAffected()
return tasks, accounts, nil
}
// ClearStuckAccount resets an account stuck in "running" (no live goroutine) to
// "idle". Returns true if a stuck row was actually cleared.
func (s *Store) ClearStuckAccount(ctx context.Context, accountID int64) (bool, error) {
ct, err := s.Pool.Exec(ctx, `UPDATE accounts SET status='idle' WHERE id=$1 AND status='running'`, accountID)
if err != nil {
return false, err
}
return ct.RowsAffected() == 1, nil
}
// ReconcileTaskStatus moves a task out of "running" once none of its accounts
// are still running — used after clearing a stuck account so the task can be
// re-run or deleted again.
func (s *Store) ReconcileTaskStatus(ctx context.Context, taskID int64) error {
_, err := s.Pool.Exec(ctx,
`UPDATE tasks SET status='idle'
WHERE id=$1 AND status='running'
AND NOT EXISTS (SELECT 1 FROM accounts WHERE task_id=$1 AND status='running')`,
taskID)
return err
}