Running a Clojure function periodically

I was working on collecting stats about our API servers, and needed to connect them to some kind of a visualization system. The idea, of course, is that measurement drives all future optimizations and improvements, so we need to be able to quickly see what was going on in our processes. 

We settled on using clojure-metrics to do the actual data collection from within our code, and then sending it all to the excellent Librato service for monitoring. 

One thing I wanted was to send a snapshot of all collected metrics every 30 seconds. For this, I had a function called report-all-metrics that I essentially needed to run every 30 seconds. It would collect everything from the metrics registry, and then connect to the Librato API, and send everything over. It would be trivial to write this in a custom way in Clojure, by wrapping it in another function that recursively calls itself after sleeping for the desired duration.

However, I figured I’d wrap ScheduledThreadPoolExecutor from the java.util.concurrent package and get the benefits of the runtime managing this for me instead. I ended up with a function called run-thunk-periodically which does essentially what I described earlier. Here’s the code:

Here it is in action:

And the output looks like this:

The idea is that while it works as expected, when there is an exception thrown, it tells you what is going on in the logs. Also, the thread-pool name is set appropriately, so you can identify the threads in a profiler.

Hope this is useful to someone!

One thought on “Running a Clojure function periodically

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s