mirror of
https://github.com/krau/SaveAny-Bot.git
synced 2026-08-28 03:36:45 +08:00
feat: implement task cancellation feature and update task handling
This commit is contained in:
+10
-4
@@ -1,6 +1,7 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"time"
|
||||
@@ -48,11 +49,16 @@ func processPendingTask(task *types.Task) error {
|
||||
return fmt.Errorf("context is not *ext.Context: %T", task.Ctx)
|
||||
}
|
||||
|
||||
cancelCtx, cancel := context.WithCancel(ctx)
|
||||
task.Cancel = cancel
|
||||
task.Ctx = cancelCtx
|
||||
|
||||
text, entities := buildProgressMessageEntity(task, 0, task.StartTime, 0)
|
||||
ctx.EditMessage(task.ReplyChatID, &tg.MessagesEditMessageRequest{
|
||||
Message: text,
|
||||
Entities: entities,
|
||||
ID: task.ReplyMessageID,
|
||||
Message: text,
|
||||
Entities: entities,
|
||||
ID: task.ReplyMessageID,
|
||||
ReplyMarkup: getCancelTaskMarkup(task),
|
||||
})
|
||||
progressCallback := buildProgressCallback(ctx, task, getProgressUpdateCount(task.File.FileSize))
|
||||
|
||||
@@ -63,7 +69,7 @@ func processPendingTask(task *types.Task) error {
|
||||
defer dest.Close()
|
||||
task.StartTime = time.Now()
|
||||
downloadBuider := Downloader.Download(bot.Client.API(), task.File.Location).WithThreads(getTaskThreads(task.File.FileSize))
|
||||
_, err = downloadBuider.Parallel(ctx, dest)
|
||||
_, err = downloadBuider.Parallel(cancelCtx, dest)
|
||||
if err != nil {
|
||||
return fmt.Errorf("下载文件失败: %w", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user