-
Notifications
You must be signed in to change notification settings - Fork 666
/
app.go
172 lines (146 loc) · 4.53 KB
/
app.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package app
import (
"fmt"
"os"
"os/signal"
"sync"
"syscall"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"
"github.com/ava-labs/avalanchego/node"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/utils/perms"
"github.com/ava-labs/avalanchego/utils/ulimit"
)
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() error
// 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() error
// ExitCode should only be called after [Start] returns with no error. It
// should block until the application finishes
ExitCode() (int, error)
}
func New(config node.Config) (App, error) {
// 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)
}
if err := perms.ChmodR(config.LoggingConfig.Directory, true, perms.ReadWriteExecute); err != nil {
return nil, fmt.Errorf("failed to restrict the permissions of the log directory with: %w", err)
}
logFactory := logging.NewFactory(config.LoggingConfig)
log, err := logFactory.Make("main")
if err != nil {
logFactory.Close()
return nil, fmt.Errorf("failed to initialize log: %w", err)
}
// update fd limit
fdLimit := config.FdLimit
if err := ulimit.Set(fdLimit, log); err != nil {
log.Fatal("failed to set fd-limit",
zap.Error(err),
)
logFactory.Close()
return nil, err
}
n, err := node.New(&config, logFactory, log)
if err != nil {
log.Stop()
logFactory.Close()
return nil, fmt.Errorf("failed to initialize node: %w", err)
}
return &app{
node: n,
log: log,
logFactory: logFactory,
}, nil
}
func Run(app App) int {
// start running the application
if err := app.Start(); err != nil {
return 1
}
// register signals to kill the application
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGINT)
signal.Notify(signals, syscall.SIGTERM)
// start up a new go routine to handle attempts to kill the application
var eg errgroup.Group
eg.Go(func() error {
for range signals {
return app.Stop()
}
return nil
})
// wait for the app to exit and get the exit code response
exitCode, err := app.ExitCode()
// shut down the signal go routine
signal.Stop(signals)
close(signals)
// if there was an error closing or running the application, report that error
if eg.Wait() != nil || err != nil {
return 1
}
// 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. Errors returned from this method
// are not logged.
func (a *app) Start() error {
// [p.ExitCode] will block until [p.exitWG.Done] is called
a.exitWG.Add(1)
go func() {
defer func() {
if r := recover(); r != nil {
fmt.Println("caught panic", r)
}
a.log.Stop()
a.logFactory.Close()
a.exitWG.Done()
}()
defer func() {
// If [p.node.Dispatch()] panics, then we should log the panic and
// then re-raise the panic. This is why the above defer is broken
// into two parts.
a.log.StopOnPanic()
}()
err := a.node.Dispatch()
a.log.Debug("dispatch returned",
zap.Error(err),
)
}()
return nil
}
// Stop attempts to shutdown the currently running node. This function will
// return immediately.
func (a *app) Stop() error {
a.node.Shutdown(0)
return nil
}
// 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, error) {
a.exitWG.Wait()
return a.node.ExitCode(), nil
}