7.1.483-stable
In this page
scheduler
@library("scheduler", "0.0.0");
A cron
-like task scheduler.
Useful for automatic background imports or simulations.
scheduler::add
Adds a new task to the scheduler. Returns the previous task that matches the same function if any.
Note that the previous task will be lost.
scheduler::add(scheduler::PeriodicTask { fn: my_task, every: duration });
scheduler::list
Returns the current list of scheduled tasks
var current_tasks = scheduler::list();
scheduler::find
Looks for a task that matches the given function
.
var found = scheduler::find(my_task);
if (found != null) {
// use scheduler::PeriodicTask
}
scheduler::activate
Tries to find a task that matches function
and activates it.
Returns true
if a task is found, false
otherwise.
if (scheduler::activate(my_task)) {
println("my_task successfully activated");
} else {
println("my_task does not exist");
}
scheduler::deactivate
Tries to find a task that matches function
and deactivates it.
Returns true
if a task is found, false
otherwise.
if (scheduler::deactivate(my_task)) {
println("my_task is not longer scheduled");
} else {
println("my_task is not in the periodic tasks list");
}
Example
Periodically calls https://api.open-meteo.com
to get the temperature in Luxembourg
. The data is aggregated into a nodeGeo
and a nodeTime
:
var cities: nodeGeo<nodeTime<float>>;
fn main() {
scheduler::add(
scheduler::PeriodicTask {
fn: fn () {
fetch_temperature(geo { 49.612, 6.119 });
},
every: 30minmin,
}
);
}
fn fetch_temperature(location: geo) {
var city = cities.get(location);
if (city == null) {
city = nodeTime<float> {};
cities.set(location, city);
}
var res = Http::get(
"https://api.open-meteo.com/v1/forecast?latitude=${location.lat()}&longitude=${location.lng()}¤t=temperature_2m&timezone=UTC",
null
);
var current = res.get("current");
if (current is Map) {
var timepoint = time::parse(current.get("time") as String, null);
var temperature = current.get("temperature_2m") as float;
city.setAt(timepoint, temperature);
}
}