Skip to content
This repository has been archived by the owner on Dec 26, 2024. It is now read-only.

Commit

Permalink
feat: unify config and fix concurrent use (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
zze0s authored Sep 5, 2022
1 parent 1fbeddb commit 94a6a82
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 45 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ clients:
autobrr:
host: http://localhost:7474
apikey: YOUR_API_KEY
radarr:
arr:
- name: radarr
type: radarr
host: https://yourdomain.com/radarr
apikey: YOUR_API_KEY
filters:
- 15
sonarr:

- name: sonarr
type: sonarr
# host: http://localhost:PORT
# host: http://sonarr
host: https://yourdomain.com/sonarr
Expand All @@ -40,8 +42,9 @@ clients:
If you're trying to reach radarr or sonarr hosted on swizzin from some other location, you need to do it like this with basic auth:
```yaml
radarr:
arr:
- name: radarr
type: radarr
host: https://domain.com/radarr
apikey: YOUR_API_KEY
basicAuth:
Expand Down
6 changes: 4 additions & 2 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,21 @@ clients:
autobrr:
host: http://localhost:7474
apikey: API_KEY
radarr:
arr:
- name: radarr
type: radarr
host: http://localhost:7878
apikey: API_KEY
filters:
- 15
- name: radarr4k
type: radarr
host: http://localhost:7878
apikey: API_KEY
filters:
- 16
sonarr:
- name: sonarr
type: sonarr
host: http://localhost:8989
apikey: API_KEY
filters:
Expand Down
33 changes: 22 additions & 11 deletions internal/domain/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,20 @@ type BasicAuth struct {

type ArrConfig struct {
Name string `koanf:"name"`
Type ArrType `koanf:"type"`
Host string `koanf:"host"`
Apikey string `koanf:"apikey"`
BasicAuth *BasicAuth `koanf:"basicAuth"`
Filters []int `koanf:"filters"`
}

type ArrType string

var (
ArrTypeRadarr ArrType = "radarr"
ArrTypeSonarr ArrType = "sonarr"
)

type AutobrrConfig struct {
Host string `koanf:"host"`
Apikey string `koanf:"apikey"`
Expand All @@ -43,8 +51,7 @@ type Config struct {
Schedule string `koanf:"schedule"`
Clients struct {
Autobrr *AutobrrConfig `koanf:"autobrr"`
Radarr []*ArrConfig `koanf:"radarr"`
Sonarr []*ArrConfig `koanf:"sonarr"`
Arr []*ArrConfig `koanf:"arr"`
} `koanf:"clients"`
}

Expand All @@ -55,8 +62,7 @@ func (c *Config) defaults() {
c.Schedule = "0 */6 * * *"

c.Clients.Autobrr = nil
c.Clients.Sonarr = nil
c.Clients.Radarr = nil
c.Clients.Arr = nil
}

var k = koanf.New(".")
Expand Down Expand Up @@ -152,19 +158,24 @@ clients:
autobrr:
host: http://localhost:7474
apikey: API_KEY
radarr:
arr:
- name: radarr
type: radarr
host: http://localhost:7878
apikey: API_KEY
filters:
- 15
#- name: radarr4k
# host: http://localhost:7878
# apikey: API_KEY
# filters:
# - 16
sonarr:
- name: radarr4k
type: radarr
host: http://localhost:7878
apikey: API_KEY
filters:
- 16
- name: sonarr
type: sonarr
host: http://localhost:8989
apikey: API_KEY
filters:
Expand Down
51 changes: 22 additions & 29 deletions internal/processor/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ func NewService(cfg *domain.Config) *Service {
}

func (s Service) Process(dryRun bool) error {
ctx := context.TODO()

if s.cfg.Clients.Autobrr == nil {
log.Fatal().Msg("must supply autobrr configuration!")
return errors.New("must supply autobrr configuration")
Expand All @@ -41,29 +39,24 @@ func (s Service) Process(dryRun bool) error {

start := time.Now()

var g errgroup.Group

if s.cfg.Clients.Radarr != nil {
for _, arrClient := range s.cfg.Clients.Radarr {
g.Go(func() error {
if err := s.radarr(ctx, arrClient, dryRun, a); err != nil {
log.Error().Err(err).Msgf("radarr: %v something went wrong", arrClient.Name)
return err
}
return nil
})
}
}
g, ctx := errgroup.WithContext(context.Background())

if s.cfg.Clients.Arr != nil {
for _, arrClient := range s.cfg.Clients.Arr {
// https://golang.org/doc/faq#closures_and_goroutines
arrClient := arrClient

if s.cfg.Clients.Sonarr != nil {
for _, arrClient := range s.cfg.Clients.Sonarr {
g.Go(func() error {
if err := s.sonarr(ctx, arrClient, dryRun, a); err != nil {
log.Error().Err(err).Msgf("sonarr: %v something went wrong", arrClient.Name)
return err
}
return nil
})
switch arrClient.Type {
case domain.ArrTypeRadarr:
g.Go(func() error {
return s.radarr(ctx, arrClient, dryRun, a)
})

case domain.ArrTypeSonarr:
g.Go(func() error {
return s.sonarr(ctx, arrClient, dryRun, a)
})
}
}
}

Expand All @@ -78,11 +71,11 @@ func (s Service) Process(dryRun bool) error {
}

func (s Service) radarr(ctx context.Context, cfg *domain.ArrConfig, dryRun bool, brr *autobrr.Client) error {
l := log.With().Str("type", "sonarr").Str("client", cfg.Name).Logger()
l := log.With().Str("type", "radarr").Str("client", cfg.Name).Logger()

l.Debug().Msgf("gathering titles...")

movieTitles, err := s.processRadarr(cfg, l)
movieTitles, err := s.processRadarr(cfg, &l)
if err != nil {
return err
}
Expand Down Expand Up @@ -116,7 +109,7 @@ func (s Service) radarr(ctx context.Context, cfg *domain.ArrConfig, dryRun bool,
return nil
}

func (s Service) processRadarr(cfg *domain.ArrConfig, logger zerolog.Logger) ([]string, error) {
func (s Service) processRadarr(cfg *domain.ArrConfig, logger *zerolog.Logger) ([]string, error) {
c := starr.New(cfg.Apikey, cfg.Host, 0)

if cfg.BasicAuth != nil {
Expand Down Expand Up @@ -178,7 +171,7 @@ func (s Service) sonarr(ctx context.Context, cfg *domain.ArrConfig, dryRun bool,

l.Debug().Msgf("gathering titles...")

movieTitles, err := s.processSonarr(cfg, l)
movieTitles, err := s.processSonarr(cfg, &l)
if err != nil {
return err
}
Expand Down Expand Up @@ -212,7 +205,7 @@ func (s Service) sonarr(ctx context.Context, cfg *domain.ArrConfig, dryRun bool,
return nil
}

func (s Service) processSonarr(cfg *domain.ArrConfig, logger zerolog.Logger) ([]string, error) {
func (s Service) processSonarr(cfg *domain.ArrConfig, logger *zerolog.Logger) ([]string, error) {
c := starr.New(cfg.Apikey, cfg.Host, 0)

if cfg.BasicAuth != nil {
Expand Down

0 comments on commit 94a6a82

Please sign in to comment.