mirror of
https://github.com/Luzifer/go-latestver.git
synced 2024-12-20 10:31:16 +00:00
Store fetch-errors
Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
c149320fda
commit
331a0657ad
3 changed files with 26 additions and 12 deletions
|
@ -37,6 +37,7 @@ type (
|
|||
CatalogName string `gorm:"primaryKey" json:"-"`
|
||||
CatalogTag string `gorm:"primaryKey" json:"-"`
|
||||
CurrentVersion string `json:"current_version,omitempty"`
|
||||
Error string `json:"error,omitempty"`
|
||||
LastChecked *time.Time `json:"last_checked,omitempty"`
|
||||
VersionTime *time.Time `json:"version_time,omitempty"`
|
||||
}
|
||||
|
|
|
@ -33,7 +33,11 @@ func (h HTMLFetcher) FetchVersion(ctx context.Context, attrs *fieldcollection.Fi
|
|||
return "", time.Time{}, errors.Wrap(err, "querying xpath")
|
||||
}
|
||||
|
||||
if node.Type == html.ElementNode && node.FirstChild.Type == html.TextNode {
|
||||
if node == nil {
|
||||
return "", time.Time{}, errors.New("xpath expression lead to nil-node")
|
||||
}
|
||||
|
||||
if node.Type == html.ElementNode && node.FirstChild != nil && node.FirstChild.Type == html.TextNode {
|
||||
node = node.FirstChild
|
||||
}
|
||||
|
||||
|
|
31
scheduler.go
31
scheduler.go
|
@ -60,13 +60,21 @@ func checkForUpdates(ce *database.CatalogEntry) error {
|
|||
logger.Debug("Checking for updates")
|
||||
|
||||
ver, vertime, err := fetcher.Get(ce.Fetcher).FetchVersion(context.Background(), &ce.FetcherConfig)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "fetching version")
|
||||
}
|
||||
|
||||
ver = strings.TrimPrefix(ver, "v")
|
||||
|
||||
if cm.CurrentVersion != ver {
|
||||
switch {
|
||||
|
||||
case err != nil:
|
||||
log.WithField("entry", ce.Key()).WithError(err).Error("Fetcher caused error, error is stored in entry")
|
||||
cm.Error = err.Error()
|
||||
|
||||
case cm.CurrentVersion != ver:
|
||||
|
||||
logger.WithFields(log.Fields{
|
||||
"from": cm.CurrentVersion,
|
||||
"to": ver,
|
||||
}).Info("Entry had version update")
|
||||
|
||||
if err = storage.Logs.Add(&database.LogEntry{
|
||||
CatalogName: ce.Name,
|
||||
CatalogTag: ce.Tag,
|
||||
|
@ -76,15 +84,16 @@ func checkForUpdates(ce *database.CatalogEntry) error {
|
|||
}); err != nil {
|
||||
return errors.Wrap(err, "adding log entry")
|
||||
}
|
||||
logger.WithFields(log.Fields{
|
||||
"from": cm.CurrentVersion,
|
||||
"to": ver,
|
||||
}).Info("Entry had version update")
|
||||
|
||||
cm.VersionTime = func(v time.Time) *time.Time { return &v }(vertime)
|
||||
cm.CurrentVersion = ver
|
||||
fallthrough
|
||||
|
||||
default:
|
||||
cm.Error = ""
|
||||
|
||||
}
|
||||
|
||||
cm.CurrentVersion = ver
|
||||
cm.LastChecked = func(v time.Time) *time.Time { return &v }(time.Now())
|
||||
|
||||
return errors.Wrap(storage.Catalog.PutMeta(cm), "updating meta entry")
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue