-
Notifications
You must be signed in to change notification settings - Fork 2
/
start.go
124 lines (99 loc) · 3.74 KB
/
start.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
// Copyright 2021 dfuse Platform Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cli
import (
"fmt"
"path/filepath"
"strings"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/streamingfast/bstream"
"github.com/streamingfast/derr"
"github.com/streamingfast/dlauncher/launcher"
"go.uber.org/zap"
)
var StartCmd = &cobra.Command{Use: "start", Short: "Starts `firearweave` services all at once", RunE: firehoseCmdStartE, Args: cobra.ArbitraryArgs}
func init() {
RootCmd.AddCommand(StartCmd)
}
func firehoseCmdStartE(cmd *cobra.Command, args []string) (err error) {
cmd.SilenceUsage = true
dataDir := viper.GetString("global-data-dir")
rootLog.Debug("firearweave binary started", zap.String("data_dir", dataDir))
configFile := viper.GetString("global-config-file")
rootLog.Info(fmt.Sprintf("starting Firehose on Arweave with config file '%s'", configFile))
err = Start(dataDir, args)
if err != nil {
return fmt.Errorf("unable to launch: %w", err)
}
// If an error occurred, saying Goodbye is not greate
rootLog.Info("goodbye")
return
}
func Start(dataDir string, args []string) (err error) {
dataDirAbs, err := filepath.Abs(dataDir)
if err != nil {
return fmt.Errorf("unable to setup directory structure: %w", err)
}
err = makeDirs([]string{dataDirAbs})
if err != nil {
return err
}
tracker := bstream.NewTracker(50)
tracker.AddResolver(bstream.OffsetStartBlockResolver(200))
modules := &launcher.Runtime{
AbsDataDir: dataDirAbs,
Tracker: tracker,
}
atmCacheEnabled := viper.GetBool("common-atm-cache-enabled")
if atmCacheEnabled {
bstream.GetBlockPayloadSetter = bstream.ATMCachedPayloadSetter
cacheDir := MustReplaceDataDir(modules.AbsDataDir, viper.GetString("common-atm-cache-dir"))
storeUrl := MustReplaceDataDir(modules.AbsDataDir, viper.GetString("common-blocks-store-url"))
maxRecentEntryBytes := viper.GetInt("common-atm-max-recent-entry-bytes")
maxEntryByAgeBytes := viper.GetInt("common-atm-max-entry-by-age-bytes")
bstream.InitCache(storeUrl, cacheDir, maxRecentEntryBytes, maxEntryByAgeBytes)
}
bstream.GetProtocolFirstStreamableBlock = uint64(viper.GetInt("common-first-streamable-block"))
err = bstream.ValidateRegistry()
if err != nil {
return fmt.Errorf("protocol specific hooks not configured correctly: %w", err)
}
launch := launcher.NewLauncher(rootLog, modules)
rootLog.Debug("launcher created")
runByDefault := func(app string) bool { return true }
apps := launcher.ParseAppsFromArgs(args, runByDefault)
if len(args) == 0 {
apps = launcher.ParseAppsFromArgs(launcher.Config["start"].Args, runByDefault)
}
rootLog.Info(fmt.Sprintf("launching applications: %s", strings.Join(apps, ",")))
if err = launch.Launch(apps); err != nil {
return err
}
signalHandler := derr.SetupSignalHandler(viper.GetDuration("common-system-shutdown-signal-delay"))
select {
case <-signalHandler:
rootLog.Info("received termination signal, quitting")
go launch.Close()
case appID := <-launch.Terminating():
if launch.Err() == nil {
rootLog.Info(fmt.Sprintf("application %s triggered a clean shutdown, quitting", appID))
} else {
rootLog.Info(fmt.Sprintf("application %s shutdown unexpectedly, quitting", appID))
err = launch.Err()
}
}
launch.WaitForTermination()
return
}