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

@ -59,6 +59,7 @@ type TasksConfig struct {
Copy TaskConfig `json:"copy" envPrefix:"COPY_"` Copy TaskConfig `json:"copy" envPrefix:"COPY_"`
Decompress TaskConfig `json:"decompress" envPrefix:"DECOMPRESS_"` Decompress TaskConfig `json:"decompress" envPrefix:"DECOMPRESS_"`
DecompressUpload TaskConfig `json:"decompress_upload" envPrefix:"DECOMPRESS_UPLOAD_"` DecompressUpload TaskConfig `json:"decompress_upload" envPrefix:"DECOMPRESS_UPLOAD_"`
AllowRetryCanceled bool `json:"allow_retry_canceled" env:"ALLOW_RETRY_CANCELED"`
} }
type Cors struct { type Cors struct {
@ -182,6 +183,7 @@ func DefaultConfig() *Config {
Workers: 5, Workers: 5,
MaxRetry: 2, MaxRetry: 2,
}, },
AllowRetryCanceled: false,
}, },
Cors: Cors{ Cors: Cors{
AllowOrigins: []string{"*"}, AllowOrigins: []string{"*"},

View File

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

View File

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

View File

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

View File

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

View File

@ -2,6 +2,7 @@ package task
import ( import (
"context" "context"
"github.com/alist-org/alist/v3/internal/conf"
"github.com/alist-org/alist/v3/internal/model" "github.com/alist-org/alist/v3/internal/model"
"github.com/xhofe/tache" "github.com/xhofe/tache"
"sync" "sync"
@ -66,6 +67,20 @@ func (t *TaskExtension) Ctx() context.Context {
return t.ctx 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 { type TaskExtensionInfo interface {
tache.TaskWithInfo tache.TaskWithInfo
GetCreator() *model.User GetCreator() *model.User