[skip ci] Fix memory leak in MarkVideoWatchedTask
This commit is contained in:
1 parent
9f5efb0ba7
commit
8f8a8980df
6 files changed
+57
-30
No files matched your search
@@ -3,7 +3,7 @@ import "pkg:/source/utils/MessagePortUtils.bs"
|
||||
import "pkg:/source/utils/StringUtils.bs"
|
||||
|
||||
' Maximum number of JobTasks that can be created
|
||||
const MAX_JOB_TASK_COUNT = 15
|
||||
const MAX_JOB_TASK_COUNT = 20
|
||||
' Minimum number of JobTasks that will be kept alive
|
||||
const MIN_JOB_TASK_COUNT = 2
|
||||
' Time in milliseconds to wait before scaling down JobTasks
|
||||
|
||||
+27
-18
@@ -2,23 +2,26 @@ import "pkg:/components/Services/Innertube/InnertubeService.bs"
|
||||
import "pkg:/components/Services/Invidious/InvidiousService.bs"
|
||||
import "pkg:/source/utils/MessagePortUtils.bs"
|
||||
|
||||
@asynctask
|
||||
function MarkVideoWatchedTask(input as object) as object
|
||||
@job("MarkVideoWatchedJob")
|
||||
function ExecuteJob() as void
|
||||
input = JobGetInput()
|
||||
|
||||
videoId = input.videoId
|
||||
invidiousNode = input.invidious
|
||||
authToken = input.authToken
|
||||
profileType = input.profileType
|
||||
cancellation = m.top.cancellation
|
||||
|
||||
cancellation = CancellationUtils.CreateCancellation(JobGetCallbackNode(), "cancel", true)
|
||||
|
||||
if profileType = "invidious"
|
||||
service = new Invidious.InvidiousService(invidiousNode)
|
||||
service.MarkVideoWatched(videoId, cancellation)
|
||||
return invalid
|
||||
return
|
||||
end if
|
||||
|
||||
if profileType <> "youtube"
|
||||
LogError("MarkVideoWatchedTask: Unsupported profile type:", profileType)
|
||||
return invalid
|
||||
JobError(`Unsupported profile type: ${profileType}`)
|
||||
return
|
||||
end if
|
||||
|
||||
playbackTrackingUrls = InnertubeService.AuthGetPlaybackTrackingUrls(videoId, {
|
||||
@@ -28,13 +31,13 @@ function MarkVideoWatchedTask(input as object) as object
|
||||
})
|
||||
|
||||
if playbackTrackingUrls.error <> invalid
|
||||
LogError("MarkVideoWatchedTask: Failed to get playback tracking URLs:", playbackTrackingUrls.error)
|
||||
return invalid
|
||||
JobError(`Failed to get playback tracking URLs: ${playbackTrackingUrls.error}`)
|
||||
return
|
||||
end if
|
||||
|
||||
if StringUtils.IsNullOrEmpty(playbackTrackingUrls.videostatsPlaybackUrl)
|
||||
LogError("MarkVideoWatchedTask: videostatsPlaybackUrl is empty")
|
||||
return invalid
|
||||
JobError("videostatsPlaybackUrl is empty")
|
||||
return
|
||||
end if
|
||||
|
||||
markedAsWatched = InnertubeService.AuthMarkVideoWatched(playbackTrackingUrls.videostatsPlaybackUrl, {
|
||||
@@ -43,25 +46,29 @@ function MarkVideoWatchedTask(input as object) as object
|
||||
cancellation: cancellation
|
||||
})
|
||||
if not markedAsWatched
|
||||
return invalid
|
||||
JobError("Failed to mark video as watched")
|
||||
return
|
||||
end if
|
||||
|
||||
if StringUtils.IsNullOrEmpty(playbackTrackingUrls.videostatsWatchtimeUrl)
|
||||
LogError("MarkVideoWatchedTask: videostatsWatchtimeUrl is empty")
|
||||
return invalid
|
||||
JobError("videostatsWatchtimeUrl is empty")
|
||||
return
|
||||
end if
|
||||
|
||||
if CancellationUtils.IsCancelled(cancellation)
|
||||
return invalid
|
||||
JobCancel()
|
||||
return
|
||||
end if
|
||||
|
||||
port = CreateObject("roMessagePort")
|
||||
MessagePortUtils.ObserveFieldScopedExSafe(m.top, "watchTime", port)
|
||||
callbackNode = JobGetCallbackNode()
|
||||
MessagePortUtils.ObserveFieldScopedExSafe(callbackNode, "watchTime", port)
|
||||
CancellationUtils.ObserveCancellationScoped(cancellation, port)
|
||||
while true
|
||||
message = wait(60000, port)
|
||||
if CancellationUtils.IsCancellationEvent(cancellation, message)
|
||||
return invalid
|
||||
JobCancel()
|
||||
return
|
||||
end if
|
||||
if message = invalid
|
||||
continue while
|
||||
@@ -70,8 +77,10 @@ function MarkVideoWatchedTask(input as object) as object
|
||||
InnertubeService.AuthMarkVideoWatchTime(watchTime, playbackTrackingUrls.videostatsWatchtimeUrl, {
|
||||
accessToken: authToken.token
|
||||
onBehalfOfUser: authToken.activeIdentifyToken
|
||||
cancellation: cancellation
|
||||
' 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
|
||||
return invalid
|
||||
end function
|
||||
@@ -330,19 +330,22 @@ function MarkVideoWatched(videoId as dynamic) as void
|
||||
return
|
||||
end if
|
||||
|
||||
if m.markVideoWatchedTask <> invalid
|
||||
m.markVideoWatchedTask.cancel = true
|
||||
if m.markVideoWatchedJob <> invalid
|
||||
m.markVideoWatchedJob.cancel = true
|
||||
end if
|
||||
|
||||
profileType = authToken.profileNode.type
|
||||
m.markVideoWatchedTask = AsyncTask.Create(Tasks.MarkVideoWatchedTask, {
|
||||
|
||||
input = {
|
||||
videoId: videoId
|
||||
invidious: invidious
|
||||
authToken: authToken
|
||||
profileType: profileType
|
||||
})
|
||||
m.markVideoWatchedTask.addField("watchTime", "float", false)
|
||||
m.markVideoWatchedTask.control = "RUN"
|
||||
}
|
||||
|
||||
m.markVideoWatchedJob = JobSystem.CreateCallback()
|
||||
m.markVideoWatchedJob.addField("watchTime", "float", false)
|
||||
JobSystem.QueueJob(m.jobQueue, Jobs.MarkVideoWatchedJob, input, m.markVideoWatchedJob)
|
||||
|
||||
if profileType <> "youtube"
|
||||
return
|
||||
@@ -355,7 +358,7 @@ function MarkVideoWatched(videoId as dynamic) as void
|
||||
end function
|
||||
|
||||
function OnPositionChangeMarkWatchTime(event as object) as void
|
||||
if m.markVideoWatchedTask = invalid
|
||||
if m.markVideoWatchedJob = invalid
|
||||
return
|
||||
end if
|
||||
|
||||
@@ -365,7 +368,7 @@ function OnPositionChangeMarkWatchTime(event as object) as void
|
||||
if currentWatchTimeMarker = currentPosition
|
||||
return
|
||||
end if
|
||||
m.markVideoWatchedTask.watchTime = m.watchTimeMarker
|
||||
m.markVideoWatchedJob.watchTime = m.watchTimeMarker
|
||||
end function
|
||||
|
||||
function OnClose()
|
||||
@@ -373,9 +376,9 @@ function OnClose()
|
||||
|
||||
m.top.previousPlayer = invalid
|
||||
|
||||
if m.markVideoWatchedTask <> invalid
|
||||
m.markVideoWatchedTask.cancel = true
|
||||
m.markVideoWatchedTask = invalid
|
||||
if m.markVideoWatchedJob <> invalid
|
||||
m.markVideoWatchedJob.cancel = true
|
||||
m.markVideoWatchedJob = invalid
|
||||
end if
|
||||
if m.videoContentTask <> invalid
|
||||
m.videoContentTask.cancel = true
|
||||
|
||||
@@ -27,4 +27,12 @@ namespace Assert
|
||||
end if
|
||||
end function
|
||||
|
||||
function NodeHasField(node as dynamic, field as string, message = invalid as dynamic) as void
|
||||
if node = invalid
|
||||
throw "Assertion failed: Node is invalid. " + ToString(message)
|
||||
end if
|
||||
if not node.hasField(field)
|
||||
throw "Assertion failed: Node does not have field '" + field + "'. " + ToString(message)
|
||||
end if
|
||||
end function
|
||||
end namespace
|
||||
@@ -1,6 +1,12 @@
|
||||
import "AssertUtils.bs"
|
||||
|
||||
namespace MessagePortUtils
|
||||
|
||||
function ObserveFieldScopedExSafe(node as dynamic, field as string, port as dynamic) as void
|
||||
#if DEBUG
|
||||
Assert.IsValid(node, "Node is invalid")
|
||||
Assert.NodeHasField(node, field, `Node does not have field "${field}"`)
|
||||
#end if
|
||||
' ObserveFieldScopedEx was added in 12.0
|
||||
if FindMemberFunction(node, "ObserveFieldScopedEx") <> invalid
|
||||
' bs:disable-next-line NO_OBSERVE_FIELD_SCOPED_EX
|
||||
|
||||
Reference in new issue
Block a user