2019-12-30 19:42:10 +00:00
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package downtime
import (
"context"
"github.com/zeebo/errs"
"go.uber.org/zap"
2020-01-08 13:40:19 +00:00
2019-12-30 19:42:10 +00:00
"storj.io/common/storj"
"storj.io/storj/satellite/contact"
"storj.io/storj/satellite/overlay"
)
// Service is a service for downtime checking.
//
// architecture: Service
type Service struct {
log * zap . Logger
overlay * overlay . Service
contact * contact . Service
}
// NewService creates a new downtime tracking service.
func NewService ( log * zap . Logger , overlay * overlay . Service , contact * contact . Service ) * Service {
return & Service {
log : log ,
overlay : overlay ,
contact : contact ,
}
}
// CheckAndUpdateNodeAvailability tries to ping the supplied address and updates the uptime based on ping success or failure. Returns true if the ping and uptime updates are successful.
2020-05-19 17:42:00 +01:00
func ( service * Service ) CheckAndUpdateNodeAvailability ( ctx context . Context , nodeurl storj . NodeURL ) ( success bool , err error ) {
2020-01-02 20:41:18 +00:00
defer mon . Task ( ) ( & ctx ) ( & err )
2020-05-19 17:42:00 +01:00
pingNodeSuccess , pingErrorMessage , err := service . contact . PingBack ( ctx , nodeurl )
2019-12-30 19:42:10 +00:00
if err != nil {
service . log . Error ( "error during downtime detection ping back." ,
zap . String ( "ping error" , pingErrorMessage ) ,
zap . Error ( err ) )
return false , errs . Wrap ( err )
}
if pingNodeSuccess {
2020-05-19 17:42:00 +01:00
_ , err = service . overlay . UpdateUptime ( ctx , nodeurl . ID , true )
2019-12-30 19:42:10 +00:00
if err != nil {
service . log . Error ( "error updating node contact success information." ,
2020-05-19 17:42:00 +01:00
zap . Stringer ( "node ID" , nodeurl . ID ) ,
2019-12-30 19:42:10 +00:00
zap . Error ( err ) )
return false , errs . Wrap ( err )
}
return true , nil
}
2020-05-19 17:42:00 +01:00
_ , err = service . overlay . UpdateUptime ( ctx , nodeurl . ID , false )
2019-12-30 19:42:10 +00:00
if err != nil {
service . log . Error ( "error updating node contact failure information." ,
2020-05-19 17:42:00 +01:00
zap . Stringer ( "node ID" , nodeurl . ID ) ,
2019-12-30 19:42:10 +00:00
zap . Error ( err ) )
return false , errs . Wrap ( err )
}
return false , nil
}
2020-07-16 15:18:02 +01:00
// Close closes resources.
2019-12-30 19:42:10 +00:00
func ( service * Service ) Close ( ) error { return nil }