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)
)
-- | Fetch the current status from transmission, and remove the tranmission hash from our database
-- iff it does not exist in transmission anymore
-- | Fetch the current status from transmission,
-- and remove the transmission hash and torrent file from our database iff it does not exist in transmission anymore
getAndUpdateTransmissionTorrentsStatus ::
( MonadTransmission m,
MonadThrow m,
MonadLogger 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)))
getAndUpdateTransmissionTorrentsStatus knownTorrents = do
getAndUpdateTransmissionTorrentsStatus knownTorrents = inSpan' "getAndUpdateTransmissionTorrentsStatus" $ \span -> do
let fields = ["hashString", "percentDone"]
actualTorrents <-
lift @Transaction $
@ -77,10 +79,32 @@ getAndUpdateTransmissionTorrentsStatus knownTorrents = do
)
<&> Map.fromList
let toDelete = Map.difference knownTorrents actualTorrents
if
| Map.null toDelete -> do
addEventSimple span "We know about all transmission hashes."
pure actualTorrents
| otherwise -> inSpan' "Delete outdated transmission hashes" $ \span' -> do
addAttribute
span'
"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
SET transmission_torrent_hash = NULL,
torrent_file = NULL
WHERE transmission_torrent_hash = ANY (?::text[])
|]
$ Only (toDelete & Map.keys <&> (.torrentHash) & PGArray :: PGArray Text)

View file

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