This commit is contained in:
tickstep 2022-06-05 00:00:28 +08:00
parent ab6290e7a3
commit f804a42a19
2 changed files with 34 additions and 32 deletions

View File

@ -45,12 +45,12 @@ func (f *FileActionTask) DoAction(ctx context.Context) error {
logger.Verboseln("file action task") logger.Verboseln("file action task")
logger.Verboseln(f.syncItem) logger.Verboseln(f.syncItem)
if f.syncItem.Action == SyncFileActionUpload { if f.syncItem.Action == SyncFileActionUpload {
if e := f.uploadFile(); e != nil { if e := f.uploadFile(ctx); e != nil {
return e return e
} }
} }
if f.syncItem.Action == SyncFileActionDownload { if f.syncItem.Action == SyncFileActionDownload {
if e := f.downloadFile(); e != nil { if e := f.downloadFile(ctx); e != nil {
// TODO: retry / cleanup downloading file // TODO: retry / cleanup downloading file
return e return e
} else { } else {
@ -91,7 +91,7 @@ func (f *FileActionTask) DoAction(ctx context.Context) error {
return nil return nil
} }
func (f *FileActionTask) downloadFile() error { func (f *FileActionTask) downloadFile(ctx context.Context) error {
// check local file existed or not // check local file existed or not
if b, e := utils.PathExists(f.syncItem.getLocalFileFullPath()); e == nil && b { if b, e := utils.PathExists(f.syncItem.getLocalFileFullPath()); e == nil && b {
// file existed // file existed
@ -173,36 +173,42 @@ func (f *FileActionTask) downloadFile() error {
f.syncFileDb.Update(f.syncItem) f.syncFileDb.Update(f.syncItem)
for { for {
if f.syncItem.DownloadRange.End > f.syncItem.PanFile.FileSize { select {
f.syncItem.DownloadRange.End = f.syncItem.PanFile.FileSize case <-ctx.Done():
} // cancel routine & done
worker.SetRange(f.syncItem.DownloadRange) // 分片 logger.Verboseln("file download routine done")
return nil
// 下载分片 default:
// TODO: 分片重试策略 logger.Verboseln("do file download process")
worker.Execute() if f.syncItem.DownloadRange.End > f.syncItem.PanFile.FileSize {
f.syncItem.DownloadRange.End = f.syncItem.PanFile.FileSize
if worker.GetStatus().StatusCode() == downloader.StatusCodeSuccessed {
if f.syncItem.DownloadRange.End == f.syncItem.PanFile.FileSize {
// finished
f.syncItem.Status = SyncFileStatusSuccess
f.syncItem.StatusUpdateTime = utils.NowTimeStr()
f.syncFileDb.Update(f.syncItem)
break
} }
worker.SetRange(f.syncItem.DownloadRange) // 分片
// 下一个分片 // 下载分片
f.syncItem.DownloadRange.Begin = f.syncItem.DownloadRange.End // TODO: 下载失败,分片重试策略
f.syncItem.DownloadRange.End += f.blockSize worker.Execute()
// 存储状态 if worker.GetStatus().StatusCode() == downloader.StatusCodeSuccessed {
f.syncFileDb.Update(f.syncItem) if f.syncItem.DownloadRange.End == f.syncItem.PanFile.FileSize {
// finished
f.syncItem.Status = SyncFileStatusSuccess
f.syncItem.StatusUpdateTime = utils.NowTimeStr()
f.syncFileDb.Update(f.syncItem)
return nil
}
// 下一个分片
f.syncItem.DownloadRange.Begin = f.syncItem.DownloadRange.End
f.syncItem.DownloadRange.End += f.blockSize
// 存储状态
f.syncFileDb.Update(f.syncItem)
}
} }
} }
return nil
} }
func (f *FileActionTask) uploadFile() error { func (f *FileActionTask) uploadFile(ctx context.Context) error {
return nil return nil
} }

View File

@ -470,16 +470,12 @@ func (f *FileActionTaskManager) fileActionTaskExecutor(ctx context.Context) {
defer f.wg.Done() defer f.wg.Done()
downloadWaitGroup := waitgroup.NewWaitGroup(f.fileDownloadParallel) downloadWaitGroup := waitgroup.NewWaitGroup(f.fileDownloadParallel)
downloadCtx, downloadCancel := context.WithCancel(context.Background())
for { for {
select { select {
case <-ctx.Done(): case <-ctx.Done():
// cancel routine & done // cancel routine & done
logger.Verboseln("file executor routine done") logger.Verboseln("file executor routine done")
// cancel all download process
downloadCancel()
downloadWaitGroup.Wait() downloadWaitGroup.Wait()
return return
default: default:
@ -499,7 +495,7 @@ func (f *FileActionTaskManager) fileActionTaskExecutor(ctx context.Context) {
downloadWaitGroup.AddDelta() downloadWaitGroup.AddDelta()
f.fileInProcessQueue.PushUnique(downloadItem.syncItem) f.fileInProcessQueue.PushUnique(downloadItem.syncItem)
go func() { go func() {
if e := downloadItem.DoAction(downloadCtx); e == nil { if e := downloadItem.DoAction(ctx); e == nil {
// success // success
f.fileInProcessQueue.Remove(downloadItem) f.fileInProcessQueue.Remove(downloadItem)
} else { } else {