[skip ci] Fix memory leak in MarkVideoWatchedTask

This commit is contained in:
Brahim Hadriche committed 2025-06-21 21:20:33 -04:00
1 parent 9f5efb0ba7
commit 8f8a8980df
6 files changed
+57 -30

No files matched your search

@@ -0,0 +1,86 @@
import "pkg:/components/Services/Innertube/InnertubeService.bs"
import "pkg:/components/Services/Invidious/InvidiousService.bs"
import "pkg:/source/utils/MessagePortUtils.bs"
@job("MarkVideoWatchedJob")
function ExecuteJob() as void
input = JobGetInput()
videoId = input.videoId
invidiousNode = input.invidious
authToken = input.authToken
profileType = input.profileType
cancellation = CancellationUtils.CreateCancellation(JobGetCallbackNode(), "cancel", true)
if profileType = "invidious"
service = new Invidious.InvidiousService(invidiousNode)
service.MarkVideoWatched(videoId, cancellation)
return
end if
if profileType <> "youtube"
JobError(`Unsupported profile type: ${profileType}`)
return
end if
playbackTrackingUrls = InnertubeService.AuthGetPlaybackTrackingUrls(videoId, {
accessToken: authToken.token
onBehalfOfUser: authToken.activeIdentifyToken
cancellation: cancellation
})
if playbackTrackingUrls.error <> invalid
JobError(`Failed to get playback tracking URLs: ${playbackTrackingUrls.error}`)
return
end if
if StringUtils.IsNullOrEmpty(playbackTrackingUrls.videostatsPlaybackUrl)
JobError("videostatsPlaybackUrl is empty")
return
end if
markedAsWatched = InnertubeService.AuthMarkVideoWatched(playbackTrackingUrls.videostatsPlaybackUrl, {
accessToken: authToken.token
onBehalfOfUser: authToken.activeIdentifyToken
cancellation: cancellation
})
if not markedAsWatched
JobError("Failed to mark video as watched")
return
end if
if StringUtils.IsNullOrEmpty(playbackTrackingUrls.videostatsWatchtimeUrl)
JobError("videostatsWatchtimeUrl is empty")
return
end if
if CancellationUtils.IsCancelled(cancellation)
JobCancel()
return
end if
port = CreateObject("roMessagePort")
callbackNode = JobGetCallbackNode()
MessagePortUtils.ObserveFieldScopedExSafe(callbackNode, "watchTime", port)
CancellationUtils.ObserveCancellationScoped(cancellation, port)
while true
message = wait(60000, port)
if CancellationUtils.IsCancellationEvent(cancellation, message)
JobCancel()
return
end if
if message = invalid
continue while
end if
watchTime = message.getData()
InnertubeService.AuthMarkVideoWatchTime(watchTime, playbackTrackingUrls.videostatsWatchtimeUrl, {
accessToken: authToken.token
onBehalfOfUser: authToken.activeIdentifyToken
' TODO:P1 if we pass cancellation here, http client will remove scoped
' observer, which is stored on the current node.
' This requires an extra observer node to be independent.
cancellation: invalid
})
end while
end function