WIP
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
|
||||
"github.com/fujin/music-agregator/internal/config"
|
||||
"github.com/fujin/music-agregator/internal/indexer"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
type IndexerService struct {
|
||||
@@ -31,7 +32,25 @@ func NewIndexerService(configs []config.IndexerConfig) (*IndexerService, error)
|
||||
indexers = append(indexers, idx)
|
||||
}
|
||||
|
||||
return &IndexerService{indexers: indexers}, nil
|
||||
svc := &IndexerService{indexers: indexers}
|
||||
svc.checkHealth(context.Background())
|
||||
|
||||
return svc, nil
|
||||
}
|
||||
|
||||
func (s *IndexerService) checkHealth(ctx context.Context) {
|
||||
for _, idx := range s.indexers {
|
||||
if err := idx.TestConnection(ctx); err != nil {
|
||||
log.Warn().
|
||||
Str("indexer", idx.Name()).
|
||||
Err(err).
|
||||
Msg("[INDEXER] failed to connect to indexer")
|
||||
} else {
|
||||
log.Info().
|
||||
Str("indexer", idx.Name()).
|
||||
Msg("[INDEXER] connected successfully")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func buildTorznabURL(cfg config.IndexerConfig) string {
|
||||
@@ -54,18 +73,37 @@ func buildTorznabURL(cfg config.IndexerConfig) string {
|
||||
func (s *IndexerService) Search(ctx context.Context, criteria *indexer.MusicSearchCriteria, indexerName *string) ([]indexer.SearchResult, error) {
|
||||
var results []indexer.SearchResult
|
||||
|
||||
log.Info().
|
||||
Str("artist", criteria.Artist).
|
||||
Interface("album", criteria.Album).
|
||||
Interface("year", criteria.Year).
|
||||
Msg("[INDEXER] searching indexers")
|
||||
|
||||
for _, idx := range s.indexers {
|
||||
if indexerName != nil && idx.Name() != *indexerName {
|
||||
continue
|
||||
}
|
||||
|
||||
log.Debug().Str("indexer", idx.Name()).Msg("[INDEXER] querying indexer")
|
||||
|
||||
r, err := idx.Search(ctx, criteria)
|
||||
if err != nil {
|
||||
log.Warn().
|
||||
Str("indexer", idx.Name()).
|
||||
Err(err).
|
||||
Msg("[INDEXER] search failed")
|
||||
continue
|
||||
}
|
||||
|
||||
log.Info().
|
||||
Str("indexer", idx.Name()).
|
||||
Int("results", len(r)).
|
||||
Msg("[INDEXER] search completed")
|
||||
|
||||
results = append(results, r...)
|
||||
}
|
||||
|
||||
log.Info().Int("total_results", len(results)).Msg("[INDEXER] search finished")
|
||||
return results, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user