module Marmot

Overview

Marmot is a non concurrent scheduler.

Marmot schedules tasks on three possibles ways:

Tasks are all executed on the same fiber. This means two things: first, you don't have to worry about concurrency (your tasks can share objects which does not support concurrency, like HTTP::Client), and second, they must not block (too much). If you want to execute jobs concurrently, you must spawn a new fiber inside your tasks.

A task receive a unique parameter which is an object representing itself. It can be canceled with Marmot::Task#cancel, from inside or outside the task. A canceled task can never be started again.

Tasks do not start when created. Instead, the main entrypoint is Marmot.run, which blocks while there are tasks to run. If there is no tasks to run, or they are all canceled, it stops.

The blocking behavior can also be stopped by calling Marmot.stop. As Marmot.run is blocking, you probably want to call Marmot.stop from a task or from another fiber.

When stopped, the tasks are not canceled and they will run again if Marmot.run is called again. To cancel all the tasks there is Marmot.cancel_all_tasks.

If the computer's clock changes, the tasks scheduled on a specific time will not be scheduled again. Their next runs will be triggered at the time before the clock changes, but the next ones will be correctly scheduled.

Extended Modules

Defined in:

log.cr
marmot.cr
tasks.cr

Constant Summary

Log = ::Log.for(self)
VERSION = "0.3.0"

Instance Method Summary

Instance Method Detail

def at(time : Time, &block : Callback) : Task #

Runs a task once at a given time.


[View source]
def cancel_all_tasks : Nil #

Cancels all the tasks.


[View source]
def every(span : Time::Span, first_run = false, &block : Callback) : Task #

Runs a task every given span.

If first run is true, it will run as soon as the scheduler runs. Else it will wait span time before running for first time.


[View source]
def every(span : Symbol, *, day = 1, hour = 0, minute = 0, second = 0, &block : Callback) : Task #

Runs a task every span at the given day, hour, minute and second.

Marmot.every(:hour, hour: 16, minute: 30, second: 30)  # will run every hour at 30:30 (the hour parameter is ignored)
Marmot.every(:day, hour: 15) { ... }  # will run every day at 15:00:00
Marmot.every(:month, day: 15) { ... } # will run every month at midnight
Marmot.every(:month, day: 31) { ... } # will run every month THAT HAVE a 31th day at midnight

[View source]
def on(channel, &block : Callback) : Task #

Runs a task when a value is received on a channel.

To access the value, you need to restrict the type of the task, and use OnChannelTask#value.

channel = Channel(Int32).new
Marmot.on(channel) { |task| puts task.as(OnChannelTask).value }

[View source]
def run : Nil #

Starts scheduling the tasks.

This blocks until #stop is called or all tasks are cancelled.


[View source]
def stop #

Stops scheduling the tasks.


[View source]