feat: enhance error handling in MiogestHandler and add Check Annunci script

This commit is contained in:
Marco Pedone 2026-01-13 11:08:38 +01:00
parent cb07f1dca8
commit 19b4693512
4 changed files with 65 additions and 35 deletions

View file

@ -38,14 +38,17 @@ func NewMiogestHandler() *MiogestHandler {
} }
// ANNUNCI FROM MIOGEST // ANNUNCI FROM MIOGEST
func (m *MiogestHandler) GetAnnunci() typesdefs.AnnunciXML { func (m *MiogestHandler) GetAnnunci() (typesdefs.AnnunciXML, error) {
if len(m.AnnunciXML.Annuncio) == 0 || time.Now().After(m.annunci_cache_expires) { if len(m.AnnunciXML.Annuncio) == 0 || time.Now().After(m.annunci_cache_expires) {
m.GetAnnunciFromMiogest() err := m.GetAnnunciFromMiogest()
if err != nil {
return typesdefs.AnnunciXML{}, err
} }
return m.AnnunciXML }
return m.AnnunciXML, nil
} }
func (m *MiogestHandler) GetAnnunciFromMiogest() { func (m *MiogestHandler) GetAnnunciFromMiogest() error {
maxRetries := 3 maxRetries := 3
baseDelay := 10 * time.Second baseDelay := 10 * time.Second
@ -57,7 +60,7 @@ func (m *MiogestHandler) GetAnnunciFromMiogest() {
// Success - validate we got data // Success - validate we got data
if len(m.AnnunciXML.Annuncio) > 0 { if len(m.AnnunciXML.Annuncio) > 0 {
m.annunci_cache_expires = time.Now().Add(1 * time.Hour) m.annunci_cache_expires = time.Now().Add(1 * time.Hour)
return return nil
} }
log.Printf("Warning: Successfully fetched XML but got 0 annunci") log.Printf("Warning: Successfully fetched XML but got 0 annunci")
} }
@ -74,9 +77,12 @@ func (m *MiogestHandler) GetAnnunciFromMiogest() {
attempt+1, maxRetries, err, delay) attempt+1, maxRetries, err, delay)
time.Sleep(delay) time.Sleep(delay)
} else { } else {
log.Printf("Failed to get annunci after %d attempts: %v", maxRetries, err) log.Printf("Failed to get annunci after %d attempts: %v", maxRetries, err)
return fmt.Errorf("failed to get annunci after %d attempts: %w", maxRetries, err)
} }
} }
return nil
} }
// CATEGORIE AND CARATTERISTICHE FROM MIOGEST // CATEGORIE AND CARATTERISTICHE FROM MIOGEST
@ -132,27 +138,36 @@ func (m *MiogestHandler) GenerateCategorie() {
} }
// ANNUNCI PARSED // ANNUNCI PARSED
func (m *MiogestHandler) GetAnnunciParsed(imgPool *[]typesdefs.MediaToProcess, videoPool *[]typesdefs.MediaToProcess, forceMediaStale bool) typesdefs.AnnunciParsed { func (m *MiogestHandler) GetAnnunciParsed(imgPool *[]typesdefs.MediaToProcess, videoPool *[]typesdefs.MediaToProcess, forceMediaStale bool) (typesdefs.AnnunciParsed, error) {
// salvo il parsing nello struct nel caso voglio implementare un caching e non fare il parsing ad ogni chiamata // salvo il parsing nello struct nel caso voglio implementare un caching e non fare il parsing ad ogni chiamata
m.AnnunciParsed = m.ParseAnnunci("", imgPool, videoPool, forceMediaStale) var err error
return m.AnnunciParsed m.AnnunciParsed, err = m.ParseAnnunci("", imgPool, videoPool, forceMediaStale)
return m.AnnunciParsed, err
} }
// versione per singolo annuncio // versione per singolo annuncio
func (m *MiogestHandler) GetAnnuncioParsed(codFilter string, imgPool *[]typesdefs.MediaToProcess, videoPool *[]typesdefs.MediaToProcess) typesdefs.AnnunciParsed { func (m *MiogestHandler) GetAnnuncioParsed(codFilter string, imgPool *[]typesdefs.MediaToProcess, videoPool *[]typesdefs.MediaToProcess) (typesdefs.AnnunciParsed, error) {
data := m.ParseAnnunci(codFilter, imgPool, videoPool, false)
if len(data.Annuncio) == 0 { data, err := m.ParseAnnunci(codFilter, imgPool, videoPool, false)
return typesdefs.AnnunciParsed{} if err != nil {
return typesdefs.AnnunciParsed{}, err
} }
return data if len(data.Annuncio) == 0 {
return typesdefs.AnnunciParsed{}, fmt.Errorf("No Annuncio found for codice %s", codFilter)
}
return data, nil
} }
func (m *MiogestHandler) ParseAnnunci(codFilter string, imgPool *[]typesdefs.MediaToProcess, videoPool *[]typesdefs.MediaToProcess, forceMediaStale bool) typesdefs.AnnunciParsed { func (m *MiogestHandler) ParseAnnunci(codFilter string, imgPool *[]typesdefs.MediaToProcess, videoPool *[]typesdefs.MediaToProcess, forceMediaStale bool) (typesdefs.AnnunciParsed, error) {
annunci := m.GetAnnunci() annunci, err := m.GetAnnunci()
if err != nil {
log.Fatalf("Failed to get annunci for parsing: %v", err.Error())
return typesdefs.AnnunciParsed{}, err
}
if len(annunci.Annuncio) == 0 { if len(annunci.Annuncio) == 0 {
return typesdefs.AnnunciParsed{} return typesdefs.AnnunciParsed{}, fmt.Errorf("No Annunci in update")
} }
if codFilter != "" { if codFilter != "" {
@ -160,7 +175,7 @@ func (m *MiogestHandler) ParseAnnunci(codFilter string, imgPool *[]typesdefs.Med
return *a.Codice == codFilter return *a.Codice == codFilter
}) })
if idx == -1 { if idx == -1 {
return typesdefs.AnnunciParsed{} return typesdefs.AnnunciParsed{}, fmt.Errorf("Codice %s not found", codFilter)
} }
annunci.Annuncio = []typesdefs.AnnuncioXML{annunci.Annuncio[idx]} annunci.Annuncio = []typesdefs.AnnuncioXML{annunci.Annuncio[idx]}
} }
@ -244,7 +259,7 @@ func (m *MiogestHandler) ParseAnnunci(codFilter string, imgPool *[]typesdefs.Med
} }
bar.Finish() bar.Finish()
fmt.Println() fmt.Println()
return AnnunciArray return AnnunciArray, nil
} }
var re = regexp.MustCompile(`[^+\d]`) var re = regexp.MustCompile(`[^+\d]`)

