Refactor analyzer to use a shared log channel

This commit is contained in:
2026-01-05 22:27:45 +05:00
parent 1f8be77ab3
commit ba23474eab

View File

@@ -22,6 +22,8 @@ type analyzer struct {
notify notifications.Notifications
systemd analyzerLog.Systemd
analysis analyzerLog.Analysis
logChan chan analysisServices.Entry
}
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,
systemd: systemdService,
analysis: analysisService,
logChan: make(chan analysisServices.Entry, 1000),
}
}
func (a *analyzer) Run(ctx context.Context) {
logChan := make(chan analysisServices.Entry, 1000)
go a.systemd.Run(ctx, logChan)
go a.processLogs(ctx, logChan)
go a.systemd.Run(ctx, a.logChan)
go a.processLogs(ctx)
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 {
select {
case <-ctx.Done():
return
case entry := <-logChan:
case entry := <-a.logChan:
a.logger.Debug(fmt.Sprintf("Received log entry: %s", entry))
switch entry.Unit {
case "ssh.service":
@@ -74,6 +76,7 @@ func (a *analyzer) Close() error {
if err := a.systemd.Close(); err != nil {
return err
}
close(a.logChan)
a.logger.Debug("Analyzer is stop")