“Universal” Dune Tip: Rebuild Stuff, Sometimes

Quick hack to make “at most every n seconds” dune targets.

How to make something happen less often …

My use case is a web-page that is built by dune using the current status of my Notmuch inbox, with a few shell commands notmuch search --format=json … one can build a nice summary. But I don't want each rebuild to waste a few hundred milliseconds recomputing the same thing over and over.

It would be nice to be able to to have a “witness” of changes to the notmuch database but this doesn't seem obvious since the database is changed in various ways, some of which are also very latency-sensitive (Emacs keybindings).

Enter the (universe) special dependency, it means “always rebuild” (i.e. that our universe is changing quite often).

Let's use it to rebuild something all the time, something that builds very quickly and produces a different output only once every n seconds. And make our more expensive process depend on it, its rebuilding will be stopped there since the dependency produces the exact same result

Here it is:

 (targets every-60-seconds)
 (deps (universe))
  (run bash -c "echo $(( $(date +%s) / 60 )) > every-60-seconds")))

 (targets notmuch-status.data)
 (deps every-60-seconds)
  (run ./make-notmuch-data.sh notmuch-status.data)))

every-60-seconds is built all the time but takes an imperceptible amount of time.

notmuch-status.data actually takes some time, and then triggers the rebuild of all its dependents.

After 8 years of blograstination, this is post #9 of my attempt at not getting too fast lagging behind on the #100DaysToOffload “challenge” … Let's see where this goes.