From dd03f02332b8489f1342b51b2006a0fd6607a38e Mon Sep 17 00:00:00 2001 From: Nonoo Date: Mon, 14 Aug 2023 22:38:49 +0200 Subject: [PATCH] Avoid printing progress message after an error occurs --- queue.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/queue.go b/queue.go index 6cfde03..9b3b97d 100644 --- a/queue.go +++ b/queue.go @@ -139,6 +139,7 @@ func (q *DownloadQueue) processQueueEntry(ctx context.Context, qEntry *DownloadQ qEntry.editReply(ctx, processStartStr) var disableProgressPercentUpdate bool + var progressPercentUpdateMutex sync.Mutex var lastProgressPercentUpdateAt time.Time var lastProgressPercent int var progressUpdateTimer *time.Timer @@ -162,6 +163,9 @@ func (q *DownloadQueue) processQueueEntry(ctx context.Context, qEntry *DownloadQ qEntry.editReply(ctx, "🎬 Preparing download...\n"+sourceCodecInfo) }, UpdateProgressPercentFunc: func(progressPercent int) { + progressPercentUpdateMutex.Lock() + defer progressPercentUpdateMutex.Unlock() + if disableProgressPercentUpdate || lastProgressPercent == progressPercent { return } @@ -196,24 +200,35 @@ func (q *DownloadQueue) processQueueEntry(ctx context.Context, qEntry *DownloadQ r, err := downloader.DownloadAndConvertURL(qEntry.Ctx, qEntry.OrigMsg.Message) if err != nil { fmt.Println(" error downloading:", err) + progressPercentUpdateMutex.Lock() + disableProgressPercentUpdate = true + progressPercentUpdateMutex.Unlock() qEntry.editReply(ctx, fmt.Sprint(errorStr+": ", err)) return } // Feeding the returned io.ReadCloser to the uploader. fmt.Println(" processing...") + progressPercentUpdateMutex.Lock() q.updateProgress(ctx, qEntry, lastProgressPercent, sourceCodecInfo) + progressPercentUpdateMutex.Unlock() + err = uploadFile(ctx, qEntry.OrigEntities, qEntry.OrigMsgUpdate, r) if err != nil { fmt.Println(" error processing:", err) + progressPercentUpdateMutex.Lock() disableProgressPercentUpdate = true + progressPercentUpdateMutex.Unlock() r.Close() qEntry.editReply(ctx, fmt.Sprint(errorStr+": ", err)) return } + progressPercentUpdateMutex.Lock() disableProgressPercentUpdate = true + progressPercentUpdateMutex.Unlock() r.Close() + progressPercentUpdateMutex.Lock() if qEntry.Canceled { fmt.Print(" canceled\n") qEntry.editReply(ctx, canceledStr+": "+getProgressbar(lastProgressPercent, progressBarLength)+"\n"+sourceCodecInfo) @@ -221,6 +236,7 @@ func (q *DownloadQueue) processQueueEntry(ctx context.Context, qEntry *DownloadQ fmt.Print(" progress: 100%\n") qEntry.editReply(ctx, processDoneStr+": "+getProgressbar(100, progressBarLength)+"\n"+sourceCodecInfo) } + progressPercentUpdateMutex.Unlock() qEntry.sendTypingCancelAction(ctx) }