This library provides a simple task manager that allows you to schedule tasks to be executed immediately or after a specified delay. It uses libevent for efficient event handling and supports cooperative multitasking by allowing tasks to yield control back to the event loop, ensuring that long-running tasks do not block the execution of other tasks.
Types
ScheduledTask = object
Task = object id*: string ## An optional identifier for the task, useful for debugging or tracking purposes. work*: TaskProc ## The procedure to execute for this task. The `TaskProc` will run in a threadpool ## to allow cooperative multitasking. Tasks should yield control back to the event loop if they
TaskManager = ref object base*: ptr event_base ## The libevent base used for managing events and the event loop.
TaskProc = proc () {....gcsafe, nimcall.}
- A procedure type representing the work to be done for a task. This is a simple no-argument procedure that will be executed when the task runs
Procs
proc close(manager: var TaskManager) {....raises: [], tags: [], forbids: [].}
- Cleans up resources used by the TaskManager. This should be called after stopping the manager to free resources.
proc halt(manager: TaskManager; delay: Duration): bool {....raises: [], tags: [], forbids: [].}
- Schedules the manager to stop after a specified delay. This is a convenient way to automatically stop the manager after a certain amount of time has passed. Returns false if the manager is nil or already stopping.
proc newTaskManager(tickMs = 10): TaskManager {....raises: [CatchableError], tags: [], forbids: [].}
- Initializes the TaskManager. This sets up the libevent base and a periodic timer event that checks for scheduled tasks and executes pending tasks. The tickMs parameter controls how often the manager checks for due tasks (default is 10ms).
proc run(manager: TaskManager) {....raises: [], tags: [], forbids: [].}
- Starts the event loop to process tasks. This will block until the manager is stopped. Each task run in a threadpool to allow cooperative multitasking.
proc stop(manager: TaskManager) {....raises: [], tags: [], forbids: [].}
- Signals the manager to stop. This will cause the event loop to exit after the current tick completes.
proc submitNewTask(manager: TaskManager; id: string; work: TaskProc): bool {. discardable, ...raises: [], tags: [], forbids: [].}
- A convenience overload for submitting a new task without needing to create a Task object first.
proc submitRepeatingTask(manager: TaskManager; every: Duration; task: Task; startIn: Option[Duration] = none(Duration)): bool {. ...raises: [], tags: [TimeEffect], forbids: [].}
-
Schedules a repeating task that runs at a specified interval. The first execution can be delayed by startIn, which defaults to the same as every if not provided or non-positive.
- every: repeat interval (must be > 0)
- startIn: optional first delay; if <= 0, uses every
proc submitTask(manager: TaskManager; delay: Duration; task: Task; repeatTask = false) {....raises: [], tags: [TimeEffect], forbids: [].}
- Schedules a one-shot task after delay. If repeatTask is true, the task will be rescheduled to run repeatedly at the specified interval.
proc submitTask(manager: TaskManager; task: Task): bool {....raises: [], tags: [], forbids: [].}
- Submits a task for immediate execution on the next tick. Returns false if the manager is stopping or nil.