View file

@ -0,0 +1,10 @@
#!/bin/sh
set -e # Exit on any error
echo "Starting Check Annunci job..."
if curl -f --connect-timeout 30 --max-time 300 "http://web:3000/api/trpc/annunci.checkAnnunci"; then
echo "Check Annunci job completed successfully"
else
echo "Failed to Check Annunci" >&2
exit 1
fi

View file

@ -1,10 +0,0 @@
#!/bin/sh
set -e # Exit on any error
echo "Starting Test..."
if echo "TEST"; then
echo "Test completed successfully"
else
echo "Failed to Test" >&2
exit 1
fi

View file

@ -93,18 +93,27 @@ func (s *Server) SetupRoutes() *echo.Echo {
}) })
e.GET("/miogest", func(c echo.Context) error { e.GET("/miogest", func(c echo.Context) error {
data := s.miogest.GetAnnunci() data, err := s.miogest.GetAnnunci()
if err != nil {
return c.String(http.StatusInternalServerError, err.Error())
}
return c.JSONPretty(http.StatusOK, data, " ") return c.JSONPretty(http.StatusOK, data, " ")
}) })
e.GET("/miogest-sample", func(c echo.Context) error { e.GET("/miogest-sample", func(c echo.Context) error {
xml := s.miogest.GetAnnunci() xml, err := s.miogest.GetAnnunci()
if err != nil {
return c.String(http.StatusInternalServerError, err.Error())
}
rand := rand.Intn(len(xml.Annuncio)) rand := rand.Intn(len(xml.Annuncio))
data := xml.Annuncio[rand] data := xml.Annuncio[rand]
return c.JSONPretty(http.StatusOK, data, " ") return c.JSONPretty(http.StatusOK, data, " ")
}) })
e.GET("/parse", func(c echo.Context) error { e.GET("/parse", func(c echo.Context) error {
start := time.Now() start := time.Now()
var data = s.miogest.GetAnnunciParsed(nil, nil, false) var data, err = s.miogest.GetAnnunciParsed(nil, nil, false)
if err != nil {
return c.String(http.StatusInternalServerError, err.Error())
}
end := time.Now() end := time.Now()
fmt.Println("time taken:", end.Sub(start).String()) fmt.Println("time taken:", end.Sub(start).String())
return c.JSONPretty(http.StatusOK, data, " ") return c.JSONPretty(http.StatusOK, data, " ")
@ -118,8 +127,11 @@ func (s *Server) SetupRoutes() *echo.Echo {
imgPool := []typesdefs.MediaToProcess{} imgPool := []typesdefs.MediaToProcess{}
videoPool := []typesdefs.MediaToProcess{} videoPool := []typesdefs.MediaToProcess{}
var data = s.miogest.GetAnnunciParsed(&imgPool, &videoPool, forceMediaStale) data, err := s.miogest.GetAnnunciParsed(&imgPool, &videoPool, forceMediaStale)
err := queries.AnnunciInsert(data, true) if err != nil {
return c.String(http.StatusInternalServerError, err.Error())
}
err = queries.AnnunciInsert(data, true)
if err != nil { if err != nil {
return c.String(http.StatusInternalServerError, err.Error()) return c.String(http.StatusInternalServerError, err.Error())
} }
@ -148,8 +160,11 @@ func (s *Server) SetupRoutes() *echo.Echo {
cod := c.Param("cod") cod := c.Param("cod")
imgPool := []typesdefs.MediaToProcess{} imgPool := []typesdefs.MediaToProcess{}
videoPool := []typesdefs.MediaToProcess{} videoPool := []typesdefs.MediaToProcess{}
var data = s.miogest.GetAnnuncioParsed(cod, &imgPool, &videoPool) data, err := s.miogest.GetAnnuncioParsed(cod, &imgPool, &videoPool)
err := queries.AnnunciInsert(data, false) if err != nil {
return c.String(http.StatusInternalServerError, err.Error())
}
err = queries.AnnunciInsert(data, false)
if err != nil { if err != nil {
return c.String(http.StatusInternalServerError, err.Error()) return c.String(http.StatusInternalServerError, err.Error())
} }