overall timestamp returns 0 when error happens, split check and mark into two func

This commit is contained in:
Tan Jiang 2017-07-12 15:57:57 +08:00
parent 5f233f3e21
commit 436f0efab8
4 changed files with 13 additions and 9 deletions

View File

@ -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.

View File

@ -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)
}

View File

@ -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)

View File

@ -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())
}