Skip to content

Commit

Permalink
feat: move Node Services creation from cmd to internal Library
Browse files Browse the repository at this point in the history
  • Loading branch information
fmoura committed Mar 7, 2024
1 parent 2c11f31 commit e6e3b7c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 42 deletions.
40 changes: 7 additions & 33 deletions cmd/cartesi-rollups-node/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,51 +10,21 @@ import (
"time"

"github.com/cartesi/rollups-node/internal/config"
"github.com/cartesi/rollups-node/internal/node"
"github.com/cartesi/rollups-node/internal/services"
)

func main() {
startTime := time.Now()
var s []services.Service

ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()

nodeConfig := config.NewNodeConfigFromEnv()

nodeConfig.Validate()

config.InitLog(nodeConfig)

sunodoValidatorEnabled := nodeConfig.CartesiExperimentalSunodoValidatorEnabled()
if !sunodoValidatorEnabled {
// add Redis first
s = append(s, newRedis(nodeConfig))
}

// add services without dependencies
s = append(s, newGraphQLServer(nodeConfig))
s = append(s, newIndexer(nodeConfig))
s = append(s, newStateServer(nodeConfig))

// start either the server manager or host runner
if nodeConfig.CartesiFeatureHostMode() {
s = append(s, newHostRunner(nodeConfig))
} else {
s = append(s, newServerManager(nodeConfig))
}

// enable claimer if reader mode and sunodo validator mode are disabled
if !nodeConfig.CartesiFeatureDisableClaimer() && !sunodoValidatorEnabled {
s = append(s, newAuthorityClaimer(nodeConfig))
}

// add services with dependencies
s = append(s, newAdvanceRunner(nodeConfig)) // Depends on the server-manager/host-runner
s = append(s, newDispatcher(nodeConfig)) // Depends on the state server
s = append(s, newInspectServer(nodeConfig)) // Depends on the server-manager/host-runner

s = append(s, newHttpService(nodeConfig))
s := node.NewNodeServices(nodeConfig)

ready := make(chan struct{}, 1)
// logs startup time
Expand All @@ -68,7 +38,11 @@ func main() {
}()

// start supervisor
supervisor := newSupervisorService(s)
supervisor := services.SupervisorService{
Name: "rollups-node",
Services: s,
}

if err := supervisor.Start(ctx, ready); err != nil {
config.ErrorLogger.Print(err)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// (c) Cartesi and individual authors (see AUTHORS)
// SPDX-License-Identifier: Apache-2.0 (see LICENSE)

package main
package node

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// (c) Cartesi and individual authors (see AUTHORS)
// SPDX-License-Identifier: Apache-2.0 (see LICENSE)

package main
package node

import (
"fmt"
Expand Down Expand Up @@ -387,13 +387,6 @@ func newStateServer(nodeConfig config.NodeConfig) services.CommandService {
return s
}

func newSupervisorService(s []services.Service) services.SupervisorService {
return services.SupervisorService{
Name: "rollups-node",
Services: s,
}
}

func newHttpService(nodeConfig config.NodeConfig) services.HttpService {
addr := fmt.Sprintf(
"%v:%v",
Expand All @@ -407,3 +400,38 @@ func newHttpService(nodeConfig config.NodeConfig) services.HttpService {
Handler: handler,
}
}

func NewNodeServices(nodeConfig config.NodeConfig) []services.Service {
var s []services.Service
sunodoValidatorEnabled := nodeConfig.CartesiExperimentalSunodoValidatorEnabled()
if !sunodoValidatorEnabled {
// add Redis first
s = append(s, newRedis(nodeConfig))
}

// add services without dependencies
s = append(s, newGraphQLServer(nodeConfig))
s = append(s, newIndexer(nodeConfig))
s = append(s, newStateServer(nodeConfig))

// start either the server manager or host runner
if nodeConfig.CartesiFeatureHostMode() {
s = append(s, newHostRunner(nodeConfig))
} else {
s = append(s, newServerManager(nodeConfig))
}

// enable claimer if reader mode and sunodo validator mode are disabled
if !nodeConfig.CartesiFeatureDisableClaimer() && !sunodoValidatorEnabled {
s = append(s, newAuthorityClaimer(nodeConfig))
}

// add services with dependencies
s = append(s, newAdvanceRunner(nodeConfig)) // Depends on the server-manager/host-runner
s = append(s, newDispatcher(nodeConfig)) // Depends on the state server
s = append(s, newInspectServer(nodeConfig)) // Depends on the server-manager/host-runner

s = append(s, newHttpService(nodeConfig))

return s
}

0 comments on commit e6e3b7c

Please sign in to comment.