Skip to content

Commit

Permalink
clair: mv notifier to top level
Browse files Browse the repository at this point in the history
  • Loading branch information
jzelinskie committed Jan 26, 2017
1 parent 9c63a63 commit e5c567f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 20 deletions.
3 changes: 1 addition & 2 deletions cmd/clair/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (
"github.com/coreos/clair/api/context"
"github.com/coreos/clair/config"
"github.com/coreos/clair/database"
"github.com/coreos/clair/notifier"
"github.com/coreos/clair/pkg/stopper"

// Register database driver.
Expand Down Expand Up @@ -102,7 +101,7 @@ func Boot(config *config.Config) {

// Start notifier
st.Begin()
go notifier.Run(config.Notifier, db, st)
go clair.RunNotifier(config.Notifier, db, st)

// Start API
st.Begin()
Expand Down
31 changes: 13 additions & 18 deletions notifier/notifier.go → notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// Package notifier fetches notifications from the database and informs the
// specified remote handler about their existences, inviting the third party to
// actively query the API about it.
package notifier
package clair

import (
"time"

"github.com/coreos/pkg/capnslog"
"github.com/coreos/pkg/timeutil"
"github.com/pborman/uuid"
"github.com/prometheus/client_golang/prometheus"
Expand All @@ -33,15 +29,13 @@ import (
)

const (
checkInterval = 5 * time.Minute
refreshLockDuration = time.Minute * 2
lockDuration = time.Minute*8 + refreshLockDuration
maxBackOff = 15 * time.Minute
notifierCheckInterval = 5 * time.Minute
notifierMaxBackOff = 15 * time.Minute
notifierLockRefreshDuration = time.Minute * 2
notifierLockDuration = time.Minute*8 + notifierLockRefreshDuration
)

var (
log = capnslog.NewPackageLogger("github.com/coreos/clair", "notifier")

promNotifierLatencyMilliseconds = prometheus.NewHistogram(prometheus.HistogramOpts{
Name: "clair_notifier_latency_milliseconds",
Help: "Time it takes to send a notification after it's been created.",
Expand All @@ -58,8 +52,9 @@ func init() {
prometheus.MustRegister(promNotifierBackendErrorsTotal)
}

// Run starts the Notifier service.
func Run(config *config.NotifierConfig, datastore database.Datastore, stopper *stopper.Stopper) {
// RunNotifier begins a process that checks for new notifications that should
// be sent out to third parties.
func RunNotifier(config *config.NotifierConfig, datastore database.Datastore, stopper *stopper.Stopper) {
defer stopper.End()

// Configure registered notifiers.
Expand Down Expand Up @@ -113,8 +108,8 @@ func Run(config *config.NotifierConfig, datastore database.Datastore, stopper *s
select {
case <-done:
break outer
case <-time.After(refreshLockDuration):
datastore.Lock(notification.Name, whoAmI, lockDuration, true)
case <-time.After(notifierLockRefreshDuration):
datastore.Lock(notification.Name, whoAmI, notifierLockDuration, true)
}
}
}
Expand All @@ -133,15 +128,15 @@ func findTask(datastore database.Datastore, renotifyInterval time.Duration, whoA
}

// Wait.
if !stopper.Sleep(checkInterval) {
if !stopper.Sleep(notifierCheckInterval) {
return nil
}

continue
}

// Lock the notification.
if hasLock, _ := datastore.Lock(notification.Name, whoAmI, lockDuration, false); hasLock {
if hasLock, _ := datastore.Lock(notification.Name, whoAmI, notifierLockDuration, false); hasLock {
log.Infof("found and locked a notification: %s", notification.Name)
return &notification
}
Expand Down Expand Up @@ -173,7 +168,7 @@ func handleTask(n database.VulnerabilityNotification, st *stopper.Stopper, maxAt
// Send failed; increase attempts/backoff and retry.
promNotifierBackendErrorsTotal.WithLabelValues(senderName).Inc()
log.Errorf("could not send notification '%s' via notifier '%s': %v", n.Name, senderName, err)
backOff = timeutil.ExpBackoff(backOff, maxBackOff)
backOff = timeutil.ExpBackoff(backOff, notifierMaxBackOff)
attempts++
continue
}
Expand Down

0 comments on commit e5c567f

Please sign in to comment.