feat(task): allow retry canceled (#7852)

pull/7859/merge
KirCute_ECT 2025-01-27 20:18:10 +08:00 committed by GitHub
parent 23f3178f39
commit d5ec998699
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 31 additions and 7 deletions

View File

@ -53,12 +53,13 @@ type TaskConfig struct {
}
type TasksConfig struct {
Download TaskConfig `json:"download" envPrefix:"DOWNLOAD_"`
Transfer TaskConfig `json:"transfer" envPrefix:"TRANSFER_"`
Upload TaskConfig `json:"upload" envPrefix:"UPLOAD_"`
Copy TaskConfig `json:"copy" envPrefix:"COPY_"`
Decompress TaskConfig `json:"decompress" envPrefix:"DECOMPRESS_"`
DecompressUpload TaskConfig `json:"decompress_upload" envPrefix:"DECOMPRESS_UPLOAD_"`
Download TaskConfig `json:"download" envPrefix:"DOWNLOAD_"`
Transfer TaskConfig `json:"transfer" envPrefix:"TRANSFER_"`
Upload TaskConfig `json:"upload" envPrefix:"UPLOAD_"`
Copy TaskConfig `json:"copy" envPrefix:"COPY_"`
Decompress TaskConfig `json:"decompress" envPrefix:"DECOMPRESS_"`
DecompressUpload TaskConfig `json:"decompress_upload" envPrefix:"DECOMPRESS_UPLOAD_"`
AllowRetryCanceled bool `json:"allow_retry_canceled" env:"ALLOW_RETRY_CANCELED"`
}
type Cors struct {
@ -182,6 +183,7 @@ func DefaultConfig() *Config {
Workers: 5,
MaxRetry: 2,
},
AllowRetryCanceled: false,
},
Cors: Cors{
AllowOrigins: []string{"*"},

View File

@ -50,6 +50,7 @@ func (t *ArchiveDownloadTask) GetStatus() string {
}
func (t *ArchiveDownloadTask) Run() error {
t.ReinitCtx()
t.ClearEndTime()
t.SetStartTime(time.Now())
defer func() { t.SetEndTime(time.Now()) }()
@ -144,6 +145,7 @@ func (t *ArchiveContentUploadTask) GetStatus() string {
}
func (t *ArchiveContentUploadTask) Run() error {
t.ReinitCtx()
t.ClearEndTime()
t.SetStartTime(time.Now())
defer func() { t.SetEndTime(time.Now()) }()
@ -235,7 +237,9 @@ func (t *ArchiveContentUploadTask) RunWithNextTaskCallback(f func(nextTsk *Archi
func (t *ArchiveContentUploadTask) Cancel() {
t.TaskExtension.Cancel()
t.deleteSrcFile()
if !conf.Conf.Tasks.AllowRetryCanceled {
t.deleteSrcFile()
}
}
func (t *ArchiveContentUploadTask) deleteSrcFile() {

View File

@ -39,6 +39,7 @@ func (t *CopyTask) GetStatus() string {
}
func (t *CopyTask) Run() error {
t.ReinitCtx()
t.ClearEndTime()
t.SetStartTime(time.Now())
defer func() { t.SetEndTime(time.Now()) }()

View File

@ -28,6 +28,7 @@ type DownloadTask struct {
}
func (t *DownloadTask) Run() error {
t.ReinitCtx()
t.ClearEndTime()
t.SetStartTime(time.Now())
defer func() { t.SetEndTime(time.Now()) }()

View File

@ -32,6 +32,7 @@ type TransferTask struct {
}
func (t *TransferTask) Run() error {
t.ReinitCtx()
t.ClearEndTime()
t.SetStartTime(time.Now())
defer func() { t.SetEndTime(time.Now()) }()

View File

@ -2,6 +2,7 @@ package task
import (
"context"
"github.com/alist-org/alist/v3/internal/conf"
"github.com/alist-org/alist/v3/internal/model"
"github.com/xhofe/tache"
"sync"
@ -66,6 +67,20 @@ func (t *TaskExtension) Ctx() context.Context {
return t.ctx
}
func (t *TaskExtension) ReinitCtx() {
if !conf.Conf.Tasks.AllowRetryCanceled {
return
}
select {
case <-t.Base.Ctx().Done():
ctx, cancel := context.WithCancel(context.Background())
t.SetCtx(ctx)
t.SetCancelFunc(cancel)
t.ctx = nil
default:
}
}
type TaskExtensionInfo interface {
tache.TaskWithInfo
GetCreator() *model.User