How do you deploy your Clojure applications, and how do you keep them running? In most recent and relevant Linux distributions, systemd is the default init system, so the easiest thing to do is create a systemd service to start your application as a service. You can find examples of that for the services running on my box hosting matthiasnehlsen.com and all of the related live examples in this repo.
With systemd, there's the concept of the watchdog. An application needs to issue
sd_notify
commands every so often, otherwise it's killed and restarted.
For more on the subject, I recommend this
blog post by Lennart Poettering,
one of systemd's original authors.
This library facilitates the required calls to systemd via JNA,
using the SDNotify library, but only if
the NOTIFY_SOCKET
environment variable is set. Otherwise, nothing happens, thus
there's no harm even if you're on an entirely different platform during development.
Import the library:
(:require [matthiasn.systemd-watchdog.core :as wd])
Start the watchdog notifier:
(wd/start-watchdog! 5000)
That's it.
You can have a look at my live examples, these are all supervised by systemd now.
The service files for all these projects are in the conf
repo, have a look at the watchdog configuration there, for example in
birdwatch.service
or in systems-toolbox-ws-latency.service
.
You can call the start-watchdog!
function in any kind of REPL-driven workflow
as often as you want, it's not going to do anything unless the NOTIFY_SOCKET
environment variable is set.
The most important building block here is the SDNotify Java library. This library is then called inside a minimal systems-toolbox system, which can be embedded in any Clojure application.
Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.