forked from lightninglabs/pool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.go
76 lines (66 loc) · 1.7 KB
/
run.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
package pool
import (
"fmt"
"net/http"
"os"
"path/filepath"
"github.com/lightningnetwork/lnd/build"
"github.com/lightningnetwork/lnd/signal"
)
// Run starts the trader daemon and blocks until it's shut down again.
func Run(cfg *Config) error {
// Hook interceptor for os signals.
var err error
cfg.ShutdownInterceptor, err = signal.Intercept()
if err != nil {
return err
}
cfg.RequestShutdown = cfg.ShutdownInterceptor.RequestShutdown
logWriter = build.NewRotatingLogWriter()
SetupLoggers(logWriter, cfg.ShutdownInterceptor)
// Special show command to list supported subsystems and exit.
if cfg.DebugLevel == "show" {
fmt.Printf("Supported subsystems: %v\n",
logWriter.SupportedSubsystems())
os.Exit(0)
}
// Initialize logging at the default logging level.
err = logWriter.InitLogRotator(
filepath.Join(cfg.LogDir, DefaultLogFilename),
cfg.MaxLogFileSize, cfg.MaxLogFiles,
)
if err != nil {
return err
}
err = build.ParseAndSetDebugLevels(cfg.DebugLevel, logWriter)
if err != nil {
return err
}
if cfg.Profile != "" {
go func() {
log.Infof("Pprof listening on %v", cfg.Profile)
profileRedirect := http.RedirectHandler(
"/debug/pprof", http.StatusSeeOther,
)
http.Handle("/", profileRedirect)
//nolint:gosec
err := http.ListenAndServe(cfg.Profile, nil)
if err != nil {
log.Errorf("Unable to run profiler: %v", err)
}
}()
}
trader := NewServer(cfg)
err = trader.Start()
if err != nil {
log.Errorf("Error starting server: %v", err)
return fmt.Errorf("unable to start server: %v", err)
}
<-cfg.ShutdownInterceptor.ShutdownChannel()
err = trader.Stop()
if err != nil {
log.Errorf("Error stopping server: %v", err)
return err
}
return nil
}