Skip to content

Commit

Permalink
refactor: replace Service with its implementation
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
torives committed Nov 14, 2023
1 parent af1ed09 commit ae13664
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 39 deletions.
60 changes: 25 additions & 35 deletions internal/services/service.go
Original file line number Diff line number Diff line change
@@ -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 (
Expand All @@ -17,33 +18,22 @@ 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
DefaultStateServerPort = "50051"
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
Expand All @@ -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)
}
}()

Expand All @@ -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():
Expand All @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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{}{}
Expand Down Expand Up @@ -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"),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit ae13664

Please sign in to comment.