fix(users/Profpatsch/whatcd-resolver): also delete db torrent file

If transmission does not know about a torrent file anymore, we should
not only delete our local transmission torrent hash, but also the
torrent file.

Before, it would always display the old weighted torrent, even after
removing it from transmission. Now, it will automatically clear and
switch to the new top-weighted mp3 version.

Small bug: since we only clean the torrents after already fetching the
table data, we get a stale result right after clean-up. Maybe we
should re-do the fetch instead.

Also logs what gets deleted and only ever deletes if there is
something to delete, leading to more obvious traces.

Change-Id: I0f6c1dc3669d0f549efaba2c46c34b77c1eb7e33
Reviewed-on: https://cl.tvl.fyi/c/depot/+/12332
Reviewed-by: Profpatsch <mail@profpatsch.de>
Tested-by: BuildkiteCI
This commit is contained in:
Profpatsch 2024-08-24 14:54:44 +02:00
parent 861b7caa06
commit f6dc1f1819
2 changed files with 38 additions and 15 deletions

View file

@ -49,18 +49,20 @@ scientificPercentage =
| otherwise -> Right $ Percentage $ ceiling (f * 100) | otherwise -> Right $ Percentage $ ceiling (f * 100)
) )
-- | Fetch the current status from transmission, and remove the tranmission hash from our database -- | Fetch the current status from transmission,
-- iff it does not exist in transmission anymore -- and remove the transmission hash and torrent file from our database iff it does not exist in transmission anymore
getAndUpdateTransmissionTorrentsStatus :: getAndUpdateTransmissionTorrentsStatus ::
( MonadTransmission m, ( MonadTransmission m,
MonadThrow m, MonadThrow m,
MonadLogger m, MonadLogger m,
MonadPostgres m, MonadPostgres m,
MonadOtel m MonadOtel m,
HasField "groupId" info Int,
HasField "torrentId" info Int
) => ) =>
Map (Label "torrentHash" Text) () -> Map (Label "torrentHash" Text) info ->
(Transaction m (Map (Label "torrentHash" Text) (Label "percentDone" Percentage))) (Transaction m (Map (Label "torrentHash" Text) (Label "percentDone" Percentage)))
getAndUpdateTransmissionTorrentsStatus knownTorrents = do getAndUpdateTransmissionTorrentsStatus knownTorrents = inSpan' "getAndUpdateTransmissionTorrentsStatus" $ \span -> do
let fields = ["hashString", "percentDone"] let fields = ["hashString", "percentDone"]
actualTorrents <- actualTorrents <-
lift @Transaction $ lift @Transaction $
@ -77,14 +79,36 @@ getAndUpdateTransmissionTorrentsStatus knownTorrents = do
) )
<&> Map.fromList <&> Map.fromList
let toDelete = Map.difference knownTorrents actualTorrents let toDelete = Map.difference knownTorrents actualTorrents
execute if
[fmt| | Map.null toDelete -> do
UPDATE redacted.torrents_json addEventSimple span "We know about all transmission hashes."
SET transmission_torrent_hash = NULL pure actualTorrents
WHERE transmission_torrent_hash = ANY (?::text[]) | otherwise -> inSpan' "Delete outdated transmission hashes" $ \span' -> do
|] addAttribute
$ Only (toDelete & Map.keys <&> (.torrentHash) & PGArray :: PGArray Text) span'
pure actualTorrents "db.delete-transmission-hashes"
( toDelete
& Map.toList
& Enc.list
( \(k, v) ->
Enc.object
[ ("torrentHash", Enc.text k.torrentHash),
("groupId", Enc.int v.groupId),
("torrentId", Enc.int v.torrentId)
]
)
& jsonAttribute
)
_ <-
execute
[fmt|
UPDATE redacted.torrents_json
SET transmission_torrent_hash = NULL,
torrent_file = NULL
WHERE transmission_torrent_hash = ANY (?::text[])
|]
$ Only (toDelete & Map.keys <&> (.torrentHash) & PGArray :: PGArray Text)
pure actualTorrents
getTransmissionTorrentsTable :: getTransmissionTorrentsTable ::
(MonadTransmission m, MonadThrow m, MonadLogger m, MonadOtel m) => m Html (MonadTransmission m, MonadThrow m, MonadLogger m, MonadOtel m) => m Html

View file

@ -566,10 +566,9 @@ getBestTorrentsData artistFilter = inSpan' "get torrents table data" $ \span ->
( bestStale ( bestStale
& mapMaybe & mapMaybe
( \td -> case td.torrentStatus of ( \td -> case td.torrentStatus of
InTransmission h -> Just h InTransmission h -> Just (getLabel @"torrentHash" h, td)
_ -> Nothing _ -> Nothing
) )
<&> (\t -> (getLabel @"torrentHash" t, t.transmissionInfo))
& Map.fromList & Map.fromList
) )
pure $ pure $