fa72f1b323
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
170 lines
4.8 KiB
Go
170 lines
4.8 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net/http"
|
|
|
|
"github.com/vasyansk/imap-copier/internal/crypto"
|
|
"github.com/vasyansk/imap-copier/internal/csvimport"
|
|
"github.com/vasyansk/imap-copier/internal/orchestrator"
|
|
"github.com/vasyansk/imap-copier/internal/store"
|
|
)
|
|
|
|
func (s *Server) handleImportCSV(w http.ResponseWriter, r *http.Request) {
|
|
taskID, err := pathID(r, "id")
|
|
if err != nil {
|
|
http.Error(w, "bad id", http.StatusBadRequest)
|
|
return
|
|
}
|
|
file, _, err := r.FormFile("file")
|
|
if err != nil {
|
|
http.Error(w, "file required", http.StatusBadRequest)
|
|
return
|
|
}
|
|
defer file.Close()
|
|
rows, err := csvimport.Parse(file)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
for _, row := range rows {
|
|
srcEnc, err := crypto.Encrypt(s.cfg.EncKey, []byte(row.SrcPass))
|
|
if err != nil {
|
|
http.Error(w, "encrypt", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
dstEnc, err := crypto.Encrypt(s.cfg.EncKey, []byte(row.DstPass))
|
|
if err != nil {
|
|
http.Error(w, "encrypt", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
if _, err := s.store.CreateAccount(r.Context(), store.Account{
|
|
TaskID: taskID, SrcLogin: row.SrcLogin, SrcPassEnc: srcEnc,
|
|
DstLogin: row.DstLogin, DstPassEnc: dstEnc,
|
|
}); err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
}
|
|
writeJSON(w, http.StatusCreated, map[string]int{"imported": len(rows)})
|
|
}
|
|
|
|
func (s *Server) handleTestAccounts(w http.ResponseWriter, r *http.Request) {
|
|
taskID, err := pathID(r, "id")
|
|
if err != nil {
|
|
http.Error(w, "bad id", http.StatusBadRequest)
|
|
return
|
|
}
|
|
// Detach from the request context: the request context is cancelled when
|
|
// this handler returns, which would otherwise kill the background test run.
|
|
ctx := context.WithoutCancel(r.Context())
|
|
go s.orch.TestAccounts(ctx, taskID) // прогресс через WS
|
|
w.WriteHeader(http.StatusAccepted)
|
|
}
|
|
|
|
func (s *Server) handleRun(w http.ResponseWriter, r *http.Request) {
|
|
taskID, err := pathID(r, "id")
|
|
if err != nil {
|
|
http.Error(w, "bad id", http.StatusBadRequest)
|
|
return
|
|
}
|
|
runID, err := s.orch.Run(r.Context(), taskID)
|
|
if errors.Is(err, orchestrator.ErrNotTested) {
|
|
http.Error(w, "accounts must pass connection tests first", http.StatusConflict)
|
|
return
|
|
}
|
|
if errors.Is(err, orchestrator.ErrAlreadyRunning) {
|
|
http.Error(w, "task is already running", http.StatusConflict)
|
|
return
|
|
}
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusAccepted, map[string]int64{"run_id": runID})
|
|
}
|
|
|
|
func (s *Server) handleCancelAccount(w http.ResponseWriter, r *http.Request) {
|
|
taskID, err := pathID(r, "id")
|
|
if err != nil {
|
|
http.Error(w, "bad id", http.StatusBadRequest)
|
|
return
|
|
}
|
|
accID, err := pathID(r, "accountId")
|
|
if err != nil {
|
|
http.Error(w, "bad account id", http.StatusBadRequest)
|
|
return
|
|
}
|
|
// Live in-flight copy: signal it to stop.
|
|
if s.orch.CancelAccount(accID) {
|
|
w.WriteHeader(http.StatusAccepted)
|
|
return
|
|
}
|
|
// No live goroutine but the DB may still say "running" (stale state left by
|
|
// a crash/restart): clear it so the account/task become usable again.
|
|
cleared, err := s.store.ClearStuckAccount(r.Context(), accID)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
if !cleared {
|
|
http.Error(w, "account is not running", http.StatusConflict)
|
|
return
|
|
}
|
|
if err := s.store.ReconcileTaskStatus(r.Context(), taskID); err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
w.WriteHeader(http.StatusAccepted)
|
|
}
|
|
|
|
func (s *Server) handleDeleteTask(w http.ResponseWriter, r *http.Request) {
|
|
id, err := pathID(r, "id")
|
|
if err != nil {
|
|
http.Error(w, "bad id", http.StatusBadRequest)
|
|
return
|
|
}
|
|
task, err := s.store.GetTask(r.Context(), id)
|
|
if err != nil {
|
|
http.Error(w, "not found", http.StatusNotFound)
|
|
return
|
|
}
|
|
if task.Status == "running" {
|
|
http.Error(w, "cannot delete a running task", http.StatusConflict)
|
|
return
|
|
}
|
|
if err := s.store.DeleteTask(r.Context(), id); err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|
|
|
|
func (s *Server) handleDeleteAccount(w http.ResponseWriter, r *http.Request) {
|
|
taskID, err := pathID(r, "id")
|
|
if err != nil {
|
|
http.Error(w, "bad id", http.StatusBadRequest)
|
|
return
|
|
}
|
|
accID, err := pathID(r, "accountId")
|
|
if err != nil {
|
|
http.Error(w, "bad account id", http.StatusBadRequest)
|
|
return
|
|
}
|
|
task, err := s.store.GetTask(r.Context(), taskID)
|
|
if err != nil {
|
|
http.Error(w, "not found", http.StatusNotFound)
|
|
return
|
|
}
|
|
if task.Status == "running" {
|
|
http.Error(w, "cannot modify accounts while task is running", http.StatusConflict)
|
|
return
|
|
}
|
|
if err := s.store.DeleteAccount(r.Context(), accID); err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|