mirror of
https://github.com/krau/SaveAny-Bot.git
synced 2026-09-05 07:36:36 +08:00
fix: handle context cancellation during aria2 download and improve cleanup logic
This commit is contained in:
@@ -25,6 +25,10 @@ func (t *Task) Execute(ctx context.Context) error {
|
|||||||
|
|
||||||
// Wait for aria2 download to complete
|
// Wait for aria2 download to complete
|
||||||
if err := t.waitForDownload(ctx); err != nil {
|
if err := t.waitForDownload(ctx); err != nil {
|
||||||
|
// If context was canceled, also cancel the aria2 download
|
||||||
|
if errors.Is(err, context.Canceled) {
|
||||||
|
t.cancelAria2Download()
|
||||||
|
}
|
||||||
logger.Errorf("Aria2 download failed: %v", err)
|
logger.Errorf("Aria2 download failed: %v", err)
|
||||||
if t.Progress != nil {
|
if t.Progress != nil {
|
||||||
t.Progress.OnDone(ctx, t, err)
|
t.Progress.OnDone(ctx, t, err)
|
||||||
@@ -47,7 +51,7 @@ func (t *Task) Execute(ctx context.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Clean up aria2 download result
|
// Clean up aria2 download result
|
||||||
if _, err := t.Aria2Client.RemoveDownloadResult(ctx, t.GID); err != nil {
|
if _, err := t.Aria2Client.RemoveDownloadResult(context.Background(), t.GID); err != nil {
|
||||||
logger.Warnf("Failed to remove aria2 download result: %v", err)
|
logger.Warnf("Failed to remove aria2 download result: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -222,3 +226,25 @@ func (t *Task) removeFileIfNeeded(filePath string) {
|
|||||||
logger.Debugf("Removed local file %s", filePath)
|
logger.Debugf("Removed local file %s", filePath)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// cancelAria2Download cancels the aria2 download task
|
||||||
|
func (t *Task) cancelAria2Download() {
|
||||||
|
logger := log.FromContext(t.ctx)
|
||||||
|
logger.Infof("Canceling aria2 download GID: %s", t.GID)
|
||||||
|
|
||||||
|
// Use a background context with timeout for cleanup
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
// Try to force remove the download
|
||||||
|
if _, err := t.Aria2Client.ForceRemove(ctx, t.GID); err != nil {
|
||||||
|
logger.Warnf("Failed to cancel aria2 download %s: %v", t.GID, err)
|
||||||
|
} else {
|
||||||
|
logger.Infof("Successfully canceled aria2 download %s", t.GID)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Also remove the download result to clean up
|
||||||
|
if _, err := t.Aria2Client.RemoveDownloadResult(ctx, t.GID); err != nil {
|
||||||
|
logger.Debugf("Failed to remove download result for %s: %v", t.GID, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user