Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup improvements #153

Merged
merged 1 commit into from
Jun 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions cmd/podsync/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,33 @@ func (u *Updater) updateFeed(ctx context.Context, feedConfig *config.Feed) error

log.Debugf("received %d episode(s) for %q", len(result.Episodes), result.Title)

episodeSet := make(map[string]struct{})
if err := u.db.WalkEpisodes(ctx, feedConfig.ID, func(episode *model.Episode) error {
if episode.Status != model.EpisodeDownloaded && episode.Status != model.EpisodeCleaned {
episodeSet[episode.ID] = struct{}{}
}
return nil
}); err != nil {
return err
}

if err := u.db.AddFeed(ctx, feedConfig.ID, result); err != nil {
return err
}

for _, episode := range result.Episodes {
delete(episodeSet, episode.ID)
}

// removing episodes that are no longer available in the feed and not downloaded or cleaned
for id := range episodeSet {
log.Infof("removing episode %q", id)
err := u.db.DeleteEpisode(feedConfig.ID, id)
if err != nil {
return err
}
}

log.Debug("successfully saved updates to storage")
return nil
}
Expand Down Expand Up @@ -372,6 +395,8 @@ func (u *Updater) cleanup(ctx context.Context, feedConfig *config.Feed) error {

if err := u.db.UpdateEpisode(feedID, episode.ID, func(episode *model.Episode) error {
episode.Status = model.EpisodeCleaned
episode.Title = ""
episode.Description = ""
return nil
}); err != nil {
result = multierror.Append(result, errors.Wrapf(err, "failed to set state for cleaned episode: %s", episode.ID))
Expand Down
7 changes: 7 additions & 0 deletions pkg/db/badger.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,13 @@ func (b *Badger) UpdateEpisode(feedID string, episodeID string, cb func(episode
})
}

func (b *Badger) DeleteEpisode(feedID, episodeID string) error {
key := b.getKey(episodePath, feedID, episodeID)
return b.db.Update(func(txn *badger.Txn) error {
return txn.Delete(key)
})
}

func (b *Badger) WalkEpisodes(ctx context.Context, feedID string, cb func(episode *model.Episode) error) error {
return b.db.View(func(txn *badger.Txn) error {
return b.walkEpisodes(txn, feedID, cb)
Expand Down
3 changes: 3 additions & 0 deletions pkg/db/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ type Storage interface {
// UpdateEpisode updates episode fields
UpdateEpisode(feedID string, episodeID string, cb func(episode *model.Episode) error) error

// DeleteEpisode deletes an episode
DeleteEpisode(feedID string, episodeID string) error

// WalkEpisodes iterates over episodes that belong to the given feed ID
WalkEpisodes(ctx context.Context, feedID string, cb func(episode *model.Episode) error) error
}