-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.go
136 lines (118 loc) · 3.62 KB
/
main.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
package main
import (
"allora_offchain_node/lib"
usecase "allora_offchain_node/usecase"
"context"
"encoding/json"
"fmt"
"os"
"os/signal"
"syscall"
"github.com/joho/godotenv"
"github.com/rs/zerolog/log"
)
func ConvertEntrypointsToInstances(userConfig lib.UserConfig) error {
/// Initialize adapters using the factory function
for i, worker := range userConfig.Worker {
if worker.InferenceEntrypointName != "" {
adapter, err := NewAlloraAdapter(worker.InferenceEntrypointName)
if err != nil {
fmt.Println("Error creating inference adapter:", err)
return err
}
userConfig.Worker[i].InferenceEntrypoint = adapter
}
if worker.ForecastEntrypointName != "" {
adapter, err := NewAlloraAdapter(worker.ForecastEntrypointName)
if err != nil {
fmt.Println("Error creating forecast adapter:", err)
return err
}
userConfig.Worker[i].ForecastEntrypoint = adapter
}
}
for i, reputer := range userConfig.Reputer {
if reputer.GroundTruthEntrypointName != "" {
adapter, err := NewAlloraAdapter(reputer.GroundTruthEntrypointName)
if err != nil {
fmt.Println("Error creating reputer adapter:", err)
return err
}
userConfig.Reputer[i].GroundTruthEntrypoint = adapter
}
}
for i, reputer := range userConfig.Reputer {
if reputer.LossFunctionEntrypointName != "" {
adapter, err := NewAlloraAdapter(reputer.LossFunctionEntrypointName)
if err != nil {
fmt.Println("Error creating reputer adapter:", err)
return err
}
userConfig.Reputer[i].LossFunctionEntrypoint = adapter
}
}
return nil
}
func main() {
initLogger()
if dotErr := godotenv.Load(); dotErr != nil {
log.Info().Msg("Unable to load .env file")
}
log.Info().Msg("Starting allora offchain node...")
metrics := lib.NewMetrics(lib.CounterData)
metrics.RegisterMetricsCounters()
metrics.StartMetricsServer(":2112")
finalUserConfig := lib.UserConfig{} // nolint: exhaustruct
alloraJsonConfig := os.Getenv(lib.ALLORA_OFFCHAIN_NODE_CONFIG_JSON)
if alloraJsonConfig != "" {
log.Info().Msg("Config using JSON env var")
// completely reset UserConfig
err := json.Unmarshal([]byte(alloraJsonConfig), &finalUserConfig)
if err != nil {
log.Fatal().Err(err).Msg("Failed to parse JSON config file from Config")
return
}
} else if os.Getenv(lib.ALLORA_OFFCHAIN_NODE_CONFIG_FILE_PATH) != "" {
log.Info().Msg("Config using JSON config file")
// parse file defined in CONFIG_FILE_PATH into UserConfig
file, err := os.Open(os.Getenv(lib.ALLORA_OFFCHAIN_NODE_CONFIG_FILE_PATH))
if err != nil {
log.Fatal().Err(err).Msg("Failed to open JSON config file")
return
}
defer file.Close()
decoder := json.NewDecoder(file)
// completely reset UserConfig
err = decoder.Decode(&finalUserConfig)
if err != nil {
log.Fatal().Err(err).Msg("Failed to parse JSON config file")
return
}
} else {
log.Fatal().Msg("Could not find config file. Please create a config.json file and pass as environment variable.")
return
}
// Convert entrypoints to instances of adapters
err := ConvertEntrypointsToInstances(finalUserConfig)
if err != nil {
log.Fatal().Err(err).Msg("Failed to convert Entrypoints to instances of adapters")
return
}
spawner, err := usecase.NewUseCaseSuite(finalUserConfig)
if err != nil {
log.Fatal().Err(err).Msg("Failed to initialize use case, exiting")
return
}
spawner.Metrics = *metrics
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
sigCtx, sigCancel := signal.NotifyContext(ctx, syscall.SIGINT, syscall.SIGTERM)
defer sigCancel()
go func() {
spawner.Spawn(sigCtx)
cancel()
}()
<-sigCtx.Done()
log.Info().Msg("Stopping...")
<-ctx.Done()
}