feat: improve daemon resilience, streaming, and usenet downloads
- Add daemon state persistence and stale resume file cleanup - Add TriggerPoll for WebSocket resume actions - Improve stream server with graceful shutdown and connection tracking - Add desktop notifications for download completion - Add media file organization with Movies/TV Shows detection - Improve usenet downloader with progress tracking and resume support - Add self-update package with GitHub release verification - Downgrade tablewriter to v0.0.5 (v1.x API breaking change)
This commit is contained in:
parent
e332c0a6e4
commit
197e33956a
24 changed files with 2310 additions and 84 deletions
|
|
@ -82,15 +82,18 @@ func TestHeartbeat(t *testing.T) {
|
|||
if req.AgentID != "agent-123" {
|
||||
t.Errorf("agentId = %q, want agent-123", req.AgentID)
|
||||
}
|
||||
json.NewEncoder(w).Encode(StatusResponse{Success: true})
|
||||
json.NewEncoder(w).Encode(HeartbeatResponse{Success: true})
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
c := NewClient(srv.URL, "test-key", "unarr-test")
|
||||
err := c.Heartbeat(context.Background(), HeartbeatRequest{AgentID: "agent-123"})
|
||||
resp, err := c.Heartbeat(context.Background(), HeartbeatRequest{AgentID: "agent-123"})
|
||||
if err != nil {
|
||||
t.Fatalf("Heartbeat failed: %v", err)
|
||||
}
|
||||
if !resp.Success {
|
||||
t.Error("expected success=true")
|
||||
}
|
||||
}
|
||||
|
||||
func TestClaimTasks(t *testing.T) {
|
||||
|
|
@ -115,21 +118,21 @@ func TestClaimTasks(t *testing.T) {
|
|||
defer srv.Close()
|
||||
|
||||
c := NewClient(srv.URL, "test-key", "unarr-test")
|
||||
tasks, err := c.ClaimTasks(context.Background(), "agent-123")
|
||||
resp, err := c.ClaimTasks(context.Background(), "agent-123")
|
||||
if err != nil {
|
||||
t.Fatalf("ClaimTasks failed: %v", err)
|
||||
}
|
||||
if len(tasks) != 1 {
|
||||
t.Fatalf("len(tasks) = %d, want 1", len(tasks))
|
||||
if len(resp.Tasks) != 1 {
|
||||
t.Fatalf("len(tasks) = %d, want 1", len(resp.Tasks))
|
||||
}
|
||||
if tasks[0].ID != "task-uuid-1" {
|
||||
t.Errorf("task.ID = %q, want task-uuid-1", tasks[0].ID)
|
||||
if resp.Tasks[0].ID != "task-uuid-1" {
|
||||
t.Errorf("task.ID = %q, want task-uuid-1", resp.Tasks[0].ID)
|
||||
}
|
||||
if tasks[0].InfoHash != "abc123def456abc123def456abc123def456abc1" {
|
||||
t.Errorf("task.InfoHash = %q", tasks[0].InfoHash)
|
||||
if resp.Tasks[0].InfoHash != "abc123def456abc123def456abc123def456abc1" {
|
||||
t.Errorf("task.InfoHash = %q", resp.Tasks[0].InfoHash)
|
||||
}
|
||||
if tasks[0].PreferredMethod != "auto" {
|
||||
t.Errorf("task.PreferredMethod = %q, want auto", tasks[0].PreferredMethod)
|
||||
if resp.Tasks[0].PreferredMethod != "auto" {
|
||||
t.Errorf("task.PreferredMethod = %q, want auto", resp.Tasks[0].PreferredMethod)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -177,12 +180,12 @@ func TestClaimTasksEmpty(t *testing.T) {
|
|||
defer srv.Close()
|
||||
|
||||
c := NewClient(srv.URL, "test-key", "unarr-test")
|
||||
tasks, err := c.ClaimTasks(context.Background(), "agent-123")
|
||||
resp, err := c.ClaimTasks(context.Background(), "agent-123")
|
||||
if err != nil {
|
||||
t.Fatalf("ClaimTasks failed: %v", err)
|
||||
}
|
||||
if len(tasks) != 0 {
|
||||
t.Errorf("expected empty tasks, got %d", len(tasks))
|
||||
if len(resp.Tasks) != 0 {
|
||||
t.Errorf("expected empty tasks, got %d", len(resp.Tasks))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -276,10 +279,107 @@ func TestUserAgent(t *testing.T) {
|
|||
if r.Header.Get("User-Agent") != "unarr/0.2.0" {
|
||||
t.Errorf("User-Agent = %q, want unarr/0.2.0", r.Header.Get("User-Agent"))
|
||||
}
|
||||
json.NewEncoder(w).Encode(StatusResponse{Success: true})
|
||||
json.NewEncoder(w).Encode(HeartbeatResponse{Success: true})
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
c := NewClient(srv.URL, "test-key", "unarr/0.2.0")
|
||||
c.Heartbeat(context.Background(), HeartbeatRequest{AgentID: "x"})
|
||||
}
|
||||
|
||||
func TestHeartbeatWithUpgradeSignal(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
json.NewEncoder(w).Encode(HeartbeatResponse{
|
||||
Success: true,
|
||||
Upgrade: &UpgradeSignal{Version: "2.0.0"},
|
||||
})
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
c := NewClient(srv.URL, "test-key", "unarr-test")
|
||||
resp, err := c.Heartbeat(context.Background(), HeartbeatRequest{AgentID: "agent-1"})
|
||||
if err != nil {
|
||||
t.Fatalf("Heartbeat failed: %v", err)
|
||||
}
|
||||
if resp.Upgrade == nil {
|
||||
t.Fatal("expected upgrade signal, got nil")
|
||||
}
|
||||
if resp.Upgrade.Version != "2.0.0" {
|
||||
t.Errorf("upgrade version = %q, want 2.0.0", resp.Upgrade.Version)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHeartbeatWithoutUpgradeSignal(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
json.NewEncoder(w).Encode(HeartbeatResponse{Success: true})
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
c := NewClient(srv.URL, "test-key", "unarr-test")
|
||||
resp, err := c.Heartbeat(context.Background(), HeartbeatRequest{AgentID: "agent-1"})
|
||||
if err != nil {
|
||||
t.Fatalf("Heartbeat failed: %v", err)
|
||||
}
|
||||
if resp.Upgrade != nil {
|
||||
t.Errorf("expected no upgrade signal, got %+v", resp.Upgrade)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReportUpgradeResult(t *testing.T) {
|
||||
var received UpgradeResult
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/api/internal/agent/upgrade-result" {
|
||||
t.Errorf("path = %s, want /api/internal/agent/upgrade-result", r.URL.Path)
|
||||
}
|
||||
if r.Method != http.MethodPost {
|
||||
t.Errorf("method = %s, want POST", r.Method)
|
||||
}
|
||||
json.NewDecoder(r.Body).Decode(&received)
|
||||
json.NewEncoder(w).Encode(struct{ Success bool }{Success: true})
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
c := NewClient(srv.URL, "test-key", "unarr-test")
|
||||
err := c.ReportUpgradeResult(context.Background(), UpgradeResult{
|
||||
AgentID: "agent-1",
|
||||
Success: true,
|
||||
Version: "2.0.0",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("ReportUpgradeResult failed: %v", err)
|
||||
}
|
||||
if received.AgentID != "agent-1" {
|
||||
t.Errorf("agentId = %q, want agent-1", received.AgentID)
|
||||
}
|
||||
if !received.Success {
|
||||
t.Error("expected success=true")
|
||||
}
|
||||
if received.Version != "2.0.0" {
|
||||
t.Errorf("version = %q, want 2.0.0", received.Version)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReportUpgradeResultFailure(t *testing.T) {
|
||||
var received UpgradeResult
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
json.NewDecoder(r.Body).Decode(&received)
|
||||
json.NewEncoder(w).Encode(struct{ Success bool }{Success: true})
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
c := NewClient(srv.URL, "test-key", "unarr-test")
|
||||
err := c.ReportUpgradeResult(context.Background(), UpgradeResult{
|
||||
AgentID: "agent-1",
|
||||
Success: false,
|
||||
Error: "checksum mismatch",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("ReportUpgradeResult failed: %v", err)
|
||||
}
|
||||
if received.Success {
|
||||
t.Error("expected success=false")
|
||||
}
|
||||
if received.Error != "checksum mismatch" {
|
||||
t.Errorf("error = %q, want 'checksum mismatch'", received.Error)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -44,6 +44,9 @@ type Daemon struct {
|
|||
// Exposed tickers for hot-reload
|
||||
PollTicker *time.Ticker
|
||||
HeartbeatTicker *time.Ticker
|
||||
|
||||
// pollNow triggers an immediate poll (e.g. on resume)
|
||||
pollNow chan struct{}
|
||||
}
|
||||
|
||||
// NewDaemon creates a daemon with the given transport.
|
||||
|
|
@ -59,6 +62,7 @@ func NewDaemon(cfg DaemonConfig, transport Transport) *Daemon {
|
|||
return &Daemon{
|
||||
cfg: cfg,
|
||||
transport: transport,
|
||||
pollNow: make(chan struct{}, 1),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -151,6 +155,9 @@ func (d *Daemon) Run(ctx context.Context) error {
|
|||
if d.transport.Mode() == "http" {
|
||||
d.poll(ctx)
|
||||
}
|
||||
|
||||
case <-d.pollNow:
|
||||
d.poll(ctx)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -236,6 +243,15 @@ func (d *Daemon) handleEvent(event ServerEvent) {
|
|||
}
|
||||
}
|
||||
|
||||
// TriggerPoll requests an immediate task poll cycle.
|
||||
// Used when a resume event is received to pick up re-pending tasks faster.
|
||||
func (d *Daemon) TriggerPoll() {
|
||||
select {
|
||||
case d.pollNow <- struct{}{}:
|
||||
default: // already pending
|
||||
}
|
||||
}
|
||||
|
||||
// ClearUpgradeInProgress resets the upgrade flag so a retry can be attempted.
|
||||
func (d *Daemon) ClearUpgradeInProgress() {
|
||||
d.upgradeInProgress = false
|
||||
|
|
|
|||
72
internal/agent/state.go
Normal file
72
internal/agent/state.go
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
package agent
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/torrentclaw/torrentclaw-cli/internal/config"
|
||||
)
|
||||
|
||||
// DaemonState is written to disk every heartbeat for external tools to read.
|
||||
type DaemonState struct {
|
||||
AgentID string `json:"agentId"`
|
||||
Status string `json:"status"` // running | upgrading | shutting_down
|
||||
Version string `json:"version"`
|
||||
PID int `json:"pid"`
|
||||
StartedAt time.Time `json:"startedAt"`
|
||||
LastHeartbeat time.Time `json:"lastHeartbeat"`
|
||||
ActiveTasks int `json:"activeTasks"`
|
||||
CompletedCount int `json:"completedCount"`
|
||||
FailedCount int `json:"failedCount"`
|
||||
TotalDownloaded int64 `json:"totalDownloaded"`
|
||||
MethodStats map[string]int `json:"methodStats,omitempty"`
|
||||
}
|
||||
|
||||
// stateFilePathFn is overridable for testing.
|
||||
var stateFilePathFn = func() string {
|
||||
return filepath.Join(config.DataDir(), "daemon.state.json")
|
||||
}
|
||||
|
||||
// StateFilePath returns the path to the daemon state file.
|
||||
func StateFilePath() string {
|
||||
return stateFilePathFn()
|
||||
}
|
||||
|
||||
// WriteState writes the daemon state to disk (best-effort, never errors).
|
||||
func WriteState(state *DaemonState) {
|
||||
path := StateFilePath()
|
||||
dir := filepath.Dir(path)
|
||||
os.MkdirAll(dir, 0o755)
|
||||
|
||||
data, err := json.MarshalIndent(state, "", " ")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// Write to temp file then rename for atomicity
|
||||
tmp := path + ".tmp"
|
||||
if err := os.WriteFile(tmp, data, 0o644); err != nil {
|
||||
return
|
||||
}
|
||||
os.Rename(tmp, path)
|
||||
}
|
||||
|
||||
// ReadState reads the daemon state from disk. Returns nil if not found.
|
||||
func ReadState() *DaemonState {
|
||||
data, err := os.ReadFile(StateFilePath())
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
var state DaemonState
|
||||
if json.Unmarshal(data, &state) != nil {
|
||||
return nil
|
||||
}
|
||||
return &state
|
||||
}
|
||||
|
||||
// RemoveState deletes the state file (called on clean shutdown).
|
||||
func RemoveState() {
|
||||
os.Remove(StateFilePath())
|
||||
}
|
||||
106
internal/agent/state_test.go
Normal file
106
internal/agent/state_test.go
Normal file
|
|
@ -0,0 +1,106 @@
|
|||
package agent
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestWriteAndReadState(t *testing.T) {
|
||||
// Override the state file path for testing
|
||||
tmpDir := t.TempDir()
|
||||
origFn := stateFilePathFn
|
||||
stateFilePathFn = func() string { return filepath.Join(tmpDir, "daemon.state.json") }
|
||||
defer func() { stateFilePathFn = origFn }()
|
||||
|
||||
state := &DaemonState{
|
||||
AgentID: "agent-123",
|
||||
Status: "running",
|
||||
Version: "1.0.0",
|
||||
PID: 12345,
|
||||
StartedAt: time.Now().Truncate(time.Second),
|
||||
LastHeartbeat: time.Now().Truncate(time.Second),
|
||||
ActiveTasks: 3,
|
||||
CompletedCount: 10,
|
||||
FailedCount: 2,
|
||||
TotalDownloaded: 1024 * 1024 * 500,
|
||||
MethodStats: map[string]int{"torrent": 8, "debrid": 2},
|
||||
}
|
||||
|
||||
WriteState(state)
|
||||
|
||||
read := ReadState()
|
||||
if read == nil {
|
||||
t.Fatal("ReadState() returned nil")
|
||||
}
|
||||
if read.AgentID != "agent-123" {
|
||||
t.Errorf("AgentID = %q, want agent-123", read.AgentID)
|
||||
}
|
||||
if read.Status != "running" {
|
||||
t.Errorf("Status = %q, want running", read.Status)
|
||||
}
|
||||
if read.Version != "1.0.0" {
|
||||
t.Errorf("Version = %q, want 1.0.0", read.Version)
|
||||
}
|
||||
if read.PID != 12345 {
|
||||
t.Errorf("PID = %d, want 12345", read.PID)
|
||||
}
|
||||
if read.ActiveTasks != 3 {
|
||||
t.Errorf("ActiveTasks = %d, want 3", read.ActiveTasks)
|
||||
}
|
||||
if read.CompletedCount != 10 {
|
||||
t.Errorf("CompletedCount = %d, want 10", read.CompletedCount)
|
||||
}
|
||||
if read.MethodStats["torrent"] != 8 {
|
||||
t.Errorf("MethodStats[torrent] = %d, want 8", read.MethodStats["torrent"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadStateNotFound(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
origFn := stateFilePathFn
|
||||
stateFilePathFn = func() string { return filepath.Join(tmpDir, "nonexistent.json") }
|
||||
defer func() { stateFilePathFn = origFn }()
|
||||
|
||||
state := ReadState()
|
||||
if state != nil {
|
||||
t.Errorf("ReadState() = %+v, want nil for missing file", state)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemoveState(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
origFn := stateFilePathFn
|
||||
stateFilePathFn = func() string { return filepath.Join(tmpDir, "daemon.state.json") }
|
||||
defer func() { stateFilePathFn = origFn }()
|
||||
|
||||
WriteState(&DaemonState{AgentID: "test"})
|
||||
|
||||
// Verify file exists
|
||||
path := StateFilePath()
|
||||
if _, err := os.Stat(path); err != nil {
|
||||
t.Fatalf("state file should exist: %v", err)
|
||||
}
|
||||
|
||||
RemoveState()
|
||||
|
||||
if _, err := os.Stat(path); !os.IsNotExist(err) {
|
||||
t.Error("state file should be removed after RemoveState()")
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadStateCorruptedJSON(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
origFn := stateFilePathFn
|
||||
path := filepath.Join(tmpDir, "daemon.state.json")
|
||||
stateFilePathFn = func() string { return path }
|
||||
defer func() { stateFilePathFn = origFn }()
|
||||
|
||||
os.WriteFile(path, []byte("not valid json{{{"), 0o644)
|
||||
|
||||
state := ReadState()
|
||||
if state != nil {
|
||||
t.Errorf("ReadState() should return nil for corrupted JSON, got %+v", state)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue