代码拉取完成,页面将自动刷新
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)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。