-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_manager.go
More file actions
302 lines (260 loc) · 8.62 KB
/
task_manager.go
File metadata and controls
302 lines (260 loc) · 8.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package main
import (
"context"
"log"
"sync"
)
// DownloadTask represents a download job
type DownloadTask struct {
VideoID string
SourceURL string
Title string
}
// TaskManager manages download tasks
type TaskManager struct {
TaskQueue chan DownloadTask
PriorityQueue chan DownloadTask
RunningTasks sync.Map // map[string]context.CancelFunc
CurrentTaskID string
mu sync.Mutex
}
var taskManager *TaskManager
// NewTaskManager creates a new task manager
func NewTaskManager() *TaskManager {
return &TaskManager{
TaskQueue: make(chan DownloadTask, 1000), // Buffer for pending tasks
PriorityQueue: make(chan DownloadTask, 1000), // High priority buffer
}
}
// Start begins processing tasks
func (tm *TaskManager) Start() {
go func() {
for {
var task DownloadTask
select {
case task = <-tm.PriorityQueue:
// Got priority task
default:
// No priority task, check normal queue
select {
case task = <-tm.PriorityQueue:
// Double check priority
case task = <-tm.TaskQueue:
// Got normal task
}
}
log.Printf("Processing task: %s (%s)", task.Title, task.VideoID)
tm.processTask(task)
}
}()
}
// Add adds a task to the queue
func (tm *TaskManager) Add(task DownloadTask) {
tm.TaskQueue <- task
}
// ResumeDownloads finds unfinished tasks and adds them to the queue
func (tm *TaskManager) ResumeDownloads(store *Store) {
store.RLock()
// Collect tasks first to avoid potential deadlock if channel blocks and processTask needs Lock
var tasksToResume []DownloadTask
for _, v := range store.Videos {
if v.Status == "pending" || v.Status == "downloading" {
tasksToResume = append(tasksToResume, DownloadTask{
VideoID: v.ID,
SourceURL: v.SourceURL,
Title: v.Title,
})
}
}
store.RUnlock()
count := 0
for _, task := range tasksToResume {
log.Printf("Resuming download for: %s", task.Title)
tm.Add(task)
count++
}
log.Printf("Resumed %d tasks", count)
}
// CancelTask cancels a running task
func (tm *TaskManager) CancelTask(videoID string) {
if cancel, ok := tm.RunningTasks.Load(videoID); ok {
log.Printf("Cancelling task: %s", videoID)
cancel.(context.CancelFunc)()
tm.RunningTasks.Delete(videoID)
}
}
// RetryWithPreemption adds a task to priority queue and preempts current task
func (tm *TaskManager) RetryWithPreemption(retryTask DownloadTask) {
tm.mu.Lock()
currentID := tm.CurrentTaskID
tm.mu.Unlock()
// 1. Queue the retry task first (so it runs immediately after current stops)
tm.PriorityQueue <- retryTask
// 2. If something is running, interrupt it
if currentID != "" {
// Check if it's the same task (don't interrupt itself)
if currentID == retryTask.VideoID {
return
}
if cancel, ok := tm.RunningTasks.Load(currentID); ok {
log.Printf("Preempting task %s for retry of %s", currentID, retryTask.Title)
// Get current task info to re-queue
if video, found := store.GetVideo(currentID); found {
// Re-queue current task to priority queue (after retry task)
tm.PriorityQueue <- DownloadTask{
VideoID: video.ID,
SourceURL: video.SourceURL,
Title: video.Title,
}
// Reset status to pending so it doesn't stay as "downloading"
store.UpdateVideoStatus(currentID, "pending")
}
// Cancel execution
cancel.(context.CancelFunc)()
}
}
}
func (tm *TaskManager) processTask(task DownloadTask) {
// Add retry logic to run after task completes (success or failure)
defer tm.AutoRetryAllPending(task.VideoID)
// Update CurrentTaskID
tm.mu.Lock()
tm.CurrentTaskID = task.VideoID
tm.mu.Unlock()
defer func() {
tm.mu.Lock()
if tm.CurrentTaskID == task.VideoID {
tm.CurrentTaskID = ""
}
tm.mu.Unlock()
}()
// Check if video still exists (wasn't deleted while in queue)
_, exists := store.GetVideo(task.VideoID)
if !exists {
log.Printf("Task skipped (video deleted): %s", task.Title)
return
}
// Setup context for cancellation
ctx, cancel := context.WithCancel(context.Background())
tm.RunningTasks.Store(task.VideoID, cancel)
defer func() {
cancel()
tm.RunningTasks.Delete(task.VideoID)
}()
// Update status to downloading
if err := store.UpdateVideoStatus(task.VideoID, "downloading"); err != nil {
log.Printf("Task stopped (video deleted?): %s", task.Title)
return
}
// 1. Prepare download (parse m3u8, create local file, get fragments)
// Even if it was partially downloaded, SetupDownload will regenerate the list
// and DownloadSegments will skip existing files.
// NOTE: SetupDownload overwrites index.m3u8. This is fine.
_, fragments, err := SetupDownload(task.SourceURL, task.VideoID)
if err != nil {
// Check if preempted/cancelled
if ctx.Err() != nil {
log.Printf("Task preempted/cancelled during setup: %s", task.Title)
return
}
log.Printf("Setup failed for %s: %v", task.Title, err)
store.UpdateVideoStatus(task.VideoID, "error")
return
}
// 2. Download segments
if err := DownloadSegments(ctx, fragments, func(downloaded, total int) {
store.UpdateVideoProgress(task.VideoID, downloaded, total)
}); err != nil {
// Check if preempted/cancelled
if ctx.Err() != nil {
log.Printf("Task preempted/cancelled during download: %s", task.Title)
return
}
log.Printf("Download failed (or stopped) for %s: %v", task.Title, err)
store.UpdateVideoStatus(task.VideoID, "error")
return
}
// 3. Complete
if err := store.UpdateVideoStatus(task.VideoID, "completed"); err != nil {
log.Printf("Failed to mark completed (video deleted?): %s", task.Title)
return
}
log.Printf("Completed download for: %s", task.Title)
// Trigger cleaner
if cleaner != nil {
go cleaner.CheckAndClean()
}
}
// AutoRetryAllPending finds all uncompleted tasks and queues them
func (tm *TaskManager) AutoRetryAllPending(finishedTaskID string) {
// Strategy:
// Find ALL tasks that are (error OR pending) and NOT current task
// Sort them by ID (time)
// Add them to queue if they are likely not in queue (hard to know, but safe to re-add)
// Actually, if we just add them all, we might duplicate.
// But the user wants "try one by one from the first".
// If we add them to the queue, they will be processed in order.
// To avoid flooding the queue with duplicates every time a task finishes:
// We should probably only add ONE task at a time?
// The user said "after completing a task, sequentially retry ALL preceding unfinished tasks".
// This implies a continuous process.
// If we just add the *next* oldest task, that creates a chain reaction.
// When that one finishes, it calls AutoRetryAllPending again, adding the next one.
// This seems like the right approach to avoid queue flooding.
// Just pick the oldest one.
// But wait, what if there are multiple workers? (Currently 1)
// If we pick the oldest, and it's already in queue but far back, re-adding it moves it to back?
// No, adding to channel puts it at back.
// If we want to prioritize "preceding" tasks (older tasks), we should maybe use PriorityQueue?
// But priority queue is for user actions.
// If we use normal queue, they go to back.
// If we want to ensure older tasks run first, we should perhaps re-queue them to PriorityQueue?
// Or just rely on the fact that we pick the *oldest* one.
// Let's stick to the previous logic: Pick the single oldest target video.
// This creates a chain: Task A finishes -> Pick Oldest (B) -> Add B.
// Task B finishes -> Pick Oldest (C) -> Add C.
// This effectively "retries all preceding tasks sequentially".
tm.AutoRetryFirstError(finishedTaskID)
}
// AutoRetryFirstError finds the oldest unfinished task and queues it
func (tm *TaskManager) AutoRetryFirstError(skipID string) {
// Find the video with smallest ID that is (error OR pending)
store.RLock()
var targetVideo *Video
for _, v := range store.Videos {
if v.Status == "error" || v.Status == "pending" {
// Skip if it's the current running task
if v.ID == tm.CurrentTaskID {
continue
}
// Skip if it is the task that just finished (to avoid immediate loop on failure)
if v.ID == skipID {
continue
}
if targetVideo == nil {
val := v
targetVideo = &val
} else {
// Compare IDs (smaller is older/priority)
if v.ID < targetVideo.ID {
val := v
targetVideo = &val
}
}
}
}
store.RUnlock()
if targetVideo != nil {
log.Printf("Auto-scheduling oldest task: %s [%s]", targetVideo.Title, targetVideo.Status)
// If it was error, reset to pending
if targetVideo.Status == "error" {
store.UpdateVideoStatus(targetVideo.ID, "pending")
}
// Add to normal queue
tm.Add(DownloadTask{
VideoID: targetVideo.ID,
SourceURL: targetVideo.SourceURL,
Title: targetVideo.Title,
})
}
}