Skip to content

Commit d783941

Browse files
authored
fix(task): retain the install queue worker across cron reloads (#213)
1 parent e5bae1e commit d783941

2 files changed

Lines changed: 63 additions & 5 deletions

File tree

‎internal/task/task.go‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ type Task struct {
2828
InstallingApps sync.Map
2929
InstallAppQueue chan TaskItem
3030
chExitQueue chan bool
31+
queueStartOnce sync.Once
32+
queueStopOnce sync.Once
3133
InvalidAccounts map[string]bool
3234
// RefreshingDevices prevents concurrent refresh operations for the same device UDID
3335
RefreshingDevices sync.Map
@@ -98,7 +100,10 @@ func new() *Task {
98100

99101
func (t *Task) RunSchedule() error {
100102
if t.c != nil {
101-
t.Stop()
103+
// Updating cron settings must not restart the install worker.
104+
// That worker can still be in its initial 30-second usbmuxd wait.
105+
<-t.c.Stop().Done()
106+
t.c = nil
102107
}
103108

104109
if _, err := cron.ParseStandard(app.Settings.Task.CrodTime); err != nil {
@@ -151,13 +156,20 @@ func (t *Task) Start() {
151156
}
152157
})
153158

154-
go t.runQueue()
159+
// The installation queue belongs to the Task lifetime, not to the
160+
// cron schedule. Repeated settings updates must never spawn new workers.
161+
t.queueStartOnce.Do(func() { go t.runQueue() })
155162
}
156163

164+
// Stop shuts down the Task. RunSchedule only replaces the cron schedule.
157165
func (t *Task) Stop() {
158-
t.chExitQueue <- true
159-
<-t.c.Stop().Done()
160-
t.c = nil
166+
// Closing is non-blocking even when runQueue is still waiting for
167+
// usbmuxd, and the once guard makes repeated Stop calls safe.
168+
t.queueStopOnce.Do(func() { close(t.chExitQueue) })
169+
if t.c != nil {
170+
<-t.c.Stop().Done()
171+
t.c = nil
172+
}
161173
}
162174

163175
// removeStaleTempFiles removes the staged files abandoned in the upload
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package task
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
"github.com/bitxeno/atvloadly/internal/app"
8+
)
9+
10+
// Updating settings repeatedly must not wait for the install worker's
11+
// initial usbmuxd readiness delay (30 seconds on Linux).
12+
func TestRunScheduleRepeatedReloadDoesNotBlock(t *testing.T) {
13+
previous := app.Settings
14+
settings := &app.SettingsConfiguration{}
15+
settings.Task.CrodTime = "0,30 3-6 * * *"
16+
settings.Task.Enabled = true
17+
settings.Update.CheckInterval = 6
18+
app.Settings = settings
19+
defer func() { app.Settings = previous }()
20+
21+
tk := new()
22+
defer tk.Stop()
23+
for i := 0; i < 4; i++ {
24+
result := make(chan error, 1)
25+
go func() { result <- tk.RunSchedule() }()
26+
select {
27+
case err := <-result:
28+
if err != nil {
29+
t.Fatalf("schedule reload %d failed: %v", i+1, err)
30+
}
31+
case <-time.After(2 * time.Second):
32+
t.Fatalf("schedule reload %d blocked while queue worker was starting", i+1)
33+
}
34+
}
35+
}
36+
37+
func TestStopWithoutStartingIsIdempotent(t *testing.T) {
38+
tk := new()
39+
tk.Stop()
40+
tk.Stop()
41+
select {
42+
case <-tk.chExitQueue:
43+
default:
44+
t.Fatal("queue shutdown channel was not closed")
45+
}
46+
}

0 commit comments

Comments
 (0)
⌂ 换网址