From ae13664f2eaa3575ed2264f28a44291c30b2ba94 Mon Sep 17 00:00:00 2001 From: Victor Yves Crispim Date: Thu, 9 Nov 2023 11:25:42 -0300 Subject: [PATCH] refactor: replace Service with its implementation Currently, there isn't a need to have multiple implementations of the Service interface. Therefore, this commits replaces it with its only implementation to keep things simple. --- internal/services/service.go | 60 ++++++++----------- ...simple_service_test.go => service_test.go} | 8 +-- 2 files changed, 29 insertions(+), 39 deletions(-) rename internal/services/{simple_service_test.go => service_test.go} (88%) diff --git a/internal/services/service.go b/internal/services/service.go index f00726bce..206a22a7d 100644 --- a/internal/services/service.go +++ b/internal/services/service.go @@ -1,7 +1,8 @@ // (c) Cartesi and individual authors (see AUTHORS) // SPDX-License-Identifier: Apache-2.0 (see LICENSE) -// Package services provides mechanisms to start multiple services in the background +// Package services provides mechanisms to start multiple services in the +// background package services import ( @@ -17,18 +18,6 @@ import ( "github.com/cartesi/rollups-node/internal/logger" ) -// A service that runs in the background endlessly until the context is canceled -type Service interface { - fmt.Stringer - - // Start a service that will run until completion or until the context is - // canceled - Start(ctx context.Context) error - - // Block until the service is ready or context is canceled - Ready(ctx context.Context) error -} - const ( DefaultServiceTimeout = 15 * time.Second DefaultDialInterval = 100 * time.Millisecond @@ -36,14 +25,15 @@ const ( DefaultHealthcheckPort = "8080" ) -// simpleService implements the context cancelation logic of the Service interface -type simpleService struct { - serviceName string +type Service struct { + name string binaryName string healthcheckPort string } -func (s simpleService) Start(ctx context.Context) error { +// Start will execute a binary and wait for its completion or until the context +// is canceled +func (s Service) Start(ctx context.Context) error { cmd := exec.Command(s.binaryName) cmd.Stderr = os.Stderr cmd.Stdout = os.Stdout @@ -57,7 +47,7 @@ func (s simpleService) Start(ctx context.Context) error { logger.Debug.Printf("%v: %v\n", s.String(), ctx.Err()) if err := cmd.Process.Signal(syscall.SIGTERM); err != nil { msg := "%v: failed to send SIGTERM to %v\n" - logger.Error.Printf(msg, s.String(), s.binaryName) + logger.Error.Printf(msg, s.String(), s.name) } }() @@ -72,7 +62,7 @@ func (s simpleService) Start(ctx context.Context) error { // // A service is considered ready when it is possible to establish a connection // to its healthcheck endpoint. -func (s simpleService) Ready(ctx context.Context) error { +func (s Service) Ready(ctx context.Context) error { for { select { case <-ctx.Done(): @@ -89,8 +79,8 @@ func (s simpleService) Ready(ctx context.Context) error { } } -func (s simpleService) String() string { - return s.serviceName +func (s Service) String() string { + return s.name } // The Run function serves as a very simple supervisor: it will start all the @@ -102,7 +92,7 @@ func Run(ctx context.Context, services []Service) { } // start services - startedServices := 0 + startedServicesCount := 0 ctx, cancel := context.WithCancel(ctx) exit := make(chan struct{}, len(services)) for _, service := range services { @@ -138,7 +128,7 @@ func Run(ctx context.Context, services []Service) { wait := make(chan struct{}) go func() { cancel() - for i := 0; i < startedServices; i++ { + for i := 0; i < startedServicesCount; i++ { <-exit } wait <- struct{}{} @@ -184,33 +174,33 @@ func healthcheckEnv(serviceName string) string { } var ( - AdvanceRunner = simpleService{ - serviceName: "advance-runner", + AdvanceRunner = Service{ + name: "advance-runner", binaryName: "cartesi-rollups-advance-runner", healthcheckPort: healthcheckPort("advance-runner"), } - Dispatcher = simpleService{ - serviceName: "dispatcher", + Dispatcher = Service{ + name: "dispatcher", binaryName: "cartesi-rollups-dispatcher", healthcheckPort: healthcheckPort("dispatcher"), } - GraphQLServer Service = simpleService{ - serviceName: "graphql-server", + GraphQLServer Service = Service{ + name: "graphql-server", binaryName: "cartesi-rollups-graphql-server", healthcheckPort: healthcheckPort("graphql-server"), } - Indexer Service = simpleService{ - serviceName: "indexer", + Indexer Service = Service{ + name: "indexer", binaryName: "cartesi-rollups-indexer", healthcheckPort: healthcheckPort("indexer"), } - InspectServer = simpleService{ - serviceName: "inspect-server", + InspectServer = Service{ + name: "inspect-server", binaryName: "cartesi-rollups-inspect-server", healthcheckPort: healthcheckPort("inspect-server"), } - StateServer = simpleService{ - serviceName: "state-server", + StateServer = Service{ + name: "state-server", binaryName: "cartesi-rollups-state-server", healthcheckPort: healthcheckPort("state-server"), } diff --git a/internal/services/simple_service_test.go b/internal/services/service_test.go similarity index 88% rename from internal/services/simple_service_test.go rename to internal/services/service_test.go index 5dd7871a9..8ef7a9fa5 100644 --- a/internal/services/simple_service_test.go +++ b/internal/services/service_test.go @@ -20,13 +20,13 @@ func setup() { setRustBinariesPath() } -func TestSimpleService(t *testing.T) { +func TestService(t *testing.T) { t.Run("it stops when the context is cancelled", func(t *testing.T) { setup() - service := simpleService{ - serviceName: "graphql-server", - binaryName: "cartesi-rollups-graphql-server", + service := Service{ + name: "graphql-server", + binaryName: "cartesi-rollups-graphql-server", } ctx, cancel := context.WithCancel(context.Background()) exit := make(chan error)