Refactor analyzer to use a shared log channel
This commit is contained in:
@@ -22,6 +22,8 @@ type analyzer struct {
|
|||||||
notify notifications.Notifications
|
notify notifications.Notifications
|
||||||
systemd analyzerLog.Systemd
|
systemd analyzerLog.Systemd
|
||||||
analysis analyzerLog.Analysis
|
analysis analyzerLog.Analysis
|
||||||
|
|
||||||
|
logChan chan analysisServices.Entry
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(config config2.Config, logger log.Logger, notify notifications.Notifications) Analyzer {
|
func New(config config2.Config, logger log.Logger, notify notifications.Notifications) Analyzer {
|
||||||
@@ -39,23 +41,23 @@ func New(config config2.Config, logger log.Logger, notify notifications.Notifica
|
|||||||
notify: notify,
|
notify: notify,
|
||||||
systemd: systemdService,
|
systemd: systemdService,
|
||||||
analysis: analysisService,
|
analysis: analysisService,
|
||||||
|
|
||||||
|
logChan: make(chan analysisServices.Entry, 1000),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *analyzer) Run(ctx context.Context) {
|
func (a *analyzer) Run(ctx context.Context) {
|
||||||
logChan := make(chan analysisServices.Entry, 1000)
|
go a.systemd.Run(ctx, a.logChan)
|
||||||
|
go a.processLogs(ctx)
|
||||||
go a.systemd.Run(ctx, logChan)
|
|
||||||
go a.processLogs(ctx, logChan)
|
|
||||||
a.logger.Debug("Analyzer is start")
|
a.logger.Debug("Analyzer is start")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *analyzer) processLogs(ctx context.Context, logChan <-chan analysisServices.Entry) {
|
func (a *analyzer) processLogs(ctx context.Context) {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return
|
return
|
||||||
case entry := <-logChan:
|
case entry := <-a.logChan:
|
||||||
a.logger.Debug(fmt.Sprintf("Received log entry: %s", entry))
|
a.logger.Debug(fmt.Sprintf("Received log entry: %s", entry))
|
||||||
switch entry.Unit {
|
switch entry.Unit {
|
||||||
case "ssh.service":
|
case "ssh.service":
|
||||||
@@ -74,6 +76,7 @@ func (a *analyzer) Close() error {
|
|||||||
if err := a.systemd.Close(); err != nil {
|
if err := a.systemd.Close(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
close(a.logChan)
|
||||||
|
|
||||||
a.logger.Debug("Analyzer is stop")
|
a.logger.Debug("Analyzer is stop")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user