Usage Examples
Script Example: (NOT LOCALSCRIPT)
--[[
TaskScheduler Example
made by @knopka_01 :D
]]
-- Load the TaskScheduler module using its ID
local TaskScheduler = require(121440418698601)
-- Add a log entry saying the module has been loaded
TaskScheduler:AddLog("TaskScheduler Module Loaded", "require module")
-- Define the first task function (prints a message and waits for 1 second)
local function Task1()
print("Task 1 is running!")
wait(1)
return "Task 1 result" -- Returns a result after finishing
end
-- Define the second task function (prints a message and causes an error)
local function Task2()
print("Task 2 is running!")
error("Task 2 Error! Oh no!!!") -- Simulates an error to test retries
end
-- Define the third task function (prints a message and waits for 2 seconds)
local function Task3()
print("Task 3 is running!")
wait(2)
return "Task 3 result" -- Returns a result after finishing
end
-- Define the fourth task function (prints a message and waits for 3 seconds)
local function Task4()
print("Task 4 is running!")
wait(3)
return "Task 4 result" -- Returns a result after finishing
end
-- Add the first task to the TaskScheduler with a high priority and a timeout of 5 seconds
local task1 = TaskScheduler:AddTask("Task1", Task1, nil, TaskScheduler.PriorityLevels.High, 5)
-- Add the second task with normal priority, and it depends on Task1 to finish first
local task2 = TaskScheduler:AddTask("Task2", Task2, { "Task1" }, TaskScheduler.PriorityLevels.Normal, 5)
-- Add the third task with low priority, depending on Task2
local task3 = TaskScheduler:AddTask("Task3", Task3, { "Task2" }, TaskScheduler.PriorityLevels.Low, 5)
-- Add the fourth task with normal priority and a longer timeout of 10 seconds
local task4 = TaskScheduler:AddTask("Task4", Task4, nil, TaskScheduler.PriorityLevels.Normal, 10)
-- Group task1 and task2 under "NetworkTasks" for easy management
task1:SetCategory("NetworkTasks")
task2:SetCategory("NetworkTasks")
-- Group task3 and task4 under a namespace "Compute"
task3:SetNamespace("Compute")
task4:SetNamespace("Compute")
-- Set task2 to retry up to 3 times if it fails
task2.MaxRetries = 3
-- Use exponential retry strategy (increases wait time between retries)
task2.RetryStrategy = TaskScheduler.RetryStrategies.Exponential
-- Add a rule to throttle tasks in the "NetworkTasks" group to run every 2 seconds
TaskScheduler:AddThrottlingRule("NetworkThrottle", 2)
-- Assign the throttle rule to task1
task1.ThrottleKey = "NetworkThrottle"
-- Event that triggers when task1 starts
task1.OnStart = function(task)
print(task.Name .. " has started")
end
-- Event that triggers when task1 completes
task1.OnComplete = function(task)
print(task.Name .. " completed with result: " .. tostring(task.Result))
end
-- Event that triggers when task2 fails
task2.OnFail = function(task)
print(task.Name .. " failed with error: " .. tostring(task.Error))
end
-- Event that triggers when task2 retries after failing
task2.OnRetry = function(task)
print(task.Name .. " retrying, attempt " .. task.Retries)
end
-- Add task1 and task2 to a group called "ImportantTasks"
TaskScheduler:AddTaskToGroup("ImportantTasks", task1)
TaskScheduler:AddTaskToGroup("ImportantTasks", task2)
-- Add task3 and task4 to a group called "ComputeTasks"
TaskScheduler:AddTaskToGroup("ComputeTasks", task3)
TaskScheduler:AddTaskToGroup("ComputeTasks", task4)
-- Set a global error handler for any task that fails
TaskScheduler.GlobalErrorHandler = function(task)
print("Global error handler: Task " .. task.Name .. " failed.")
end
-- Run all tasks in the "ImportantTasks" group (task1 and task2)
TaskScheduler:RunGroup("ImportantTasks")
-- Wait for 2 seconds, then pause task3
task.wait(2)
TaskScheduler:PauseTask("Task3")
print("Task 3 paused")
-- Wait for 3 more seconds, then resume task3
task.wait(3)
TaskScheduler:ResumeTask("Task3")
print("Task 3 resumed")
-- Cancel task4, meaning it won't run
TaskScheduler:CancelTask("Task4")
print("Task 4 was cancelled")
-- Run all remaining tasks in the scheduler
TaskScheduler:RunAll()
-- Get all the logs and print them
local logs = TaskScheduler:GetLogs()
for i, log in ipairs(logs) do
print("Log " .. i .. ": " .. log.Message .. " (Time: " .. log.Time .. ")")
end
-- Example of how to cancel or pause tasks dynamically (for future use):
-- TaskScheduler:CancelTask("LoadPlayerData")
-- TaskScheduler:PauseAllTasks()
-- TaskScheduler:ResumeAllTasks()
LocalScript Example:
(Using a LocalScript will require manual installation of this module.)
local TaskScheduler = require(game.ReplicatedStorage.TaskScheduler) -- Load the TaskScheduler module
-- Example task functions (these are the tasks we want to run)
local function Task1()
print("Task 1 is running!")
wait(1) -- Simulate work by waiting 1 second
return "Task 1 result" -- Return a result when finished
end
local function Task2()
print("Task 2 is running!")
error("Task 2 Errored! Oh no!!") -- Simulate an error in task 2
end
local function Task3()
print("Task 3 is running!")
wait(2) -- Simulate work by waiting 2 seconds
return "Task 3 result" -- Return a result when finished
end
local function Task4()
print("Task 4 is running!")
wait(3) -- Simulate work by waiting 3 seconds
return "Task 4 result" -- Return a result when finished
end
-- Add tasks to the TaskScheduler
-- Task1: High priority, no dependencies, timeout of 5 seconds
local task1 = TaskScheduler:AddTask("Task1", Task1, nil, TaskScheduler.PriorityLevels.High, 5)
-- Task2: Normal priority, depends on Task1, timeout of 5 seconds
local task2 = TaskScheduler:AddTask("Task2", Task2, { "Task1" }, TaskScheduler.PriorityLevels.Normal, 5)
-- Task3: Low priority, depends on Task2, timeout of 5 seconds
local task3 = TaskScheduler:AddTask("Task3", Task3, { "Task2" }, TaskScheduler.PriorityLevels.Low, 5)
-- Task4: Normal priority, no dependencies, timeout of 10 seconds
local task4 = TaskScheduler:AddTask("Task4", Task4, nil, TaskScheduler.PriorityLevels.Normal, 10)
-- Grouping tasks by categories and namespaces
task1:SetCategory("NetworkTasks") -- Group Task1 under 'NetworkTasks'
task2:SetCategory("NetworkTasks") -- Group Task2 under 'NetworkTasks'
task3:SetNamespace("Compute") -- Group Task3 under 'Compute'
task4:SetNamespace("Compute") -- Group Task4 under 'Compute'
-- Set up retries for Task2 (if it fails)
task2.MaxRetries = 3 -- Retry up to 3 times
task2.RetryStrategy = TaskScheduler.RetryStrategies.Exponential -- Retry with increasing delay
-- Throttle Task1 (limit how often it can run)
TaskScheduler:AddThrottlingRule("NetworkThrottle", 2) -- Set 2-second throttle for network tasks
task1.ThrottleKey = "NetworkThrottle" -- Apply the throttle rule to Task1
-- Task event handlers (custom functions that run when the task starts, finishes, fails, etc.)
task1.OnStart = function(task)
print(task.Name .. " has started")
end
task1.OnComplete = function(task)
print(task.Name .. " completed with result: " .. tostring(task.Result))
end
task2.OnFail = function(task)
print(task.Name .. " failed with error: " .. tostring(task.Error))
end
task2.OnRetry = function(task)
print(task.Name .. " retrying, attempt " .. task.Retries)
end
-- Group tasks together for batch operations
TaskScheduler:AddTaskToGroup("ImportantTasks", task1) -- Add Task1 and Task2 to the "ImportantTasks" group
TaskScheduler:AddTaskToGroup("ImportantTasks", task2)
TaskScheduler:AddTaskToGroup("ComputeTasks", task3) -- Add Task3 and Task4 to the "ComputeTasks" group
TaskScheduler:AddTaskToGroup("ComputeTasks", task4)
-- Set a global error handler (runs if any task fails and doesn't have its own error handler)
TaskScheduler.GlobalErrorHandler = function(task)
print("Global error handler: Task " .. task.Name .. " failed.")
end
-- Run all tasks in the "ImportantTasks" group (task1 and task2)
TaskScheduler:RunGroup("ImportantTasks")
-- Pause and resume tasks after some time has passed
task.wait(2) -- Wait 2 seconds
TaskScheduler:PauseTask("Task3") -- Pause Task3
print("Task 3 paused")
task.wait(3) -- Wait 3 more seconds
TaskScheduler:ResumeTask("Task3") -- Resume Task3
print("Task 3 resumed")
-- Cancel Task4 (prevent it from running)
TaskScheduler:CancelTask("Task4")
print("Task 4 was cancelled")
-- Run all tasks that haven't been run yet
TaskScheduler:RunAll()
-- Get all logs from the TaskScheduler and print them
local logs = TaskScheduler:GetLogs()
for i, log in ipairs(logs) do
print("Log " .. i .. ": " .. log.Message .. " (Time: " .. log.Time .. ")")
end
-- Example of how to cancel or pause tasks in the future
-- TaskScheduler:CancelTask("LoadPlayerData") -- Example: cancel task "LoadPlayerData"
-- TaskScheduler:PauseAllTasks() -- Example: pause all tasks
-- TaskScheduler:ResumeAllTasks() -- Example: resume all paused tasks
Last updated