mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-08 17:40:28 +00:00
fix: sort of completed tasks (#297)
This commit is contained in:
7 files changed
+25
-19
No files matched your search
@@ -572,7 +572,8 @@ func (d *Downloader) getProtocolConfig(name string, v any) bool {
|
||||
func (d *Downloader) watch(task *Task) {
|
||||
err := task.fetcher.Wait()
|
||||
if err != nil {
|
||||
task.Status = base.DownloadStatusError
|
||||
task.updateStatus(base.DownloadStatusError)
|
||||
d.storage.Put(bucketTask, task.ID, task.clone())
|
||||
d.emit(EventKeyError, task, err)
|
||||
} else {
|
||||
task.Progress.Used = task.timer.Used()
|
||||
@@ -586,7 +587,7 @@ func (d *Downloader) watch(task *Task) {
|
||||
totalSize := task.Meta.Res.Size
|
||||
task.Progress.Speed = totalSize / used
|
||||
task.Progress.Downloaded = totalSize
|
||||
task.Status = base.DownloadStatusDone
|
||||
task.updateStatus(base.DownloadStatusDone)
|
||||
d.storage.Put(bucketTask, task.ID, task.clone())
|
||||
d.emit(EventKeyDone, task)
|
||||
d.emit(EventKeyFinally, task, err)
|
||||
@@ -658,7 +659,6 @@ func (d *Downloader) doCreate(fetcher fetcher.Fetcher, opts *base.Options) (task
|
||||
task.fetcher = fetcher
|
||||
task.Meta = fetcher.Meta()
|
||||
task.Progress = &Progress{}
|
||||
task.Status = base.DownloadStatusReady
|
||||
initTask(task)
|
||||
if err = fetcher.Create(opts); err != nil {
|
||||
return
|
||||
@@ -699,7 +699,7 @@ func (d *Downloader) doStart(task *Task) (err error) {
|
||||
|
||||
cloneTask := task.clone()
|
||||
isCreate := task.Status == base.DownloadStatusReady
|
||||
task.Status = base.DownloadStatusRunning
|
||||
task.updateStatus(base.DownloadStatusRunning)
|
||||
|
||||
doStart := func() error {
|
||||
task.lock.Lock()
|
||||
@@ -714,7 +714,8 @@ func (d *Downloader) doStart(task *Task) (err error) {
|
||||
if task.Meta.Res == nil {
|
||||
err := task.fetcher.Resolve(task.Meta.Req)
|
||||
if err != nil {
|
||||
task.Status = base.DownloadStatusError
|
||||
task.updateStatus(base.DownloadStatusError)
|
||||
d.storage.Put(bucketTask, task.ID, task.clone())
|
||||
d.emit(EventKeyError, task, err)
|
||||
return err
|
||||
}
|
||||
@@ -768,7 +769,7 @@ func (d *Downloader) doStart(task *Task) (err error) {
|
||||
func (d *Downloader) doPause(task *Task) (err error) {
|
||||
err = func() error {
|
||||
if task.Status != base.DownloadStatusDone {
|
||||
task.Status = base.DownloadStatusPause
|
||||
task.updateStatus(base.DownloadStatusPause)
|
||||
task.timer.Pause()
|
||||
if task.fetcher != nil {
|
||||
if err := task.fetcher.Pause(); err != nil {
|
||||
|
||||
@@ -24,6 +24,7 @@ type Task struct {
|
||||
Status base.Status `json:"status"`
|
||||
Progress *Progress `json:"progress"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
UpdatedAt time.Time `json:"updatedAt"`
|
||||
|
||||
fetcherBuilder fetcher.FetcherBuilder
|
||||
fetcher fetcher.Fetcher
|
||||
@@ -41,9 +42,15 @@ func NewTask() *Task {
|
||||
ID: id,
|
||||
Status: base.DownloadStatusReady,
|
||||
CreatedAt: time.Now(),
|
||||
UpdatedAt: time.Now(),
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Task) updateStatus(status base.Status) {
|
||||
t.UpdatedAt = time.Now()
|
||||
t.Status = status
|
||||
}
|
||||
|
||||
func (t *Task) clone() *Task {
|
||||
return &Task{
|
||||
ID: t.ID,
|
||||
@@ -51,6 +58,7 @@ func (t *Task) clone() *Task {
|
||||
Status: t.Status,
|
||||
Progress: t.Progress,
|
||||
CreatedAt: t.CreatedAt,
|
||||
UpdatedAt: t.UpdatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user