taskqueue

Types

NoTaskError = object of CatchableError
  taskQueue: TaskQueue
Action = proc () {...}{.gcsafe.}
TaskQueue = ref object
  active: bool
  queue: HeapQueue[Task]
  now*: proc (): Timestamp {...}{.gcsafe.}

Procs

proc isRunning(q: TaskQueue): bool {...}{.raises: [], tags: [].}
proc newTaskQueue(): TaskQueue {...}{.raises: [], tags: [].}
Create a new Task Scheduler
proc runAt(q: TaskQueue; time: Timestamp; action: Action) {...}{.raises: [], tags: [].}
Schedule a task to run at time
proc len(q: TaskQueue): int {...}{.raises: [], tags: [].}
Number of task on queue
proc nextTaskTime(q: TaskQueue): Timestamp {...}{.raises: [NoTaskError], tags: [].}
Get the executing time of the next task raise NoTaskError if q is empty
proc process(q: TaskQueue; time: Timestamp) {...}{.raises: [Exception], tags: [RootEffect].}

Run all tasks that is scheduled on or before time.

Tasks will be processed in the order of scheduled time. In case two tasks are scheduled on the same time, the order of processing is undefined.

This is useful to control task processing manually.

proc process(q: TaskQueue) {...}{.inline, raises: [Exception], tags: [RootEffect].}
Equivalent to q.process(q.now())
proc stop(q: TaskQueue) {...}{.raises: [], tags: [].}
Stop poll() or exec()
proc poll(q: TaskQueue; interval: int = 16): Future[void] {...}{.
    raises: [Exception, OSError, FutureError], tags: [RootEffect].}

Run process() for every interval until stop()

Internally uses addTimer in asyncdispatch.

proc exec(q: TaskQueue) {...}{.raises: [Exception], tags: [RootEffect].}

Run process() in a while loop until stop() is called.

This procedure blocks, run in other thread if needed.

Templates

template runEvery(q: TaskQueue; firstTime: Timestamp; interval: Timespan; body: untyped)
Process task at firstTime and then schedule the next one at t = firstTime + i * interval where i is the smallest whole number such that t is larger than current time.
template runAround(q: TaskQueue; firstTime: Timestamp; timespan: Timespan;
                  body: untyped)
Process task at firstTime and then schedule the next one timespan later than current time.