From 436f0efab8fd29e5c38a0c60f1c46ba8a01a67e1 Mon Sep 17 00:00:00 2001 From: Tan Jiang Date: Wed, 12 Jul 2017 15:57:57 +0800 Subject: [PATCH] overall timestamp returns 0 when error happens, split check and mark into two func --- src/common/utils/timemarker.go | 14 ++++++++------ src/ui/api/repository.go | 3 ++- src/ui/api/systeminfo.go | 2 +- src/ui/service/notifications/clair/handler.go | 3 ++- 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/common/utils/timemarker.go b/src/common/utils/timemarker.go index 673f0a7da..27dbdb107 100644 --- a/src/common/utils/timemarker.go +++ b/src/common/utils/timemarker.go @@ -37,15 +37,17 @@ type TimeMarker struct { } //Mark tries to mark a future time, which is after the duration of interval from the time it's called. -//It returns false if there is a mark in fugure, true if the mark is successfully set. -func (t *TimeMarker) Mark() bool { +func (t *TimeMarker) Mark() { t.Lock() defer t.Unlock() - if time.Now().Before(t.next) { - return false - } t.next = time.Now().Add(t.interval) - return true +} + +//Check returns true if the current time is after the mark by this marker, and the caction the mark guards and be taken. +func (t *TimeMarker) Check() bool { + t.RLock() + defer t.RUnlock() + return time.Now().After(t.next) } //Next returns the time of the next mark. diff --git a/src/ui/api/repository.go b/src/ui/api/repository.go index 492e0e989..038824d6f 100644 --- a/src/ui/api/repository.go +++ b/src/ui/api/repository.go @@ -746,7 +746,7 @@ func (ra *RepositoryAPI) ScanAll() { return } - if !utils.ScanAllMarker().Mark() { + if !utils.ScanAllMarker().Check() { log.Warningf("There is a scan all scheduled at: %v, the request will not be processed.", utils.ScanAllMarker().Next()) ra.RenderError(http.StatusPreconditionFailed, "Unable handle frequent scan all requests") return @@ -757,6 +757,7 @@ func (ra *RepositoryAPI) ScanAll() { ra.HandleInternalServerError(fmt.Sprintf("Error: %v", err)) return } + utils.ScanAllMarker().Mark() ra.Ctx.ResponseWriter.WriteHeader(http.StatusAccepted) } diff --git a/src/ui/api/systeminfo.go b/src/ui/api/systeminfo.go index 4d5cee00c..1ea2257ca 100644 --- a/src/ui/api/systeminfo.go +++ b/src/ui/api/systeminfo.go @@ -161,7 +161,7 @@ func getClairVulnStatus() *models.ClairVulnerabilityStatus { last, err := clairdao.GetLastUpdate() if err != nil { log.Errorf("Failed to get last update from Clair DB, error: %v", err) - res.OverallUTC = -1 + res.OverallUTC = 0 } else { res.OverallUTC = last log.Debugf("Clair vuln DB last update: %d", last) diff --git a/src/ui/service/notifications/clair/handler.go b/src/ui/service/notifications/clair/handler.go index 6f5053a02..82e8867a2 100644 --- a/src/ui/service/notifications/clair/handler.go +++ b/src/ui/service/notifications/clair/handler.go @@ -76,7 +76,7 @@ func (h *Handler) Handle() { } } } - if utils.ScanOverviewMarker().Mark() { + if utils.ScanOverviewMarker().Check() { go func() { <-time.After(rescanInterval) l, err := dao.ListImgScanOverviews() @@ -92,6 +92,7 @@ func (h *Handler) Handle() { } } }() + utils.ScanOverviewMarker().Mark() } else { log.Debugf("There is a rescan scheduled at %v already, skip.", utils.ScanOverviewMarker().Next()) }