Skip to content

Commit 948e0a0

Browse files
authored
refact cmd/crowdsec: extract functions from runCrowdsec() (#3669)
1 parent e35c44e commit 948e0a0

File tree

1 file changed

+34
-10
lines changed

1 file changed

+34
-10
lines changed

cmd/crowdsec/crowdsec.go

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,7 @@ func initCrowdsec(cConfig *csconfig.Config, hub *cwhub.Hub, testMode bool) (*par
7070
return csParsers, datasources, nil
7171
}
7272

73-
// runCrowdsec starts the log processor service
74-
func runCrowdsec(cConfig *csconfig.Config, parsers *parser.Parsers, hub *cwhub.Hub, datasources []acquisition.DataSource) error {
75-
inputEventChan = make(chan types.Event)
76-
inputLineChan = make(chan types.Event)
77-
73+
func startParserRoutines(cConfig *csconfig.Config, parsers *parser.Parsers) {
7874
// start go-routines for parsing, buckets pour and outputs.
7975
parserWg := &sync.WaitGroup{}
8076

@@ -99,7 +95,9 @@ func runCrowdsec(cConfig *csconfig.Config, parsers *parser.Parsers, hub *cwhub.H
9995
return nil
10096
})
10197
parserWg.Wait()
98+
}
10299

100+
func startBucketRoutines(cConfig *csconfig.Config) {
103101
bucketWg := &sync.WaitGroup{}
104102

105103
bucketsTomb.Go(func() error {
@@ -126,15 +124,14 @@ func runCrowdsec(cConfig *csconfig.Config, parsers *parser.Parsers, hub *cwhub.H
126124
return nil
127125
})
128126
bucketWg.Wait()
127+
}
129128

130-
apiClient, err := apiclient.GetLAPIClient()
131-
if err != nil {
132-
return err
133-
}
134-
129+
func startHeartBeat(cConfig *csconfig.Config, apiClient *apiclient.ApiClient) {
135130
log.Debugf("Starting HeartBeat service")
136131
apiClient.HeartBeat.StartHeartBeat(context.Background(), &outputsTomb)
132+
}
137133

134+
func startOutputRoutines(cConfig *csconfig.Config, parsers *parser.Parsers, apiClient *apiclient.ApiClient) {
138135
outputWg := &sync.WaitGroup{}
139136

140137
outputsTomb.Go(func() error {
@@ -153,7 +150,9 @@ func runCrowdsec(cConfig *csconfig.Config, parsers *parser.Parsers, hub *cwhub.H
153150
return nil
154151
})
155152
outputWg.Wait()
153+
}
156154

155+
func startLPMetrics(cConfig *csconfig.Config, apiClient *apiclient.ApiClient, hub *cwhub.Hub, datasources []acquisition.DataSource) error {
157156
mp := NewMetricsProvider(
158157
apiClient,
159158
lpMetricsDefaultInterval,
@@ -178,6 +177,31 @@ func runCrowdsec(cConfig *csconfig.Config, parsers *parser.Parsers, hub *cwhub.H
178177
}
179178
}
180179

180+
return nil
181+
}
182+
183+
// runCrowdsec starts the log processor service
184+
func runCrowdsec(cConfig *csconfig.Config, parsers *parser.Parsers, hub *cwhub.Hub, datasources []acquisition.DataSource) error {
185+
inputEventChan = make(chan types.Event)
186+
inputLineChan = make(chan types.Event)
187+
188+
startParserRoutines(cConfig, parsers)
189+
190+
startBucketRoutines(cConfig)
191+
192+
apiClient, err := apiclient.GetLAPIClient()
193+
if err != nil {
194+
return err
195+
}
196+
197+
startHeartBeat(cConfig, apiClient)
198+
199+
startOutputRoutines(cConfig, parsers, apiClient)
200+
201+
if err := startLPMetrics(cConfig, apiClient, hub, datasources); err != nil {
202+
return err
203+
}
204+
181205
log.Info("Starting processing data")
182206

183207
if err := acquisition.StartAcquisition(context.TODO(), dataSources, inputLineChan, &acquisTomb); err != nil {

0 commit comments

Comments
 (0)