Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plugins: standardize server API and errors #10771

Merged
merged 1 commit into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion plugins/cmd/chainlink-median/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const (
)

func main() {
s := plugins.StartServer(loggerName)
s := plugins.MustNewStartedServer(loggerName)
defer s.Stop()

p := median.NewPlugin(s.Logger)
Expand Down
2 changes: 1 addition & 1 deletion plugins/cmd/chainlink-solana/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const (
)

func main() {
s := plugins.StartServer(loggerName)
s := plugins.MustNewStartedServer(loggerName)
defer s.Stop()

p := &pluginRelayer{Base: plugins.Base{Logger: s.Logger}}
Expand Down
2 changes: 1 addition & 1 deletion plugins/cmd/chainlink-starknet/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const (
)

func main() {
s := plugins.StartServer(loggerName)
s := plugins.MustNewStartedServer(loggerName)
defer s.Stop()

p := &pluginRelayer{Base: plugins.Base{Logger: s.Logger}}
Expand Down
69 changes: 50 additions & 19 deletions plugins/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,47 +9,78 @@ import (
"github.com/smartcontractkit/chainlink/v2/core/services"
)

// StartServer returns a started Server.
// NewStartedServer returns a started Server.
// The caller is responsible for calling Server.Stop().
func StartServer(loggerName string) *Server {
s := Server{
func NewStartedServer(loggerName string) (*Server, error) {
s, err := newServer(loggerName)
if err != nil {
return nil, err
}
err = s.start()
if err != nil {
return nil, err
}

return s, nil
}

// MustNewStartedServer returns a new started Server like NewStartedServer, but logs and exits in the event of error.
// The caller is responsible for calling Server.Stop().
func MustNewStartedServer(loggerName string) *Server {
s, err := newServer(loggerName)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to start server: %s\n", err)
os.Exit(1)
}
err = s.start()
if err != nil {
s.Logger.Fatalf("Failed to start server: %s", err)
}

return s
}

// Server holds common plugin server fields.
type Server struct {
loop.GRPCOpts
Logger logger.SugaredLogger
*PromServer
services.Checker
}

func newServer(loggerName string) (*Server, error) {
s := &Server{
// default prometheus.Registerer
GRPCOpts: loop.SetupTelemetry(nil),
}

lggr, err := loop.NewLogger()
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to create logger: %s\n", err)
os.Exit(1)
return nil, fmt.Errorf("error creating logger: %s", err)
}
lggr = logger.Named(lggr, loggerName)
s.Logger = logger.Sugared(lggr)
return s, nil
}

func (s *Server) start() error {
envCfg, err := GetEnvConfig()
if err != nil {
lggr.Fatalf("Failed to get environment configuration: %s\n", err)
return fmt.Errorf("error getting environment configuration: %w", err)
}
s.PromServer = NewPromServer(envCfg.PrometheusPort(), lggr)
s.PromServer = NewPromServer(envCfg.PrometheusPort(), s.lggr)
err = s.PromServer.Start()
if err != nil {
lggr.Fatalf("Unrecoverable error starting prometheus server: %s", err)
return fmt.Errorf("error starting prometheus server: %w", err)
}

s.Checker = services.NewChecker()
err = s.Checker.Start()
if err != nil {
lggr.Fatalf("Failed to start health checker: %v", err)
return fmt.Errorf("error starting health checker: %w", err)
}

return &s
}

// Server holds common plugin server fields.
type Server struct {
loop.GRPCOpts
Logger logger.SugaredLogger
*PromServer
services.Checker
return nil
}

// MustRegister registers the Checkable with services.Checker, or exits upon failure.
Expand All @@ -62,7 +93,7 @@ func (s *Server) MustRegister(c services.Checkable) {
// Stop closes resources and flushes logs.
func (s *Server) Stop() {
s.Logger.ErrorIfFn(s.Checker.Close, "Failed to close health checker")
s.Logger.ErrorIfFn(s.PromServer.Close, "error closing prometheus server")
s.Logger.ErrorIfFn(s.PromServer.Close, "Failed to close prometheus server")
if err := s.Logger.Sync(); err != nil {
fmt.Println("Failed to sync logger:", err)
}
Expand Down
Loading