Deploying Common Lisp Web Applications

Question:

I am wondering how one goes about deploying a Common Lisp web application written in, say, Hunchentoot, Wookie, Woo, or even Clack.

That is, suppose I write an app that contains some files, packages, etc. Typically when I am working locally, I simply run a command in REPL that starts the server and then visit it using localhost:8000 or something like that.

However, I am a bit puzzled as to what the process is for deploying an app to a production server like AWS EC2. In what form should I deploy the Lisp code? Are there different options? What happens if the server needs to be restarted or is experiencing problems?

Answer:

I’ve figured something out lately by building self-contained executables for web apps and I wrote about it on lisp-journey/web-dev (shipping and deployment sections), as well as for the building part on the Common Lisp Cookbook/scripting#for-web-apps.

I copy the interesting parts here, there’s a bit more on each resource. Edits are welcome, primarily on those resources thanks !

edit july 2019: I contributed a page on the Cookbook: https://lispcookbook.github.io/cl-cookbook/web.html

edit: see also a list of tools and platforms that provide professional CL support: https://github.com/CodyReichert/awesome-cl#deployment

(edited) How to run the web app as a script

I explain below how to build and run executables, but we can of course run the application as a script. In a lisp file, say run.lisp, ensure:

  • to load your project’s asd file: (load "my-project.asd")
  • to load its dependencies: (ql:quickload :my-project)
  • to call its main function: (my-project:start) (given start is an exported symbol, otherwise ::start).

In doing so, the application starts and gives you back a Lisp REPL. You can interact with the running application. You can update it and even install new Quicklisp libraries as it runs.

How to build a self-contained executable

See also https://github.com/CodyReichert/awesome-cl#interfaces-to-other-package-managers for bindings to Homebrew and Debian packages.

With SBCL

How to build (self-contained) executables is implementation-specific (see
below Buildapp and Rowsell). With SBCL, as says
its documentation,
it is a matter of:

sb-ext is an SBCL extension to run external processes. See other
SBCL extensions
(many of them are made implementation-portable in other libraries).

:executable t tells to build an executable instead of an
image. We could build an image to save the state of our current
Lisp image, to come back working with it later. Specially useful if
we made a lot of work that is computing intensive.

If you try to run this in Slime, you’ll get an error about threads running:

Cannot save core with multiple threads running.

Run the command from a simple SBCL repl.

I suppose your project has Quicklisp dependencies. You must then:

  • ensure Quicklisp is installed and loaded at Lisp startup (you
    completed Quicklisp installation)
  • load the project’s .asd
  • install dependencies
  • build the executable.

That gives:

From the command line, or from a Makefile, use --load and --eval:

With ASDF

Now that we’seen the basics, we need a portable method. Since its
version 3.1, ASDF allows to do that. It introduces the make command,
that reads parameters from the .asd. Add this to your .asd declaration:

and call asdf:make :my-system.

So, in a Makefile:

With Roswell or Buildapp

Roswell, an implementation manager and much
more, also has the ros build command, that should work for many
implementations.

We can also make our app installable with Roswell by a ros install my-app. See its documentation.

We’ll finish with a word on
Buildapp, a battle-tested and
still popular “application for SBCL or CCL that configures and saves
an executable Common Lisp image”.

Many applications use it (for example,
pgloader), it is available on
Debian: apt install buildapp, but you shouldn’t need it now with asdf:make or Roswell.

For web apps

We can similarly build a self-contained executable for our web-app. It
would thus contain a web server and would be able to run on the
command line:

Note that this runs the production webserver, not a development one,
so we can run the binary on our VPS right away and access the app from
outside.

We have one thing to take care of, it is to find and put the thread of
the running web server on the foreground. In our main function, we
can do something like this:

We used the bordeaux-threads library ((ql:quickload "bordeaux-threads"), alias bt) and uiop, which is part of ASDF so
already loaded, in order to exit in a portable way (uiop:quit, with
an optional return code, instead of sb-ext:quit).

Parsing command line arguments

see the Cookbook here. TLDR; use uiop:command-line-arguments to get a list of the arguments. To parse them for real, there are libraries.

Deployment

Straightforward with an executable. The web app is visible from the outside right away.

On Heroku

See this buildpack.

Daemonizing, restarting in case of crashes, handling logs

See how to do that on your system.

Most GNU/Linux distros now come with Systemd.

Examples search result:

It is as simple as writing a configuration file:

running a command to start it:

a command to check its status:

and Systemd can handle logging (we write to stdout or stderr, it writes logs):

and it handles crashes and restarts the app:

and it can start the app after a reboot:

to enable it:

Debugging SBCL error: ensure_space: failed to allocate n bytes

If you get this error with SBCL on your server:

then disable ASLR:

Connecting to a remote Swank server

Little example here: http://cvberry.com/tech_writings/howtos/remotely_modifying_a_running_program_using_swank.html.

Demo project here: https://lisp-journey.gitlab.io/blog/i-realized-that-to-live-reload-my-web-app-is-easy-and-convenient/

It defines a simple function that prints forever:

On our server, we run it with

we do port forwarding on our development machine:

this will securely forward port 4006 on the server at example.com to
our local computer’s port 4006 (swanks accepts connections from
localhost).

We connect to the running swank with M-x slime-connect, typing in
port 4006.

We can write new code:

and eval it as usual with M-x slime-eval-region for instance. The output should change.

There are more pointers on CV Berry’s page.

Hot reload

Example with Quickutil. See notes on lisp-journey.

It has to be run on the server (a simple fabfile command can call this
through ssh). Beforehand, a fab update has run git pull on the
server, so new code is present but not running. It connects to the
local swank server, loads the new code, stops and starts the app in a
row.

Continuous Integration, continuous delivery of executables, Docker

See https://lispcookbook.github.io/cl-cookbook/testing.html#continuous-integration

Leave a Reply