Avoid printing progress message after an error occurs

This commit is contained in:
Nonoo 2023-08-14 22:38:49 +02:00
parent 82945d1a0c
commit dd03f02332

View File

@ -139,6 +139,7 @@ func (q *DownloadQueue) processQueueEntry(ctx context.Context, qEntry *DownloadQ
qEntry.editReply(ctx, processStartStr) qEntry.editReply(ctx, processStartStr)
var disableProgressPercentUpdate bool var disableProgressPercentUpdate bool
var progressPercentUpdateMutex sync.Mutex
var lastProgressPercentUpdateAt time.Time var lastProgressPercentUpdateAt time.Time
var lastProgressPercent int var lastProgressPercent int
var progressUpdateTimer *time.Timer var progressUpdateTimer *time.Timer
@ -162,6 +163,9 @@ func (q *DownloadQueue) processQueueEntry(ctx context.Context, qEntry *DownloadQ
qEntry.editReply(ctx, "🎬 Preparing download...\n"+sourceCodecInfo) qEntry.editReply(ctx, "🎬 Preparing download...\n"+sourceCodecInfo)
}, },
UpdateProgressPercentFunc: func(progressPercent int) { UpdateProgressPercentFunc: func(progressPercent int) {
progressPercentUpdateMutex.Lock()
defer progressPercentUpdateMutex.Unlock()
if disableProgressPercentUpdate || lastProgressPercent == progressPercent { if disableProgressPercentUpdate || lastProgressPercent == progressPercent {
return return
} }
@ -196,24 +200,35 @@ func (q *DownloadQueue) processQueueEntry(ctx context.Context, qEntry *DownloadQ
r, err := downloader.DownloadAndConvertURL(qEntry.Ctx, qEntry.OrigMsg.Message) r, err := downloader.DownloadAndConvertURL(qEntry.Ctx, qEntry.OrigMsg.Message)
if err != nil { if err != nil {
fmt.Println(" error downloading:", err) fmt.Println(" error downloading:", err)
progressPercentUpdateMutex.Lock()
disableProgressPercentUpdate = true
progressPercentUpdateMutex.Unlock()
qEntry.editReply(ctx, fmt.Sprint(errorStr+": ", err)) qEntry.editReply(ctx, fmt.Sprint(errorStr+": ", err))
return return
} }
// Feeding the returned io.ReadCloser to the uploader. // Feeding the returned io.ReadCloser to the uploader.
fmt.Println(" processing...") fmt.Println(" processing...")
progressPercentUpdateMutex.Lock()
q.updateProgress(ctx, qEntry, lastProgressPercent, sourceCodecInfo) q.updateProgress(ctx, qEntry, lastProgressPercent, sourceCodecInfo)
progressPercentUpdateMutex.Unlock()
err = uploadFile(ctx, qEntry.OrigEntities, qEntry.OrigMsgUpdate, r) err = uploadFile(ctx, qEntry.OrigEntities, qEntry.OrigMsgUpdate, r)
if err != nil { if err != nil {
fmt.Println(" error processing:", err) fmt.Println(" error processing:", err)
progressPercentUpdateMutex.Lock()
disableProgressPercentUpdate = true disableProgressPercentUpdate = true
progressPercentUpdateMutex.Unlock()
r.Close() r.Close()
qEntry.editReply(ctx, fmt.Sprint(errorStr+": ", err)) qEntry.editReply(ctx, fmt.Sprint(errorStr+": ", err))
return return
} }
progressPercentUpdateMutex.Lock()
disableProgressPercentUpdate = true disableProgressPercentUpdate = true
progressPercentUpdateMutex.Unlock()
r.Close() r.Close()
progressPercentUpdateMutex.Lock()
if qEntry.Canceled { if qEntry.Canceled {
fmt.Print(" canceled\n") fmt.Print(" canceled\n")
qEntry.editReply(ctx, canceledStr+": "+getProgressbar(lastProgressPercent, progressBarLength)+"\n"+sourceCodecInfo) 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") fmt.Print(" progress: 100%\n")
qEntry.editReply(ctx, processDoneStr+": "+getProgressbar(100, progressBarLength)+"\n"+sourceCodecInfo) qEntry.editReply(ctx, processDoneStr+": "+getProgressbar(100, progressBarLength)+"\n"+sourceCodecInfo)
} }
progressPercentUpdateMutex.Unlock()
qEntry.sendTypingCancelAction(ctx) qEntry.sendTypingCancelAction(ctx)
} }