From c9d77682f82d4e6c70563efb5eee9a8de8b097f8 Mon Sep 17 00:00:00 2001 From: Vladimir Stoilov Date: Fri, 10 Mar 2023 16:42:04 +0100 Subject: [PATCH] Fix report downloads deleting pending updates --- api/database.go | 10 +++++----- api/endpoints.go | 2 +- updater/resource.go | 2 +- updater/state.go | 18 ++++++++++++++---- 4 files changed, 21 insertions(+), 11 deletions(-) diff --git a/api/database.go b/api/database.go index a6d7809..804dc1a 100644 --- a/api/database.go +++ b/api/database.go @@ -290,7 +290,7 @@ func (api *DatabaseAPI) handleGet(opID []byte, key string) { r, err := api.db.Get(key) if err == nil { - data, err = marshalRecord(r, true) + data, err = MarshalRecord(r, true) } if err != nil { api.send(opID, dbMsgTypeError, err.Error(), nil) @@ -348,7 +348,7 @@ func (api *DatabaseAPI) processQuery(opID []byte, q *query.Query) (ok bool) { // process query feed if r != nil { // process record - data, err := marshalRecord(r, true) + data, err := MarshalRecord(r, true) if err != nil { api.send(opID, dbMsgTypeWarning, err.Error(), nil) continue @@ -425,7 +425,7 @@ func (api *DatabaseAPI) processSub(opID []byte, sub *database.Subscription) { // process sub feed if r != nil { // process record - data, err := marshalRecord(r, true) + data, err := MarshalRecord(r, true) if err != nil { api.send(opID, dbMsgTypeWarning, err.Error(), nil) continue @@ -629,9 +629,9 @@ func (api *DatabaseAPI) handleDelete(opID []byte, key string) { api.send(opID, dbMsgTypeSuccess, emptyString, nil) } -// marsharlRecords locks and marshals the given record, additionally adding +// MarshalRecords locks and marshals the given record, additionally adding // metadata and returning it as json. -func marshalRecord(r record.Record, withDSDIdentifier bool) ([]byte, error) { +func MarshalRecord(r record.Record, withDSDIdentifier bool) ([]byte, error) { r.Lock() defer r.Unlock() diff --git a/api/endpoints.go b/api/endpoints.go index 9e2586a..3a005af 100644 --- a/api/endpoints.go +++ b/api/endpoints.go @@ -452,7 +452,7 @@ func (e *Endpoint) ServeHTTP(w http.ResponseWriter, r *http.Request) { var rec record.Record rec, err = e.RecordFunc(apiRequest) if err == nil && r != nil { - responseData, err = marshalRecord(rec, false) + responseData, err = MarshalRecord(rec, false) } case e.HandlerFunc != nil: diff --git a/updater/resource.go b/updater/resource.go index f62b435..5b94516 100644 --- a/updater/resource.go +++ b/updater/resource.go @@ -93,7 +93,7 @@ func (rv *ResourceVersion) String() string { return rv.VersionNumber } -// SemVer returns the semantiv version of the resource. +// SemVer returns the semantic version of the resource. func (rv *ResourceVersion) SemVer() *semver.Version { return rv.semVer } diff --git a/updater/state.go b/updater/state.go index 8ae12b5..11d1446 100644 --- a/updater/state.go +++ b/updater/state.go @@ -13,7 +13,7 @@ const ( StateFetching = "fetching" // Fetching a single file. ) -// RegistryState describtes the registry state. +// RegistryState describes the registry state. type RegistryState struct { sync.Mutex reg *ResourceRegistry @@ -58,7 +58,7 @@ type UpdateState struct { LastDownloadAt *time.Time // LastDownloadError holds the error of the last download. LastDownloadError error - // LastDownload holds the resources that we downloaded the last time udpates + // LastDownload holds the resources that we downloaded the last time updates // were downloaded. LastDownload []string @@ -145,8 +145,18 @@ func (s *RegistryState) ReportDownloads(downloaded []string, failed error) { s.Updates.LastDownloadError = failed s.Updates.LastDownload = downloaded - // Reset pending downloads, as they have now been downloaded. - s.Updates.PendingDownload = nil + // Remote downloaded resources from the pending list. + var newPendingDownload []string +outer: + for _, pend := range s.Updates.PendingDownload { + for _, down := range downloaded { + if pend == down { + continue outer + } + } + newPendingDownload = append(newPendingDownload, pend) + } + s.Updates.PendingDownload = newPendingDownload if failed == nil { s.Updates.LastSuccessAt = &now