mirror of
https://github.com/OpenListTeam/OpenList.git
synced 2025-09-19 20:26:26 +08:00

* feat(thunderx,pikpak): add offline download support for ThunderX; add ctx to specific PikPak functions * Update internal/offline_download/tool/download.go Co-authored-by: MadDogOwner <xiaoran@xrgzs.top> Signed-off-by: 花月喵梦 <152958106+nekohy@users.noreply.github.com> --------- Signed-off-by: 花月喵梦 <152958106+nekohy@users.noreply.github.com> Co-authored-by: MadDogOwner <xiaoran@xrgzs.top>
43 lines
1.2 KiB
Go
43 lines
1.2 KiB
Go
package thunderx
|
|
|
|
import (
|
|
"context"
|
|
"github.com/OpenListTeam/OpenList/v4/drivers/thunderx"
|
|
"github.com/OpenListTeam/OpenList/v4/internal/op"
|
|
"github.com/OpenListTeam/OpenList/v4/pkg/singleflight"
|
|
"github.com/OpenListTeam/go-cache"
|
|
"time"
|
|
)
|
|
|
|
var taskCache = cache.NewMemCache(cache.WithShards[[]thunderx.OfflineTask](16))
|
|
var taskG singleflight.Group[[]thunderx.OfflineTask]
|
|
|
|
func (t *ThunderX) GetTasks(thunderxDriver *thunderx.ThunderX) ([]thunderx.OfflineTask, error) {
|
|
key := op.Key(thunderxDriver, "/drive/v1/task")
|
|
if !t.refreshTaskCache {
|
|
if tasks, ok := taskCache.Get(key); ok {
|
|
return tasks, nil
|
|
}
|
|
}
|
|
t.refreshTaskCache = false
|
|
tasks, err, _ := taskG.Do(key, func() ([]thunderx.OfflineTask, error) {
|
|
ctx := context.Background()
|
|
phase := []string{"PHASE_TYPE_RUNNING", "PHASE_TYPE_ERROR", "PHASE_TYPE_PENDING", "PHASE_TYPE_COMPLETE"}
|
|
tasks, err := thunderxDriver.OfflineList(ctx, "", phase)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
// 添加缓存 10s
|
|
if len(tasks) > 0 {
|
|
taskCache.Set(key, tasks, cache.WithEx[[]thunderx.OfflineTask](time.Second*10))
|
|
} else {
|
|
taskCache.Del(key)
|
|
}
|
|
return tasks, nil
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return tasks, nil
|
|
}
|