Skip to content

Commit f6e5578

Browse files
committed
fix: add IsCreated flag to reliably track task creation state
1 parent 2dd50a7 commit f6e5578

3 files changed

Lines changed: 8 additions & 6 deletions

File tree

internal/controller/controller.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package controller
22

33
import (
4-
"github.com/GopeedLab/gopeed/pkg/base"
54
"net/http"
65
"net/url"
76
"os"
87
"path/filepath"
8+
9+
"github.com/GopeedLab/gopeed/pkg/base"
910
)
1011

1112
type Controller struct {

pkg/download/downloader.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,7 +1186,7 @@ func (d *Downloader) statusMut(task *Task, fn func() (bool, error)) (bool, error
11861186
}
11871187

11881188
func (d *Downloader) doStart(task *Task) (err error) {
1189-
var isCreate bool
1189+
var needCreate bool
11901190
isReturn, err := d.statusMut(task, func() (isReturn bool, err error) {
11911191
if task.Status == base.DownloadStatusRunning || task.Status == base.DownloadStatusDone {
11921192
isReturn = true
@@ -1198,7 +1198,7 @@ func (d *Downloader) doStart(task *Task) (err error) {
11981198
d.Logger.Error().Stack().Err(err).Msgf("restore fetcher failed, task id: %s", task.ID)
11991199
return
12001200
}
1201-
isCreate = (task.Status == base.DownloadStatusReady || task.Status == base.DownloadStatusWait) && task.Progress.Downloaded == 0
1201+
needCreate = !task.IsCreated
12021202
task.updateStatus(base.DownloadStatusRunning)
12031203

12041204
return
@@ -1224,7 +1224,7 @@ func (d *Downloader) doStart(task *Task) (err error) {
12241224
task.Meta.Res = task.fetcher.Meta().Res
12251225
}
12261226

1227-
if isCreate {
1227+
if needCreate {
12281228
if task.fetcherManager.AutoRename() {
12291229
d.checkDuplicateLock.Lock()
12301230
defer d.checkDuplicateLock.Unlock()
@@ -1248,10 +1248,9 @@ func (d *Downloader) doStart(task *Task) (err error) {
12481248
task.Meta.Opts.Name = newName
12491249
}
12501250
}
1251-
1251+
task.IsCreated = true
12521252
task.Meta.Res.CalcSize(task.Meta.Opts.SelectFiles)
12531253
}
1254-
12551254
task.Progress.Speed = 0
12561255
task.timer.Start()
12571256
if err := task.fetcher.Start(); err != nil {

pkg/download/model.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type Task struct {
2626
Status base.Status `json:"status"`
2727
Uploading bool `json:"uploading"`
2828
Progress *Progress `json:"progress"`
29+
IsCreated bool `json:"isCreated"`
2930
CreatedAt time.Time `json:"createdAt"`
3031
UpdatedAt time.Time `json:"updatedAt"`
3132

@@ -48,6 +49,7 @@ func NewTask() *Task {
4849
Status: base.DownloadStatusReady,
4950
CreatedAt: time.Now(),
5051
UpdatedAt: time.Now(),
52+
IsCreated: false,
5153
}
5254
}
5355

0 commit comments

Comments
 (0)