-
Notifications
You must be signed in to change notification settings - Fork 840
Remove app package
#4185
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
base: master
Are you sure you want to change the base?
Remove app package
#4185
Changes from all commits
9b20c2a
e857360
7f65bbd
fe712a1
070c100
929efa2
1c37f97
0d527df
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,12 +8,15 @@ import ( | |
| "errors" | ||
| "fmt" | ||
| "os" | ||
| "os/signal" | ||
| "syscall" | ||
|
|
||
| "github.com/spf13/pflag" | ||
| "golang.org/x/term" | ||
|
|
||
| "github.com/ava-labs/avalanchego/app" | ||
| "github.com/ava-labs/avalanchego/config" | ||
| "github.com/ava-labs/avalanchego/node" | ||
| "github.com/ava-labs/avalanchego/utils" | ||
| "github.com/ava-labs/avalanchego/version" | ||
| ) | ||
|
|
||
|
|
@@ -24,7 +27,6 @@ func main() { | |
| if errors.Is(err, pflag.ErrHelp) { | ||
| os.Exit(0) | ||
| } | ||
|
|
||
| if err != nil { | ||
| fmt.Printf("couldn't configure flags: %s\n", err) | ||
| os.Exit(1) | ||
|
|
@@ -58,15 +60,63 @@ func main() { | |
| } | ||
|
|
||
| if term.IsTerminal(int(os.Stdout.Fd())) { | ||
| fmt.Println(app.Header) | ||
| fmt.Println(Header) | ||
| } | ||
|
|
||
| nodeApp, err := app.New(nodeConfig) | ||
| runner, err := node.NewRunner(nodeConfig) | ||
| if err != nil { | ||
| fmt.Printf("couldn't start node: %s\n", err) | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| exitCode := app.Run(nodeApp) | ||
| os.Exit(exitCode) | ||
| os.Exit(Run(runner)) | ||
| } | ||
|
|
||
| const Header = ` _____ .__ .__ | ||
| / _ \___ _______ | | _____ ____ ____ | |__ ____ ,_ o | ||
| / /_\ \ \/ /\__ \ | | \__ \ / \_/ ___\| | \_/ __ \ / //\, | ||
| / | \ / / __ \| |__/ __ \| | \ \___| Y \ ___/ \>> | | ||
| \____|__ /\_/ (____ /____(____ /___| /\___ >___| /\___ > \\ | ||
| \/ \/ \/ \/ \/ \/ \/` | ||
|
|
||
| func Run(app *node.Runner) int { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if we even need this function - we only use it once anyways. Should we inline this into main? |
||
| // start running the application | ||
| app.Start() | ||
|
|
||
| // register terminationSignals to kill the application | ||
| terminationSignals := make(chan os.Signal, 1) | ||
| signal.Notify(terminationSignals, syscall.SIGINT, syscall.SIGTERM) | ||
|
|
||
| stackTraceSignal := make(chan os.Signal, 1) | ||
| signal.Notify(stackTraceSignal, syscall.SIGABRT) | ||
|
|
||
| // start up a new go routine to handle attempts to kill the application | ||
| go func() { | ||
| for range terminationSignals { | ||
| app.Stop() | ||
| return | ||
| } | ||
| }() | ||
|
|
||
| // start a goroutine to listen on SIGABRT signals, | ||
| // to print the stack trace to standard error. | ||
| go func() { | ||
| for range stackTraceSignal { | ||
| fmt.Fprint(os.Stderr, utils.GetStacktrace(true)) | ||
| } | ||
| }() | ||
|
|
||
| // wait for the app to exit and get the exit code response | ||
| exitCode := app.ExitCode() | ||
|
|
||
| // shut down the termination signal go routine | ||
| signal.Stop(terminationSignals) | ||
| close(terminationSignals) | ||
|
|
||
| // shut down the stack trace go routine | ||
| signal.Stop(stackTraceSignal) | ||
| close(stackTraceSignal) | ||
|
|
||
| // return the exit code that the application reported | ||
| return exitCode | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,51 +1,30 @@ | ||
| // Copyright (C) 2019-2025, Ava Labs, Inc. All rights reserved. | ||
| // See the file LICENSE for licensing terms. | ||
|
|
||
| package app | ||
| package node | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "os/signal" | ||
| "sync" | ||
| "syscall" | ||
|
|
||
| "go.uber.org/zap" | ||
|
|
||
| "github.com/ava-labs/avalanchego/node" | ||
| "github.com/ava-labs/avalanchego/utils" | ||
| "github.com/ava-labs/avalanchego/utils/logging" | ||
| "github.com/ava-labs/avalanchego/utils/perms" | ||
| "github.com/ava-labs/avalanchego/utils/ulimit" | ||
|
|
||
| nodeconfig "github.com/ava-labs/avalanchego/config/node" | ||
| ) | ||
|
|
||
| const Header = ` _____ .__ .__ | ||
| / _ \___ _______ | | _____ ____ ____ | |__ ____ ,_ o | ||
| / /_\ \ \/ /\__ \ | | \__ \ / \_/ ___\| | \_/ __ \ / //\, | ||
| / | \ / / __ \| |__/ __ \| | \ \___| Y \ ___/ \>> | | ||
| \____|__ /\_/ (____ /____(____ /___| /\___ >___| /\___ > \\ | ||
| \/ \/ \/ \/ \/ \/ \/` | ||
|
|
||
| var _ App = (*app)(nil) | ||
|
|
||
| type App interface { | ||
| // Start kicks off the application and returns immediately. | ||
| // Start should only be called once. | ||
| Start() | ||
|
|
||
| // Stop notifies the application to exit and returns immediately. | ||
| // Stop should only be called after [Start]. | ||
| // It is safe to call Stop multiple times. | ||
| Stop() | ||
|
|
||
| // ExitCode should only be called after [Start] returns. It | ||
| // should block until the application finishes | ||
| ExitCode() int | ||
| // Runner is a wrapper around a *Node that runs in this process | ||
| type Runner struct { | ||
| node *Node | ||
| log logging.Logger | ||
| logFactory logging.Factory | ||
| exitWG sync.WaitGroup | ||
| } | ||
|
|
||
| func New(config nodeconfig.Config) (App, error) { | ||
| func NewRunner(config nodeconfig.Config) (*Runner, error) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The code in Taking a step back... I'm not sure why we needed the old |
||
| // Set the data directory permissions to be read write. | ||
| if err := perms.ChmodR(config.DatabaseConfig.Path, true, perms.ReadWriteExecute); err != nil { | ||
| return nil, fmt.Errorf("failed to restrict the permissions of the database directory with: %w", err) | ||
|
|
@@ -71,74 +50,24 @@ func New(config nodeconfig.Config) (App, error) { | |
| return nil, err | ||
| } | ||
|
|
||
| n, err := node.New(&config, logFactory, log) | ||
| n, err := New(&config, logFactory, log) | ||
|
||
| if err != nil { | ||
| log.Fatal("failed to initialize node", zap.Error(err)) | ||
| log.Stop() | ||
| logFactory.Close() | ||
| return nil, fmt.Errorf("failed to initialize node: %w", err) | ||
| } | ||
|
|
||
| return &app{ | ||
| return &Runner{ | ||
| node: n, | ||
| log: log, | ||
| logFactory: logFactory, | ||
| }, nil | ||
| } | ||
|
|
||
| func Run(app App) int { | ||
| // start running the application | ||
| app.Start() | ||
|
|
||
| // register terminationSignals to kill the application | ||
| terminationSignals := make(chan os.Signal, 1) | ||
| signal.Notify(terminationSignals, syscall.SIGINT, syscall.SIGTERM) | ||
|
|
||
| stackTraceSignal := make(chan os.Signal, 1) | ||
| signal.Notify(stackTraceSignal, syscall.SIGABRT) | ||
|
|
||
| // start up a new go routine to handle attempts to kill the application | ||
| go func() { | ||
| for range terminationSignals { | ||
| app.Stop() | ||
| return | ||
| } | ||
| }() | ||
|
|
||
| // start a goroutine to listen on SIGABRT signals, | ||
| // to print the stack trace to standard error. | ||
| go func() { | ||
| for range stackTraceSignal { | ||
| fmt.Fprint(os.Stderr, utils.GetStacktrace(true)) | ||
| } | ||
| }() | ||
|
|
||
| // wait for the app to exit and get the exit code response | ||
| exitCode := app.ExitCode() | ||
|
|
||
| // shut down the termination signal go routine | ||
| signal.Stop(terminationSignals) | ||
| close(terminationSignals) | ||
|
|
||
| // shut down the stack trace go routine | ||
| signal.Stop(stackTraceSignal) | ||
| close(stackTraceSignal) | ||
|
|
||
| // return the exit code that the application reported | ||
| return exitCode | ||
| } | ||
|
|
||
| // app is a wrapper around a node that runs in this process | ||
| type app struct { | ||
| node *node.Node | ||
| log logging.Logger | ||
| logFactory logging.Factory | ||
| exitWG sync.WaitGroup | ||
| } | ||
|
|
||
| // Start the business logic of the node (as opposed to config reading, etc). | ||
| // Does not block until the node is done. | ||
| func (a *app) Start() { | ||
| func (a *Runner) Start() { | ||
| // [p.ExitCode] will block until [p.exitWG.Done] is called | ||
| a.exitWG.Add(1) | ||
| go func() { | ||
|
|
@@ -166,13 +95,13 @@ func (a *app) Start() { | |
|
|
||
| // Stop attempts to shutdown the currently running node. This function will | ||
| // block until Shutdown returns. | ||
| func (a *app) Stop() { | ||
| func (a *Runner) Stop() { | ||
| a.node.Shutdown(0) | ||
| } | ||
|
|
||
| // ExitCode returns the exit code that the node is reporting. This function | ||
| // blocks until the node has been shut down. | ||
| func (a *app) ExitCode() int { | ||
| func (a *Runner) ExitCode() int { | ||
| a.exitWG.Wait() | ||
| return a.node.ExitCode() | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: typically
constslive at the top of the file