Skip to content

Tasks — Keeping Summaries Fresh

Outcome: a Flux script saved on the server that runs itself every minute and keeps a second bucket filled with ten-minute means — verified by reading the summaries it wrote while nobody was watching. Transcript: influxdb-lab/transcripts/t10-tasks.txt.

The idea: queries that run themselves

Chapter 5's windowed average was an answer you compute when you ask. A task turns a question into a standing arrangement: a Flux script plus a schedule, stored on the server, run by the server, whose results are written — usually into another bucket. This is downsampling, the standard shape of a long-lived InfluxDB: raw points live briefly in one bucket (chapter 6's retention), while their means live for years in another, at a fraction of the size. Nobody has to remember to run anything; the _tasks system bucket from chapter 3 is where the server keeps the diary of these runs.

The script

A task is an ordinary Flux query with two additions: an option task line at the top declaring its name and schedule, and a to() at the end saying where results go. The lab's script, kept in the lab as influxdb-lab/task.flux:

option task = {name: "weather-10m-means", every: 1m}

from(bucket: "weather")
    |> range(start: -1h)
    |> filter(fn: (r) => r._field == "temperature")
    |> aggregateWindow(every: 10m, fn: mean, createEmpty: false)
    |> to(bucket: "weather-hourly")

One minute is a lab's schedule, chosen so the result can be witnessed; a real deployment would say every: 1h and aggregate over the hour just finished. The destination bucket was created first, empty, with infinite retention:

influx bucket create --name weather-hourly
influx task create --file task.flux
influx task ls
ID          Name            Organization        Status  Every
119146ce1c40e000    weather-10m-means   example-org     active      1m

The proof

About two minutes later, with no further commands, the destination bucket held one row per station per ten-minute window:

garden-1   02:30   17.2     rooftop-1  02:30   18.1
garden-1   02:40   17.5     rooftop-1  02:40   18.4
garden-1   02:50   17.9     rooftop-1  02:50   18.9
garden-1   03:00   18.4     rooftop-1  03:00   19.6
garden-1   03:10   19.1     rooftop-1  03:10   20.3
garden-1   03:19   19.8     rooftop-1  03:19   21.0

Because the fixture's readings stand ten minutes apart, each window holds exactly one reading and each "mean" is that reading — which is what makes the verification honest: the task's output is the fixture, reshaped, window by window, plus the third station (http-1, one row) that chapter 8's single point contributed. The last window is stamped 03:19 rather than 03:20 for chapter 5's familiar reason: each run's range ends at the run's own moment. Rewriting the same window on the next run is safe by chapter 4's rule — a window's summary has the same series and timestamp each time, so the new value overwrites the old instead of doubling it.

Working rules

  • Tasks are code. Keep the script in a file (the lab did), version it, and treat task create as a deploy.
  • A task that fails says so in its runs, not at your terminal: task ls shows status, and the run history lives with the task. Check it after any change, the way chapter 6 checks a write with a count.
  • Summaries are a one-way door. Once raw points expire, the means are all that remain — choose the window and the function (mean, max, last) as if you were choosing what the future is allowed to know.