2015-11-13 19:11:28 +00:00
// Copyright 2015 clair authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package notifier fetches notifications from the database and sends them
// to the specified remote handler.
package notifier
import (
"time"
"github.com/coreos/pkg/capnslog"
2015-12-15 16:24:58 +00:00
"github.com/coreos/pkg/timeutil"
2015-11-24 04:43:33 +00:00
"github.com/pborman/uuid"
2015-12-07 21:38:50 +00:00
"github.com/coreos/clair/config"
2015-11-13 19:11:28 +00:00
"github.com/coreos/clair/database"
"github.com/coreos/clair/health"
"github.com/coreos/clair/utils"
)
var log = capnslog . NewPackageLogger ( "github.com/coreos/clair" , "notifier" )
const (
2015-12-15 16:24:58 +00:00
checkInterval = 5 * time . Minute
2015-11-24 04:43:33 +00:00
refreshLockDuration = time . Minute * 2
lockDuration = time . Minute * 8 + refreshLockDuration
2015-12-15 16:24:58 +00:00
maxBackOff = 15 * time . Minute
2015-11-13 19:11:28 +00:00
)
2015-12-07 21:38:50 +00:00
// TODO(Quentin-M): Allow registering custom notification handlers.
2015-11-24 04:43:33 +00:00
// A Notification represents the structure of the notifications that are sent by a Notifier.
type Notification struct {
Name , Type string
Content interface { }
2015-11-13 19:11:28 +00:00
}
2015-12-15 16:36:06 +00:00
var notifiers = make ( map [ string ] Notifier )
2015-11-13 19:11:28 +00:00
2015-12-15 16:36:06 +00:00
// Notifier represents anything that can transmit notifications.
type Notifier interface {
// Configure attempts to initialize the notifier with the provided configuration.
// It returns whether the notifier is enabled or not.
Configure ( * config . NotifierConfig ) ( bool , error )
// Send transmits the specified notification.
Send ( notification * Notification ) error
}
2015-12-07 21:38:50 +00:00
2015-12-15 16:36:06 +00:00
// RegisterNotifier makes a Fetcher available by the provided name.
// If Register is called twice with the same name or if driver is nil,
// it panics.
func RegisterNotifier ( name string , n Notifier ) {
if name == "" {
panic ( "notifier: could not register a Notifier with an empty name" )
2015-11-24 04:43:33 +00:00
}
2015-12-15 16:36:06 +00:00
if n == nil {
panic ( "notifier: could not register a nil Notifier" )
2015-11-24 04:43:33 +00:00
}
2015-11-13 19:11:28 +00:00
2015-12-15 16:36:06 +00:00
if _ , dup := notifiers [ name ] ; dup {
panic ( "notifier: RegisterNotifier called twice for " + name )
2015-11-24 04:43:33 +00:00
}
2015-11-13 19:11:28 +00:00
2015-12-15 16:36:06 +00:00
notifiers [ name ] = n
2015-11-24 04:43:33 +00:00
}
2015-11-13 19:11:28 +00:00
2015-12-15 16:36:06 +00:00
// Run starts the Notifier service.
func Run ( config * config . NotifierConfig , stopper * utils . Stopper ) {
2015-11-24 04:43:33 +00:00
defer stopper . End ( )
2015-12-07 21:38:50 +00:00
2015-12-15 16:36:06 +00:00
// Configure registered notifiers.
for notifierName , notifier := range notifiers {
if configured , err := notifier . Configure ( config ) ; configured {
log . Infof ( "notifier '%s' configured\n" , notifierName )
} else {
delete ( notifiers , notifierName )
if err != nil {
log . Errorf ( "could not configure notifier '%s': %s" , notifierName , err )
}
}
}
// Do not run the updater if there is no notifier enabled.
if len ( notifiers ) == 0 {
log . Infof ( "notifier service is disabled" )
2015-12-07 21:38:50 +00:00
return
}
2015-12-15 16:36:06 +00:00
whoAmI := uuid . New ( )
log . Infof ( "notifier service started. lock identifier: %s\n" , whoAmI )
2015-11-13 19:11:28 +00:00
2015-12-15 16:36:06 +00:00
// Register healthchecker.
health . RegisterHealthchecker ( "notifier" , Healthcheck )
2015-11-13 19:11:28 +00:00
2015-12-15 18:23:57 +00:00
for running := true ; running ; {
2015-11-24 04:43:33 +00:00
// Find task.
// TODO(Quentin-M): Combine node and notification.
2015-12-15 16:36:06 +00:00
node , notification := findTask ( whoAmI , stopper )
2015-11-24 04:43:33 +00:00
if node == "" && notification == nil {
2015-12-15 18:23:57 +00:00
// Interrupted while finding a task, Clair is stopping.
2015-11-24 04:43:33 +00:00
break
2015-11-13 19:11:28 +00:00
}
2015-11-24 04:43:33 +00:00
// Handle task.
done := make ( chan bool , 1 )
go func ( ) {
2015-12-15 18:23:57 +00:00
success , interrupted := handleTask ( notification , stopper , config . Attempts )
if success {
2015-11-24 04:43:33 +00:00
database . MarkNotificationAsSent ( node )
2015-11-13 19:11:28 +00:00
}
2015-12-15 18:23:57 +00:00
if interrupted {
running = false
}
2015-12-15 16:36:06 +00:00
database . Unlock ( node , whoAmI )
2015-11-24 04:43:33 +00:00
done <- true
} ( )
// Refresh task lock until done.
outer :
for {
select {
case <- done :
break outer
case <- time . After ( refreshLockDuration ) :
2015-12-15 16:36:06 +00:00
database . Lock ( node , lockDuration , whoAmI )
2015-11-13 19:11:28 +00:00
}
2015-11-24 04:43:33 +00:00
}
}
2015-11-13 19:11:28 +00:00
2015-11-24 04:43:33 +00:00
log . Info ( "notifier service stopped" )
}
2015-11-13 19:11:28 +00:00
2015-12-15 16:36:06 +00:00
func findTask ( whoAmI string , stopper * utils . Stopper ) ( string , database . Notification ) {
2015-11-24 04:43:33 +00:00
for {
// Find a notification to send.
node , notification , err := database . FindOneNotificationToSend ( database . GetDefaultNotificationWrapper ( ) )
if err != nil {
log . Warningf ( "could not get notification to send: %s" , err )
}
2015-11-13 19:11:28 +00:00
2015-11-24 04:43:33 +00:00
// No notification or error: wait.
if notification == nil || err != nil {
if ! stopper . Sleep ( checkInterval ) {
return "" , nil
2015-11-13 19:11:28 +00:00
}
2015-11-24 04:43:33 +00:00
continue
}
2015-11-13 19:11:28 +00:00
2015-11-24 04:43:33 +00:00
// Lock the notification.
2015-12-15 16:36:06 +00:00
if hasLock , _ := database . Lock ( node , lockDuration , whoAmI ) ; hasLock {
2015-11-24 04:43:33 +00:00
log . Infof ( "found and locked a notification: %s" , notification . GetName ( ) )
return node , notification
}
}
}
2015-12-15 18:23:57 +00:00
func handleTask ( notification database . Notification , st * utils . Stopper , maxAttempts int ) ( bool , bool ) {
2015-11-24 04:43:33 +00:00
// Get notification content.
// TODO(Quentin-M): Split big notifications.
notificationContent , err := notification . GetContent ( )
if err != nil {
log . Warningf ( "could not get content of notification '%s': %s" , notification . GetName ( ) , err )
2015-12-15 18:23:57 +00:00
return false , false
2015-11-24 04:43:33 +00:00
}
2015-11-13 19:11:28 +00:00
2015-11-24 04:43:33 +00:00
// Create notification.
2015-12-15 16:36:06 +00:00
payload := & Notification {
2015-11-24 04:43:33 +00:00
Name : notification . GetName ( ) ,
Type : notification . GetType ( ) ,
Content : notificationContent ,
}
2015-11-13 19:11:28 +00:00
2015-11-24 04:43:33 +00:00
// Send notification.
2015-12-15 16:36:06 +00:00
for notifierName , notifier := range notifiers {
2015-12-15 16:24:58 +00:00
var attempts int
var backOff time . Duration
for {
// Max attempts exceeded.
if attempts >= maxAttempts {
log . Infof ( "giving up on sending notification '%s' to notifier '%s': max attempts exceeded (%d)\n" , notification . GetName ( ) , notifierName , maxAttempts )
2015-12-15 18:23:57 +00:00
return false , false
2015-12-15 16:24:58 +00:00
}
// Backoff.
if backOff > 0 {
log . Infof ( "waiting %v before retrying to send notification '%s' to notifier '%s' (Attempt %d / %d)\n" , backOff , notification . GetName ( ) , notifierName , attempts + 1 , maxAttempts )
if ! st . Sleep ( backOff ) {
2015-12-15 18:23:57 +00:00
return false , true
2015-12-15 16:24:58 +00:00
}
}
// Send using the current notifier.
if err := notifier . Send ( payload ) ; err == nil {
// Send has been successful. Go to the next one.
break
}
// Send failed; increase attempts/backoff and retry.
2015-12-15 16:36:06 +00:00
log . Errorf ( "could not send notification '%s' to notifier '%s': %s" , notification . GetName ( ) , notifierName , err )
2015-12-15 16:24:58 +00:00
backOff = timeutil . ExpBackoff ( backOff , maxBackOff )
attempts ++
2015-12-15 16:36:06 +00:00
}
2015-11-13 19:11:28 +00:00
}
2015-11-24 04:43:33 +00:00
log . Infof ( "successfully sent notification '%s'\n" , notification . GetName ( ) )
2015-12-15 18:23:57 +00:00
return true , false
2015-11-13 19:11:28 +00:00
}
2015-11-24 04:43:33 +00:00
// Healthcheck returns the health of the notifier service.
2015-12-15 16:36:06 +00:00
func Healthcheck ( ) health . Status {
2015-11-13 19:11:28 +00:00
queueSize , err := database . CountNotificationsToSend ( )
return health . Status { IsEssential : false , IsHealthy : err == nil , Details : struct { QueueSize int } { QueueSize : queueSize } }
}