Ai
1 Star 2 Fork 1

孤爺仔/BaiduPCS-Go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
bg_fg.go 1.57 KB
一键复制 编辑 原始数据 按行查看 历史
package pcscommand
import (
"fmt"
"github.com/iikira/BaiduPCS-Go/pcstable"
"os"
"strconv"
"strings"
"sync"
"sync/atomic"
)
var (
// BgMap 后台
BgMap = BgTasks{
tasks: sync.Map{},
sig: make(chan int64),
}
)
type (
// BgTasks 后台任务
BgTasks struct {
lastID int64
tasks sync.Map
started bool
sig chan int64
}
// BgDTaskItem 后台任务详情
BgDTaskItem struct {
id int
downloadOptions *DownloadOptions
pcspaths []string
}
)
// NewID 返回生成的 ID
func (b *BgTasks) NewID() int64 {
id := atomic.AddInt64(&b.lastID, 1)
return id
}
// TaskID 返回后台任务 id
func (t *BgDTaskItem) TaskID() int {
return t.id
}
// PrintAllBgTask 输出所有的后台任务
func (b *BgTasks) PrintAllBgTask() {
tb := pcstable.NewTable(os.Stdout)
tb.SetHeader([]string{"task_id", "files"})
b.tasks.Range(func(id, v interface{}) bool {
tb.Append([]string{strconv.FormatInt(id.(int64), 10), strings.Join(v.(*BgDTaskItem).pcspaths, ",")})
return true
})
tb.Render()
}
// RunBgDownload 执行后台下载
func RunBgDownload(paths []string, options *DownloadOptions) {
if !BgMap.started {
go func() {
for {
select {
case id := <-BgMap.sig:
BgMap.tasks.Delete(id)
fmt.Printf("任务:%d 已完成\n", id)
}
}
}()
} else {
BgMap.started = true
}
if options.Out == nil {
options.Out, _ = os.Open(os.DevNull)
}
task := new(BgDTaskItem)
task.pcspaths = paths
id := BgMap.NewID()
BgMap.tasks.Store(id, task)
go func(taskID int64) {
RunDownload(paths, options)
BgMap.sig <- taskID
}(id)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/guyezi/BaiduPCS-Go.git
git@gitee.com:guyezi/BaiduPCS-Go.git
guyezi
BaiduPCS-Go
BaiduPCS-Go
v3.5.6

搜索帮助