-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add the standalone advancer command
- Loading branch information
Showing
4 changed files
with
154 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// (c) Cartesi and individual authors (see AUTHORS) | ||
// SPDX-License-Identifier: Apache-2.0 (see LICENSE) | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log/slog" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
"time" | ||
|
||
"github.com/cartesi/rollups-node/internal/node/advancer" | ||
"github.com/cartesi/rollups-node/internal/node/advancer/machines" | ||
"github.com/cartesi/rollups-node/internal/node/config" | ||
"github.com/cartesi/rollups-node/internal/node/startup" | ||
"github.com/cartesi/rollups-node/internal/repository" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
const CMD_NAME = "advancer" | ||
|
||
var ( | ||
buildVersion = "devel" | ||
Cmd = &cobra.Command{ | ||
Use: CMD_NAME, | ||
Short: "Runs the Advancer", | ||
Long: "Runs the Advancer in standalone mode", | ||
RunE: run, | ||
} | ||
) | ||
|
||
func main() { | ||
err := Cmd.Execute() | ||
if err != nil { | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
func getDatabase(ctx context.Context, c config.NodeConfig) (*repository.Database, error) { | ||
err := startup.ValidateSchema(c) | ||
if err != nil { | ||
return nil, fmt.Errorf("invalid database schema: %w", err) | ||
} | ||
|
||
database, err := repository.Connect(ctx, c.PostgresEndpoint.Value) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to connect to the database: %w", err) | ||
} | ||
|
||
return database, nil | ||
} | ||
|
||
func run(cmd *cobra.Command, args []string) error { | ||
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) | ||
defer stop() | ||
|
||
c := config.FromEnv() | ||
startup.ConfigLogs(c) | ||
|
||
slog.Info("Starting the Cartesi Rollups Node Advancer", "version", buildVersion, "config", c) | ||
|
||
database, err := getDatabase(ctx, c) | ||
if err != nil { | ||
return err | ||
} | ||
defer database.Close() | ||
|
||
repo := &repository.MachineRepository{Database: database} | ||
|
||
machines, err := machines.Load(ctx, repo, c.MachineServerVerbosity) | ||
if err != nil { | ||
return fmt.Errorf("failed to load the machines: %w", err) | ||
} | ||
defer machines.Close() | ||
|
||
advancer, err := advancer.New(machines, repo) | ||
if err != nil { | ||
return fmt.Errorf("failed to create the advancer: %w", err) | ||
} | ||
|
||
poller, err := advancer.Poller(5 * time.Second) //nolint: mnd | ||
if err != nil { | ||
return fmt.Errorf("failed to create the advancer service: %w", err) | ||
} | ||
|
||
ready := make(chan struct{}, 1) | ||
|
||
err = poller.Start(ctx, ready) | ||
if err != nil { | ||
return fmt.Errorf("failed to start the advancer service: %w", err) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters