Skip to content

Commit

Permalink
database: add FindLock dbutil
Browse files Browse the repository at this point in the history
  • Loading branch information
jzelinskie committed Nov 28, 2018
1 parent 4fbeb9c commit 300bb52
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
22 changes: 22 additions & 0 deletions database/dbutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package database

import (
"errors"
"time"

"github.com/deckarep/golang-set"
Expand Down Expand Up @@ -350,3 +351,24 @@ func ReleaseLock(datastore Datastore, name, owner string) {
return
}
}

// ErrLockNotFound is returned when FindLock succeeds, but cannot find a lock.
var ErrLockNotFound = errors.New("no lock was found")

// FindLock determines if a global lock with the provided name already exists.
func FindLock(datastore Datastore, updaterLockName string) (owner string, expiration time.Time, err error) {
var tx Session
tx, err = datastore.Begin()
if err != nil {
return
}
defer tx.Rollback()

var found bool
owner, expiration, found, err = tx.FindLock(updaterLockName)
if err != nil && !found {
err = ErrLockNotFound
}

return
}
15 changes: 3 additions & 12 deletions updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,9 @@ func RunUpdater(config *UpdaterConfig, datastore database.Datastore, st *stopper
}
continue
} else {
lockOwner, lockExpiration, ok, err := findLock(datastore, updaterLockName)
if !ok || err != nil {
log.Debug("update lock is already taken")
lockOwner, lockExpiration, err := database.FindLock(datastore, updaterLockName)
if err != nil {
log.WithError(err).Warn("failed to find update lock")
nextUpdate = hasLockUntil
} else {
log.WithFields(log.Fields{"lock owner": lockOwner, "lock expiration": lockExpiration}).Debug("update lock is already taken")
Expand Down Expand Up @@ -464,15 +464,6 @@ func doVulnerabilitiesNamespacing(vulnerabilities []database.VulnerabilityWithAf
return response
}

func findLock(datastore database.Datastore, updaterLockName string) (string, time.Time, bool, error) {
tx, err := datastore.Begin()
if err != nil {
log.WithError(err).Error()
}
defer tx.Rollback()
return tx.FindLock(updaterLockName)
}

// updateUpdaterFlags updates the flags specified by updaters, every transaction
// is independent of each other.
func updateUpdaterFlags(datastore database.Datastore, flags map[string]string) error {
Expand Down

0 comments on commit 300bb52

Please sign in to comment.