SBCL: Deploying Hunchentoot application as executable
Asked Answered
T

2

11

I started playing with SBCL Common Lisp and want to develop a small web application using Hunchentoot. For easy deployment I planned to save everything in a binary using sb-ext:save-lisp-and-die as I can live with the big output size.

For the executable you need to supply a toplevel function. The problem is that the program exits when the toplevel function returns. I tried to start Hunchentoot from the executable, but the program ended after two seconds.

How can I wait until Hunchentoot was shut down (from inside a request) before stopping the program? Can I do something like join the Hunchentoot acceptor thread? Or can I even include the REPL into the executable to be able to do live debugging?

Transoceanic answered 24/5, 2015 at 10:25 Comment(4)
Did you ever figure out how to deploy your app? I am in the same position now. I wrote a quick app and would like to deploy it. What tools did you use?Zoospore
@Zoospore I'm taking notes on that on lisp-journey/web-dev#deployment. Just can't say for sure since I couldn't run my app yet because of an sbcl error.Bourgeon
@Bourgeon great! I will keep my eye on it. By the way, I would be curious to get a list of Lisp resources that you have found useful on your Lisp journey. Suggestions for books, online articles, etc.Zoospore
The resources I found really useful are basically in the Good Resources tab, + CL Recipes by E. Weitz (highly recommended to have a large overview of practical things. Lacks modern tools and libraries though) + reading sources.Bourgeon
I
5
(ql:quickload :hunchentoot)
(use-package :hunchentoot)

(defun main ()
  (hunchentoot:start-server :port 8082)
  (sb-thread:join-thread (find-if
                          (lambda (th)
                            (string= (sb-thread:thread-name th) "hunchentoot-listener-1"))
                          (sb-thread:list-all-threads))))

No explicit code is required to give you access to a REPL if you keep a terminal open (perhaps via GNU Screen). Send Ctrl+C to the terminal to break into the debugger.

Impotent answered 24/5, 2015 at 15:6 Comment(4)
It seems like I am working with a newer version of Hunchentoot. I had to use (hunchentoot:start (make-instance 'hunchentoot:easy-acceptor :port 8080) and the thread was called "hunchentoot-listener-*:8080". Works like a charm.Transoceanic
how about (find-if (lambda (th) (search "hunchentoot-listener" (sb-thread:thread-name th))) (sb-thread:list-all-threads)) for the find-if partNakisha
or simply use --eval "(loop do (sleep 86400))" in function mainFavour
@PeterSlotko yeah, that seems better, because for me, it's not called hunchentoot-listener-1 but instead called hunchentoot-listener-*:4242, so using search instead of string= makes that piece of this answer less brittle. Good suggestion!Informality
S
1
;;; I simply use sleep to yield the main thread.
;;; To start the server while developing I use
;;; start-server.  For deployment I use the main
;;; function.

(defun start-server ()
  (hunchentoot:start
    (make-instance 'hunchentoot:easy-acceptor :port 8000)))

(defun main()
  (start-server)
  (sleep #xffffffff))

Salema answered 22/5, 2022 at 16:14 Comment(1)
This might be the best answer, because joining threads can be subject to a race condition. Also (sleep most-positive-fixnum).Bourgeon

© 2022 - 2024 — McMap. All rights reserved.