Add Job System (#656)
* Add Job System * Remove testing stuff * changelog * plugin work * Convert a couple more jobs * simplify callback * assert * Logging changes and rendezvous perf * changelog * sb segment viewed job * clear cache, and few changes * Lint fix * a couple more jobs * reduce rendezvous in web server * refactor * Lint fix * no message * test shutdown * comment * free fields --------- Co-authored-by: github-action linter <githubaction@githubaction.com>
This commit is contained in:
1 parent
45983a67af
commit
d70a07049a
74 files changed
+1090
-298
No files matched your search
@@ -1,3 +1,4 @@
|
||||
import "pkg:/source/utils/StringUtils.bs"
|
||||
import "pkg:/source/utils/Types.bs"
|
||||
|
||||
namespace AsyncTask
|
||||
@@ -9,7 +10,7 @@ namespace AsyncTask
|
||||
end function
|
||||
|
||||
function Create(taskName as string, input as object, callback = invalid as object) as object
|
||||
id = StrI(rnd(2147483647), 36)
|
||||
id = StringUtils.RandomId()
|
||||
task = createObject("roSGNode", taskName)
|
||||
task.id = id
|
||||
task.input = input
|
||||
|
||||
@@ -0,0 +1,152 @@
|
||||
import "pkg:/components/JobSystem/Jobs.bs"
|
||||
import "pkg:/components/JobSystem/JobSystem.bs"
|
||||
|
||||
namespace Tests
|
||||
|
||||
@suite
|
||||
class JobSystemTests extends Rooibos.BaseTestSuite
|
||||
|
||||
protected override function setup()
|
||||
m.logger = CreateObject("roSGNode", "Logger")
|
||||
end function
|
||||
|
||||
protected override function beforeEach()
|
||||
m.jobQueue = CreateObject("roSGNode", "JobQueue")
|
||||
end function
|
||||
|
||||
protected override function afterEach()
|
||||
m.jobQueue.shutdown = true
|
||||
m.jobQueue = invalid
|
||||
end function
|
||||
|
||||
@describe("JobSystem")
|
||||
@it("Queues and processes jobs correctly")
|
||||
function _()
|
||||
successCalls = 0
|
||||
errorCalls = 0
|
||||
|
||||
messagePort = CreateObject("roMessagePort")
|
||||
|
||||
for i = 1 to 100
|
||||
callbackNode = CreateObject("roSGNode", "JobCallback")
|
||||
callbackNode.observeField("done", messagePort, ["success"])
|
||||
JobSystem.QueueJob(m.jobQueue, Jobs.SimpleTestJob, { sleep: 50, success: i mod 2 = 0 }, callbackNode)
|
||||
end for
|
||||
|
||||
while true
|
||||
msg = wait(1000, messagePort)
|
||||
|
||||
if msg = invalid
|
||||
m.testSuite.fail("Waiting for job messages timed out after 1000ms")
|
||||
exit while
|
||||
end if
|
||||
|
||||
if type(msg) = "roSGNodeEvent"
|
||||
if msg.getField() = "done"
|
||||
info = msg.getInfo()
|
||||
if info.success
|
||||
successCalls += 1
|
||||
else
|
||||
errorCalls += 1
|
||||
end if
|
||||
end if
|
||||
end if
|
||||
|
||||
if successCalls = 50 and errorCalls = 50
|
||||
exit while
|
||||
end if
|
||||
end while
|
||||
end function
|
||||
|
||||
@it("Cancels jobs correctly")
|
||||
function _()
|
||||
cancelCalls = 0
|
||||
|
||||
messagePort = CreateObject("roMessagePort")
|
||||
|
||||
callbacks = []
|
||||
for i = 1 to 10
|
||||
callbackNode = CreateObject("roSGNode", "JobCallback")
|
||||
callbackNode.observeField("done", messagePort, ["success", "cancel"])
|
||||
callbacks.push(callbackNode)
|
||||
JobSystem.QueueJob(m.jobQueue, Jobs.SimpleTestJob, { sleep: 5000, success: true }, callbackNode)
|
||||
end for
|
||||
|
||||
' Wait for jobs to start before we start cancelling them
|
||||
sleep (1000)
|
||||
|
||||
for i = 1 to 10
|
||||
callbackNode = callbacks[i - 1]
|
||||
callbackNode.cancel = true
|
||||
end for
|
||||
|
||||
while true
|
||||
msg = wait(1000, messagePort)
|
||||
|
||||
if msg = invalid
|
||||
m.testSuite.fail("Waiting for job messages timed out after 1000ms")
|
||||
exit while
|
||||
end if
|
||||
|
||||
if type(msg) = "roSGNodeEvent"
|
||||
if msg.getField() = "done"
|
||||
info = msg.getInfo()
|
||||
if info.cancel and not info.success
|
||||
cancelCalls += 1
|
||||
else if info.success
|
||||
m.testSuite.fail("Job should not have succeeded after cancellation")
|
||||
exit while
|
||||
end if
|
||||
end if
|
||||
end if
|
||||
|
||||
if cancelCalls = 10
|
||||
exit while
|
||||
end if
|
||||
end while
|
||||
end function
|
||||
|
||||
@it("Cancels jobs after they start immediately")
|
||||
function _()
|
||||
cancelCalls = 0
|
||||
|
||||
messagePort = CreateObject("roMessagePort")
|
||||
|
||||
callbacks = []
|
||||
for i = 1 to 10
|
||||
callbackNode = CreateObject("roSGNode", "JobCallback")
|
||||
callbackNode.observeField("done", messagePort, ["success", "cancel"])
|
||||
callbacks.push(callbackNode)
|
||||
JobSystem.QueueJob(m.jobQueue, Jobs.SimpleTestJob, { sleep: 5000, success: true }, callbackNode)
|
||||
callbackNode.cancel = true
|
||||
end for
|
||||
|
||||
while true
|
||||
msg = wait(1000, messagePort)
|
||||
|
||||
if msg = invalid
|
||||
m.testSuite.fail("Waiting for job messages timed out after 1000ms")
|
||||
exit while
|
||||
end if
|
||||
|
||||
if type(msg) = "roSGNodeEvent"
|
||||
if msg.getField() = "done"
|
||||
info = msg.getInfo()
|
||||
if info.cancel and not info.success
|
||||
cancelCalls += 1
|
||||
else if info.success
|
||||
m.testSuite.fail("Job should not have succeeded after cancellation")
|
||||
exit while
|
||||
end if
|
||||
end if
|
||||
end if
|
||||
|
||||
if cancelCalls = 10
|
||||
exit while
|
||||
end if
|
||||
end while
|
||||
end function
|
||||
|
||||
end class
|
||||
|
||||
end namespace
|
||||
@@ -0,0 +1,30 @@
|
||||
import "pkg:/source/utils/Logging.bs"
|
||||
import "pkg:/source/utils/Types.bs"
|
||||
|
||||
namespace Assert
|
||||
|
||||
function IsTrue(condition as dynamic, message = invalid as dynamic) as void
|
||||
if not ValidBool(condition)
|
||||
throw "Assertion failed: " + ToString(message)
|
||||
end if
|
||||
end function
|
||||
|
||||
function IsFalse(condition as dynamic, message = invalid as dynamic) as void
|
||||
if ValidBool(condition)
|
||||
throw "Assertion failed: " + ToString(message)
|
||||
end if
|
||||
end function
|
||||
|
||||
function IsValid(value as dynamic, message = invalid as dynamic) as void
|
||||
if value = invalid
|
||||
throw "Assertion failed: " + ToString(message)
|
||||
end if
|
||||
end function
|
||||
|
||||
function AreEqual(expected as dynamic, actual as dynamic, message = invalid as dynamic) as void
|
||||
if expected <> actual
|
||||
throw "Assertion failed: Expected " + ToString(expected) + ", but got " + ToString(actual) + ". " + ToString(message)
|
||||
end if
|
||||
end function
|
||||
|
||||
end namespace
|
||||
@@ -1,10 +1,10 @@
|
||||
import "pkg:/source/AsyncTask/AsyncTask.bs"
|
||||
import "pkg:/source/AsyncTask/Tasks.bs"
|
||||
import "pkg:/components/JobSystem/Jobs.bs"
|
||||
import "pkg:/components/JobSystem/JobSystem.bs"
|
||||
|
||||
namespace CacheUtils
|
||||
|
||||
function Clear()
|
||||
AsyncTask.Start(Tasks.ClearCacheTask, invalid)
|
||||
function Clear(jobQueue as object)
|
||||
JobSystem.QueueJob(jobQueue, Jobs.ClearCacheJob)
|
||||
end function
|
||||
|
||||
end namespace
|
||||
@@ -3,6 +3,18 @@ import "pkg:/source/utils/Types.bs"
|
||||
|
||||
namespace CancellationUtils
|
||||
|
||||
function CreateCancellation(node as dynamic, field as string, value as dynamic) as dynamic
|
||||
if node = invalid or field = invalid
|
||||
return invalid
|
||||
end if
|
||||
|
||||
return {
|
||||
"node": node
|
||||
"field": field
|
||||
"value": value
|
||||
}
|
||||
end function
|
||||
|
||||
function IsCancelled(cancellation as dynamic) as boolean
|
||||
if cancellation = invalid or cancellation.node = invalid or cancellation.field = invalid
|
||||
return false
|
||||
|
||||
@@ -1,33 +0,0 @@
|
||||
import "pkg:/source/utils/Logging.bs"
|
||||
|
||||
@asynctask
|
||||
function ClearCacheTask() as object
|
||||
fs = CreateObject("roFileSystem")
|
||||
if fs = invalid
|
||||
LogError("Failed to create roFileSystem object")
|
||||
return invalid
|
||||
end if
|
||||
|
||||
ClearVolume(fs, "tmp:/")
|
||||
ClearVolume(fs, "cachefs:/")
|
||||
|
||||
return invalid
|
||||
end function
|
||||
|
||||
function ClearVolume(fs as object, volume as string)
|
||||
items = fs.GetDirectoryListing(volume)
|
||||
for each item in items
|
||||
fullpath = volume + item
|
||||
|
||||
' Skip logs directory
|
||||
if fullpath = "cachefs:/logs"
|
||||
continue for
|
||||
end if
|
||||
|
||||
if not fs.Delete(fullpath)
|
||||
LogError("Failed to delete:", fullpath)
|
||||
end if
|
||||
end for
|
||||
|
||||
LogInfo("Cleared:", volume)
|
||||
end function
|
||||
@@ -31,7 +31,7 @@ namespace DebugUtils
|
||||
out += `\n` + "║ " + line + string(maxLen - line.len(), " ") + " ║"
|
||||
end for
|
||||
out += `\n` + "╚" + string(maxLen + 2, "═") + "╝"
|
||||
? out
|
||||
print out
|
||||
#end if
|
||||
end function
|
||||
end namespace
|
||||
@@ -37,5 +37,9 @@ namespace StringUtils
|
||||
end if
|
||||
return input.Mid(startIndex, endIndex - startIndex)
|
||||
end function
|
||||
|
||||
function RandomId() as string
|
||||
return StrI(rnd(2147483647), 36)
|
||||
end function
|
||||
end namespace
|
||||
|
||||
Reference in new issue
Block a user