Skip to content

Commit

Permalink
feat(indexers): update release baseurl on update (#1532)
Browse files Browse the repository at this point in the history
  • Loading branch information
zze0s committed May 3, 2024
1 parent 19e129e commit 3202c60
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 9 deletions.
2 changes: 1 addition & 1 deletion cmd/autobrr/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func main() {
authService = auth.NewService(log, userService)
downloadClientService = download_client.NewService(log, downloadClientRepo)
actionService = action.NewService(log, actionRepo, downloadClientService, bus)
indexerService = indexer.NewService(log, cfg.Config, indexerRepo, indexerAPIService, schedulingService)
indexerService = indexer.NewService(log, cfg.Config, indexerRepo, releaseRepo, indexerAPIService, schedulingService)
filterService = filter.NewService(log, filterRepo, actionRepo, releaseRepo, indexerAPIService, indexerService)
releaseService = release.NewService(log, releaseRepo, actionService, filterService, indexerService)
ircService = irc.NewService(log, serverEvents, ircRepo, releaseService, indexerService, notificationService)
Expand Down
12 changes: 11 additions & 1 deletion internal/database/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,17 @@ func (r *IndexerRepo) Update(ctx context.Context, indexer domain.Indexer) (*doma
}

func (r *IndexerRepo) List(ctx context.Context) ([]domain.Indexer, error) {
rows, err := r.db.handler.QueryContext(ctx, "SELECT id, enabled, name, identifier, implementation, base_url, settings FROM indexer ORDER BY name ASC")
queryBuilder := r.db.squirrel.
Select("id", "enabled", "name", "identifier", "implementation", "base_url", "settings").
From("indexer").
OrderBy("name ASC")

query, _, err := queryBuilder.ToSql()
if err != nil {
return nil, errors.Wrap(err, "error building query")
}

rows, err := r.db.handler.QueryContext(ctx, query)
if err != nil {
return nil, errors.Wrap(err, "error executing query")
}
Expand Down
50 changes: 50 additions & 0 deletions internal/database/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -734,3 +734,53 @@ func (repo *ReleaseRepo) CanDownloadShow(ctx context.Context, title string, seas

return true, nil
}

func (repo *ReleaseRepo) UpdateBaseURL(ctx context.Context, indexer string, oldBaseURL, newBaseURL string) error {
tx, err := repo.db.BeginTx(ctx, &sql.TxOptions{})
if err != nil {
return err
}

defer func() {
var txErr error
if p := recover(); p != nil {
txErr = tx.Rollback()
if txErr != nil {
repo.log.Error().Err(txErr).Msg("error rolling back transaction")
}
repo.log.Error().Msgf("something went terribly wrong panic: %v", p)
} else if err != nil {
txErr = tx.Rollback()
if txErr != nil {
repo.log.Error().Err(txErr).Msg("error rolling back transaction")
}
} else {
// All good, commit
txErr = tx.Commit()
if txErr != nil {
repo.log.Error().Err(txErr).Msg("error committing transaction")
}
}
}()

queryBuilder := repo.db.squirrel.
RunWith(tx).
Update("release").
Set("download_url", sq.Expr("REPLACE(download_url, ?, ?)", oldBaseURL, newBaseURL)).
Set("info_url", sq.Expr("REPLACE(info_url, ?, ?)", oldBaseURL, newBaseURL)).
Where(sq.Eq{"indexer": indexer})

result, err := queryBuilder.ExecContext(ctx)
if err != nil {
return errors.Wrap(err, "error executing query")
}

rowsAffected, err := result.RowsAffected()
if err != nil {
return errors.Wrap(err, "error getting rows affected")
}

repo.log.Trace().Msgf("release updated (%d) base urls from %q to %q", rowsAffected, oldBaseURL, newBaseURL)

return nil
}
1 change: 1 addition & 0 deletions internal/domain/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type ReleaseRepo interface {
Stats(ctx context.Context) (*ReleaseStats, error)
Delete(ctx context.Context, req *DeleteReleaseRequest) error
CanDownloadShow(ctx context.Context, title string, season int, episode int) (bool, error)
UpdateBaseURL(ctx context.Context, indexer string, oldBaseURL, newBaseURL string) error

GetActionStatus(ctx context.Context, req *GetReleaseActionStatusRequest) (*ReleaseActionStatus, error)
StoreReleaseActionStatus(ctx context.Context, status *ReleaseActionStatus) error
Expand Down
38 changes: 31 additions & 7 deletions internal/indexer/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@ type Service interface {
}

type service struct {
log zerolog.Logger
config *domain.Config
repo domain.IndexerRepo
ApiService APIService
scheduler scheduler.Service
log zerolog.Logger
config *domain.Config
repo domain.IndexerRepo
releaseRepo domain.ReleaseRepo
ApiService APIService
scheduler scheduler.Service

// contains all raw indexer definitions
definitions map[string]domain.IndexerDefinition
Expand All @@ -63,11 +64,12 @@ type service struct {
rssIndexers map[string]*domain.IndexerDefinition
}

func NewService(log logger.Logger, config *domain.Config, repo domain.IndexerRepo, apiService APIService, scheduler scheduler.Service) Service {
func NewService(log logger.Logger, config *domain.Config, repo domain.IndexerRepo, releaseRepo domain.ReleaseRepo, apiService APIService, scheduler scheduler.Service) Service {
return &service{
log: log.With().Str("module", "indexer").Logger(),
config: config,
repo: repo,
releaseRepo: releaseRepo,
ApiService: apiService,
scheduler: scheduler,
lookupIRCServerDefinition: make(map[string]map[string]*domain.IndexerDefinition),
Expand Down Expand Up @@ -119,6 +121,28 @@ func (s *service) Update(ctx context.Context, indexer domain.Indexer) (*domain.I
indexer.Settings[key] = sanitize.String(val)
}

currentIndexer, err := s.repo.FindByID(ctx, int(indexer.ID))
if err != nil {
return nil, errors.Wrap(err, "could not find indexer by id: %v", indexer.ID)
}

// only IRC indexers have baseURL set
if indexer.Implementation == string(domain.IndexerImplementationIRC) {
if indexer.BaseURL == "" {
return nil, errors.New("indexer baseURL must not be empty")
}

// check if baseURL has been updated and update releases if it was
if currentIndexer.BaseURL != indexer.BaseURL {

// update urls of releases
err = s.releaseRepo.UpdateBaseURL(ctx, indexer.Identifier, currentIndexer.BaseURL, indexer.BaseURL)
if err != nil {
return nil, errors.Wrap(err, "could not update release urls with new baseURL: %s", indexer.BaseURL)
}
}
}

i, err := s.repo.Update(ctx, indexer)
if err != nil {
s.log.Error().Err(err).Msgf("could not update indexer: %+v", indexer)
Expand All @@ -132,7 +156,7 @@ func (s *service) Update(ctx context.Context, indexer domain.Indexer) (*domain.I
}

if isImplFeed(indexer.Implementation) {
if !indexer.Enabled {
if currentIndexer.Enabled && !indexer.Enabled {
s.stopFeed(indexer.Identifier)
}
}
Expand Down

0 comments on commit 3202c60

Please sign in to comment.