qbittorrent cleanup

This commit is contained in:
royalcat 2024-12-16 00:43:04 +03:00
parent 109bbb202d
commit ee2fc5ab9d
8 changed files with 459 additions and 12 deletions
daemons/qbittorrent

View file

@ -7,6 +7,7 @@ import (
"log/slog"
"os"
"path"
"slices"
"git.kmsign.ru/royalcat/tstor/pkg/qbittorrent"
"git.kmsign.ru/royalcat/tstor/pkg/rlog"
@ -15,6 +16,59 @@ import (
func (d *Daemon) Cleanup(ctx context.Context, run bool) ([]string, error) {
d.log.Info(ctx, "cleanup started")
torrentInfos, err := d.client.qb.Torrent().GetTorrents(ctx, &qbittorrent.TorrentOption{})
if err != nil {
d.log.Error(ctx, "failed to get torrents", rlog.Error(err))
return nil, fmt.Errorf("failed to get torrents: %w", err)
}
daemonsHashes := []string{}
for _, info := range torrentInfos {
daemonsHashes = append(daemonsHashes, info.Hash)
}
dataDirs, err := os.ReadDir(d.dataDir)
if err != nil {
d.log.Error(ctx, "failed to read data directory", slog.String("path", d.dataDir), rlog.Error(err))
return nil, fmt.Errorf("failed to read data directory: %w", err)
}
dataHashes := []string{}
for _, entry := range dataDirs {
dataHashes = append(dataHashes, entry.Name())
}
hashToDelete := make([]string, 0, 5)
for _, v := range dataHashes {
if !slices.Contains(daemonsHashes, v) {
hashToDelete = append(hashToDelete, v)
}
}
d.log.Info(ctx, "marked torrents to delete",
slog.Int("count", len(hashToDelete)),
slog.Any("infohashes", hashToDelete),
)
if !run {
d.log.Info(ctx, "dry run, skipping deletion")
return hashToDelete, nil
}
for _, hash := range hashToDelete {
d.log.Info(ctx, "deleting stale torrent data", slog.String("infohash", hash))
err := os.RemoveAll(path.Join(d.dataDir, hash))
if err != nil {
d.log.Error(ctx, "failed to delete torrent data", slog.String("infohash", hash), rlog.Error(err))
return nil, fmt.Errorf("failed to delete torrent data: %w", err)
}
}
return hashToDelete, nil
}
func (d *Daemon) CleanupUnregistred(ctx context.Context, run bool) ([]string, error) {
d.log.Info(ctx, "cleanup started")
torrentInfos, err := d.client.qb.Torrent().GetTorrents(ctx, &qbittorrent.TorrentOption{})
if err != nil {
d.log.Error(ctx, "failed to get torrents", rlog.Error(err))