Mark completely failed uploads as failed so we don't keep retrying them

This commit is contained in:
Justin Hawkins 2021-10-11 20:55:35 +10:30
parent 2a3f4ea21a
commit 7670a0c5b7

View File

@ -5,6 +5,7 @@ package upload
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"image" "image"
"io" "io"
@ -15,7 +16,6 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"time" "time"
"errors"
"github.com/fogleman/gg" "github.com/fogleman/gg"
"github.com/tardisx/discord-auto-upload/config" "github.com/tardisx/discord-auto-upload/config"
@ -35,6 +35,8 @@ type Upload struct {
Uploaded bool `json:"uploaded"` // has this file been uploaded to discord Uploaded bool `json:"uploaded"` // has this file been uploaded to discord
UploadedAt time.Time `json:"uploaded_at"` UploadedAt time.Time `json:"uploaded_at"`
Failed bool `json:"failed"`
originalFilename string // path on the local disk originalFilename string // path on the local disk
filenameToUpload string // post-watermark, or just original if unwatermarked filenameToUpload string // post-watermark, or just original if unwatermarked
@ -74,20 +76,22 @@ func (u *Uploader) AddFile(file string, conf config.Watcher) {
func (u *Uploader) Upload() { func (u *Uploader) Upload() {
for _, upload := range u.Uploads { for _, upload := range u.Uploads {
if !upload.Uploaded { if !upload.Uploaded && !upload.Failed {
upload.processUpload() upload.processUpload()
} }
} }
} }
func (u *Upload) processUpload() error { func (u *Upload) processUpload() error {
daulog.SendLog(fmt.Sprintf("Uploading: %s", u.originalFilename), daulog.LogTypeInfo)
if u.webhookURL == "" { if u.webhookURL == "" {
daulog.SendLog("WebHookURL is not configured - cannot upload!", daulog.LogTypeError) daulog.SendLog("WebHookURL is not configured - cannot upload!", daulog.LogTypeError)
return errors.New("webhook url not configured") return errors.New("webhook url not configured")
} }
if u.watermark { if u.watermark {
daulog.SendLog("Watermarking", daulog.LogTypeInfo) daulog.SendLog("Watermarking image", daulog.LogTypeInfo)
u.applyWatermark() u.applyWatermark()
} else { } else {
u.filenameToUpload = u.originalFilename u.filenameToUpload = u.originalFilename
@ -125,9 +129,9 @@ func (u *Upload) processUpload() error {
start := time.Now() start := time.Now()
if u.Client == nil { if u.Client == nil {
// if no client was specified (a unit test) then create // if no client was specified (a unit test) then create
// a default one // a default one
u.Client = &http.Client{Timeout: time.Second * 30} u.Client = &http.Client{Timeout: time.Second * 30}
} }
resp, err := u.Client.Do(request) resp, err := u.Client.Do(request)
@ -203,6 +207,8 @@ func (u *Upload) processUpload() error {
if retriesRemaining == 0 { if retriesRemaining == 0 {
daulog.SendLog("Failed to upload, even after all retries", daulog.LogTypeError) daulog.SendLog("Failed to upload, even after all retries", daulog.LogTypeError)
u.Failed = true
return errors.New("could not upload after all retries")
} }
return nil return nil
} }