-
Notifications
You must be signed in to change notification settings - Fork 11
/
tasks.go
356 lines (340 loc) · 9.15 KB
/
tasks.go
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package main
import (
"encoding/json"
"fmt"
"github.com/gorilla/websocket"
"github.com/hanjm/log"
"io/ioutil"
"net/http"
"os"
"strings"
"time"
//"syscall"
"net/url"
"runtime"
)
type TasksManager struct {
tasks []Task
ConnectionsManger *ConnectionsManger
downloadDir string
limitByteSize int64
limitTimeout time.Duration
PushTasksUpdateChan chan struct{}
}
func NewTasksManager(downloadDir string, limitByteSize int64, limitTimeout time.Duration) *TasksManager {
return &TasksManager{
tasks: make([]Task, 0, 64),
ConnectionsManger: NewConnectionsManger(),
PushTasksUpdateChan: make(chan struct{}, 2),
downloadDir: downloadDir,
limitByteSize: limitByteSize,
limitTimeout: limitTimeout,
}
}
func (m *TasksManager) WebSocketHandler(w http.ResponseWriter, r *http.Request) {
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool {
return true
},
}
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Errorf("websocket upgrader.Upgrade error:%s", err)
return
}
log.Infof("new connection from %s, number of active connections %d", conn.RemoteAddr(), len(m.ConnectionsManger.conns)+1)
m.ConnectionsManger.Add(conn)
// 首次连接,推送文件信息
m.PushTasksUpdate()
}
func (m *TasksManager) TaskHandler(w http.ResponseWriter, r *http.Request) {
// filename有特殊字符如&时无法正常通过r.URL.Query()获取
filename := strings.TrimPrefix(r.URL.RawQuery, "filename=")
filename, err := url.QueryUnescape(filename)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("param filename is invalid:" + r.URL.RawQuery))
return
}
filename = strings.Replace(filename, "/", "", -1)
switch r.Method {
case http.MethodGet:
log.Infof("[TaskHandler]download %s", filename)
// 下载文件
if filename == "" {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("param filename is empty"))
return
}
http.Redirect(w, r, m.downloadDir+filename, http.StatusTemporaryRedirect)
case http.MethodPost:
// 新建任务
sourceURL := strings.TrimSpace(r.PostFormValue("url"))
log.Infof("[TaskHandler]create task:%s", sourceURL)
if sourceURL == "" {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("param url is empty"))
return
}
// check total size
filesSize := m.ListFiles()
if filesSize > m.limitByteSize {
w.WriteHeader(http.StatusServiceUnavailable)
w.Write([]byte(fmt.Sprintf("There are too many files in server, please delete some files, FilesSize:%s", getHumanSizeString(filesSize))))
return
}
task, err := NewDownloadTask(sourceURL)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
m.AddTask(task)
go func(m *TasksManager) {
defer func() {
if rec := recover(); rec != nil {
log.Errorf("download worker panic:%s", rec)
}
}()
err := task.Download(m.downloadDir, m.limitByteSize, m.limitTimeout)
if err != nil {
log.Errorf("task download error:%s, task name:%s", err, task.FileName())
}
m.PushTasksUpdate()
}(m)
// 添加任务后,推送文件信息
w.WriteHeader(http.StatusCreated)
w.Write([]byte("CREATE OK"))
m.PushTasksUpdate()
return
case http.MethodDelete:
log.Infof("[TaskHandler]delete %s", filename)
// 删除文件
defer func() {
m.PushTasksUpdate()
}()
if filename == "" {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("param filename is empty"))
return
}
// 正在下载不能删
if task := m.GetTask(filename); task != nil && !task.IsCompleted() {
w.WriteHeader(http.StatusBadRequest)
log.Infof("[TaskHandler]delete fail,task is downloading %s", filename)
w.Write([]byte("task is downloading"))
return
}
err := m.RemoveTask(filename)
if err != nil {
log.Errorf("delete '%s' error:%s", filename, err)
w.WriteHeader(http.StatusNotFound)
w.Write([]byte(fmt.Sprintf("delete error:%s", err)))
return
}
w.WriteHeader(http.StatusOK)
w.Write([]byte("DELETE OK"))
log.Infof("[TaskHandler]delete ok, %s", filename)
m.PushTasksUpdate()
default:
w.WriteHeader(http.StatusBadRequest)
}
}
func (m *TasksManager) PushTasksUpdate() {
select {
case m.PushTasksUpdateChan <- struct{}{}:
//log.Debugf("PushTasksUpdate")
default:
pcs := make([]uintptr, 5)
runtime.Callers(1, pcs)
frames := runtime.CallersFrames(pcs)
var callers []string
for {
frame, more := frames.Next()
if !more {
break
}
callers = append(callers, fmt.Sprintf("[%s:%d:%s]", frame.File, frame.Line, frame.Function))
}
log.Warnf("%vPushTasksUpdate timeOut", callers)
}
}
// HasDownloadingTask 检查是否有正在下载的任务
func (m *TasksManager) HasDownloadingTask() bool {
for _, v := range m.tasks {
if !v.IsCompleted() {
return true
}
}
return false
}
func (m *TasksManager) GetTasks() []Task {
return m.tasks
}
func (m *TasksManager) GetTask(filename string) Task {
for _, v := range m.tasks {
if v.FileName() == filename {
return v
}
}
return nil
}
func (m *TasksManager) AddTask(t Task) {
m.tasks = append(m.tasks, t)
}
func (m *TasksManager) RemoveTask(filename string) error {
temp := make([]Task, 0, len(m.tasks))
for _, v := range m.tasks {
if v.FileName() != filename {
temp = append(temp, v)
}
}
m.tasks = temp
err := os.RemoveAll(m.downloadDir + "/" + filename)
if err != nil {
if os.IsNotExist(err) {
return nil
}
return err
}
return nil
}
// backup and restore
const backupFilename = "tasks.json"
func (m *TasksManager) BackupToJSON() error {
data, err := json.Marshal(m.tasks)
if err != nil {
return fmt.Errorf("json.Marshal error:%s", err)
}
os.Remove(backupFilename)
fp, err := os.Create(backupFilename)
if err != nil {
return fmt.Errorf("os.Create error:%s", err)
}
defer fp.Close()
defer fp.Sync()
_, err = fp.Write(data)
if err != nil {
return fmt.Errorf("fp.Write error:%s", err)
}
return nil
}
func (m *TasksManager) RestoreFromJSON() error {
fileData, err := ioutil.ReadFile(backupFilename)
if err != nil {
if os.IsNotExist(err) {
return nil
}
return fmt.Errorf("ReadFile error:%s", err)
}
var httpTasks []*HTTPTask
err = json.Unmarshal(fileData, &httpTasks)
if err != nil {
return fmt.Errorf("json.Unmarshal error:%s", err)
}
for _, ht := range httpTasks {
// 删除不存在的
if _, err := os.Stat(fmt.Sprintf("%s/%s", m.downloadDir, ht.TaskInfo.FileName)); err != nil && os.IsNotExist(err) {
continue
}
switch ht.TaskType {
case DownloadTaskTypeHTTP:
m.AddTask(ht)
case DownloadTaskTypeMagnet:
mt := &MagnetTask{
TaskType: DownloadTaskTypeMagnet,
TaskInfo: ht.TaskInfo,
}
m.AddTask(mt)
}
}
return nil
}
// 如果有未完成的, 继续下载
func (m *TasksManager) ReDownloadUncompleted() {
for _, task := range m.tasks {
if !task.IsCompleted() {
log.Infof("ReDownloadUncompleted task:%s", task.FileName())
go func(m *TasksManager) {
defer func() {
if rec := recover(); rec != nil {
log.Errorf("download worker panic:%s", rec)
}
}()
os.Remove(m.downloadDir + "/" + task.FileName())
err := task.Download(m.downloadDir, m.limitByteSize, m.limitTimeout)
if err != nil {
log.Errorf("task download error:%s, task name:%s", err, task.FileName())
}
m.PushTasksUpdate()
}(m)
}
}
}
func (m *TasksManager) ListFiles() (fileTotalSize int64) {
files, _ := ioutil.ReadDir(m.downloadDir)
for _, file := range files {
filename := file.Name()
if strings.HasSuffix(filename, ".torrent") ||
strings.HasSuffix(filename, ".aria2") {
continue
}
task := m.GetTask(filename)
if task == nil {
//rebuild new local file
fileSize := file.Size()
newLocalTask := NewHTTPTask("Local")
newLocalTask.TaskInfo.FileName = filename
newLocalTask.TaskInfo.Size = fileSize
newLocalTask.TaskInfo.ContentLength = fileSize
// todo: syscall.Stat_t is different between linux and macos
//if fs, ok := file.Sys().(syscall.Stat_t); ok {
// newLocalTask.TaskInfo.StartTime = time.Unix(fs.Ctimespec.Sec, fs.Ctimespec.Nsec)
// if delta := fs.Mtimespec.Sec - fs.Ctimespec.Sec; delta > 0 {
// newLocalTask.TaskInfo.Duration = delta
// newLocalTask.Speed = fileSize / delta
// }
//} else {
// newLocalTask.TaskInfo.StartTime = file.ModTime()
//}
newLocalTask.TaskInfo.StartTime = file.ModTime()
newLocalTask.TaskInfo.IsCompleted = true
m.AddTask(newLocalTask)
fileTotalSize += file.Size()
}
}
return fileTotalSize
}
func (m *TasksManager) PushTasksUpdateWorker() {
defer func() {
if rec := recover(); rec != nil {
log.Errorf("pushTasksUpdateWorker panic:%s", rec)
}
}()
go func() {
defer func() {
if rec := recover(); rec != nil {
log.Errorf("pushTasksUpdateWorker panic:%s", rec)
}
}()
for {
select {
case <-m.PushTasksUpdateChan:
log.Debugf("m.PushTasksUpdateChan received")
m.ConnectionsManger.Broadcast(m.GetTasks())
}
}
}()
for {
ticker := time.NewTicker(time.Second)
select {
case <-ticker.C:
if m.HasDownloadingTask() && m.ConnectionsManger.Count() > 0 {
// 当有文件在下载且有连接时, 推送下载任务更新信息
m.PushTasksUpdate()
}
}
}
}