15

我想知道如何部署一个用 Hunchentoot、Wookie、Woo 甚至 Clack 编写的 Common Lisp Web 应用程序。

也就是说,假设我编写了一个包含一些文件、包等的应用程序。通常,当我在本地工作时,我只需在 REPL 中运行一个命令来启动服务器,然后使用localhost:8000或类似的东西访问它。

但是,对于将应用程序部署到 AWS EC2 等生产服务器的过程是什么,我感到有些困惑。我应该以什么形式部署 Lisp 代码?有不同的选择吗?如果服务器需要重新启动或遇到问题会怎样?

4

3 回答 3

23

我最近通过为 Web 应用程序构建自包含的可执行文件找到了一些东西,我在lisp-journey/web-dev(运输和部署部分)以及Common Lisp Cookbook/scripting上的构建部分写了一些东西#for-web-apps

我在这里复制有趣的部分,每个资源都有更多内容。欢迎编辑,主要是在这些资源上谢谢!

2019 年 7 月编辑:我在 Cookbook 上贡献了一个页面:https ://lispcookbook.github.io/cl-cookbook/web.html

编辑:另请参阅提供专业 CL 支持的工具和平台列表:https ://github.com/CodyReichert/awesome-cl#deployment

(已编辑)如何将 Web 应用程序作为脚本运行

我在下面解释如何构建和运行可执行文件,但我们当然可以将应用程序作为脚本运行。在一个 lisp 文件中,比如说run.lisp,确保:

  • 加载项目的 asd 文件:(load "my-project.asd")
  • 加载其依赖项:(ql:quickload :my-project)
  • 调用它的主要功能:((my-project:start)给定start的是一个导出的符号,否则::start)。

这样做时,应用程序启动并返回一个 Lisp REPL。您可以与正在运行的应用程序进行交互。您可以更新它,甚至在它运行时安装新的 Quicklisp 库。

如何构建一个独立的可执行文件

另请参阅https://github.com/CodyReichert/awesome-cl#interfaces-to-other-package-managers以了解与 Homebrew 和 Debian 软件包的绑定。

带 SBCL

如何构建(自包含)可执行文件是特定于实现的(见下文 Buildapp 和 Rowsell)。使用 SBCL, 如其文档所述,这是一个问题:

(sb-ext:save-lisp-and-die #P"path/name-of-executable" :toplevel #'my-app:main-function :executable t)

sb-ext是运行外部进程的 SBCL 扩展。查看其他 SBCL 扩展 (其中许多在其他库中实现可移植)。

:executable t 告诉构建可执行文件而不是图像。我们可以构建一个图像来保存当前 Lisp 图像的状态,以便稍后使用它。如果我们进行了大量计算密集型工作,则特别有用。

如果你尝试在 Slime 中运行它,你会得到一个关于线程运行的错误:

运行多个线程时无法保存核心。

从一个简单的 SBCL repl 运行命令。

我想你的项目有 Quicklisp 依赖项。那么您必须:

  • 确保在 Lisp 启动时安装并加载 Quicklisp(您已完成 Quicklisp 安装)
  • load项目的 .asd
  • 安装依赖项
  • 构建可执行文件。

这给出了:

(load "my-app.asd")
(ql:quickload :my-app)
(sb-ext:save-lisp-and-die #p"my-app-binary" :toplevel #'my-app:main :executable t)

在命令行或 Makefile 中,使用--loadand --eval

build:
    sbcl --non-interactive \
         --load my-app.asd \
         --eval '(ql:quickload :my-app)' \
         --eval "(sb-ext:save-lisp-and-die #p\"my-app\" :toplevel #my-app:main :executable t)"

与 ASDF

现在我们已经了解了基础知识,我们需要一个可移植的方法。自 3.1 版以来,ASDF 允许这样做。它引入了从 .asd 读取参数的make命令。将此添加到您的 .asd 声明中:

:build-operation "program-op" ;; leave as is
:build-pathname "<binary-name>"
:entry-point "<my-system:main-function>"

并打电话asdf:make :my-system

因此,在 Makefile 中:

LISP ?= sbcl

build:
    $(LISP) --non-interactive \
        --load my-app.asd \
        --eval '(ql:quickload :my-app)' \
        --eval '(asdf:make :my-system)' 

使用 Roswell 或 Buildapp

Roswell,一个实施经理等等,也有ros build命令,应该适用于许多实施。

我们还可以通过ros install my-app. 请参阅其文档。

最后,我们将介绍 Buildapp,这是一个久经考验且仍然流行的“用于配置和保存可执行 Common Lisp 图像的 SBCL 或 CCL 应用程序”。

许多应用程序都使用它(例如, pgloader),它在 Debian: 上可用apt install buildapp,但您现在不应该使用 asdf:make 或 Roswell 需要它。

对于网络应用

我们可以类似地为我们的网络应用程序构建一个独立的可执行文件。因此它将包含一个 Web 服务器,并且能够在命令行上运行:

$ ./my-web-app
Hunchentoot server is started.
Listening on localhost:9003.

请注意,这运行的是生产网络服务器,而不是开发网络服务器,因此我们可以立即在我们的 VPS 上运行二进制文件并从外部访问应用程序。

我们要处理一件事,就是找到正在运行的 Web 服务器的线程并将其置于前台。在我们的main函数中,我们可以这样做:

(defun main ()
  (start-app :port 9003) ;; our start-app, for example clack:clack-up
  ;; let the webserver run.
  ;; warning: hardcoded "hunchentoot".
  (handler-case (bt:join-thread (find-if (lambda (th)
                                            (search "hunchentoot" (bt:thread-name th)))
                                         (bt:all-threads)))
    ;; Catch a user's C-c
    (#+sbcl sb-sys:interactive-interrupt
      #+ccl  ccl:interrupt-signal-condition
      #+clisp system::simple-interrupt-condition
      #+ecl ext:interactive-interrupt
      #+allegro excl:interrupt-signal
      () (progn
           (format *error-output* "Aborting.~&")
           (clack:stop *server*)
           (uiop:quit)))
    (error (c) (format t "Woops, an unknown error occured:~&~a~&" c))))

我们使用bordeaux-threads库 ( (ql:quickload "bordeaux-threads"), 别名bt) 和uiop,它是已经加载的 ASDF 的一部分,以便以可移植的方式退出 ( uiop:quit,带有可选的返回码,而不是sb-ext:quit)。

解析命令行参数

请参阅此处的食谱。TLDR;用于uiop:command-line-arguments获取参数列表。为了真正解析它们,有库。

部署

直接带有可执行文件。该网络应用程序立即从外部可见。

在 Heroku 上

请参阅此 buildpack

守护进程,在崩溃时重新启动,处理日志

了解如何在您的系统上执行此操作。

大多数 GNU/Linux 发行版现在都带有 Systemd。

示例搜索结果:

就像编写配置文件一样简单:

# /etc/systemd/system/my-app.service
[Unit]
Description=stupid simple example

[Service]
WorkingDirectory=/path/to/your/app
ExecStart=/usr/local/bin/sthg sthg
Type=simple
Restart=always
RestartSec=10

运行命令来启动它:

sudo systemctl start my-app.service

检查其状态的命令:

systemctl status my-app.service

Systemd 可以处理日志记录(我们写入 stdout 或 stderr,它会写入日志):

journalctl -f -u my-app.service

它处理崩溃并重新启动应用程序

Restart=always

它可以在重新启动后启动应用程序

[Install]
WantedBy=basic.target

启用它:

sudo systemctl enable my-app.service

调试 SBCL 错误:ensure_space: failed to allocate n bytes

如果您在服务器上使用 SBCL 收到此错误:

mmap: wanted 1040384 bytes at 0x20000000, actually mapped at 0x715fa2145000
ensure_space: failed to allocate 1040384 bytes at 0x20000000
(hint: Try "ulimit -a"; maybe you should increase memory limits.)

然后禁用ASLR

sudo bash -c "echo 0 > /proc/sys/kernel/randomize_va_space"

连接到远程 Swank 服务器

这里的小例子:http: //cvberry.com/tech_writings/howtos/remotely_modifying_a_running_program_using_swank.html

演示项目:https ://lisp-journey.gitlab.io/blog/i-realized-that-to-live-reload-my-web-app-is-easy-and-convenient/

它定义了一个永远打印的简单函数:

;; a little common lisp swank demo
;; while this program is running, you can connect to it from another terminal or machine
;; and change the definition of doprint to print something else out!
;; (ql:quickload :swank)
;; (ql:quickload :bordeaux-threads)

(require :swank)
(require :bordeaux-threads)

(defparameter *counter* 0)

(defun dostuff ()
  (format t "hello world ~a!~%" *counter*))

(defun runner ()
  (bt:make-thread (lambda ()
                    (swank:create-server :port 4006)))
  (format t "we are past go!~%")
  (loop while t do
       (sleep 5)
       (dostuff)
       (incf *counter*)
       ))

(runner)

在我们的服务器上,我们运行它

sbcl --load demo.lisp

我们在我们的开发机器上进行端口转发:

ssh -L4006:127.0.0.1:4006 username@example.com

这将安全地将 example.com 服务器上的端口 4006 转发到我们本地计算机的端口 4006(swanks 接受来自 localhost 的连接)。

我们使用 连接到正在运行的 swank M-x slime-connect,输入端口 4006。

我们可以编写新代码:

(defun dostuff ()
  (format t "goodbye world ~a!~%" *counter*))
(setf *counter* 0)

并像往常一样评估它,M-x slime-eval-region例如。输出应该改变。

CV Berry 的页面上有更多指针。

热重载

Quickutil 为例。请参阅有关 lisp-journey 的注释。

它必须在服务器上运行(一个简单的 fabfile 命令可以通过 ssh 调用它)。之前,afab update已经git pull在服务器上运行,所以新代码存在但没有运行。它连接到本地 swank 服务器,加载新代码,连续停止和启动应用程序。

持续集成、持续交付可执行文件、Docker

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

于 2018-01-05T10:06:14.820 回答
6

要在生产环境中运行 lisp 映像,您可以使用 lisp 代码生成 fasl 文件:

(compile-file "app.lisp")

通过调用 sbcl 运行生成的 .fas 文件。

sbcl --noinform \
     --load app.fas \
     --eval "(defun main (argv) (declare (ignore argv)) (hunchentoot:start (make-instance 'hunchentoot:easy-acceptor :port 4242)))"
于 2018-01-04T21:06:26.013 回答
4

我找到了一个博客,其中包含一个解决方案,我已经适应了我对 linux 机器上的生产系统的需求。不幸的是,我再也找不到对该博客的引用,所以我只能向您展示我的解决方案,它是针对 CCL 的(而原始解决方案是针对 SBCL 的),我更熟悉它。这是启动系统的程序:

(require 'swank)
(require 'hunchentoot)

(defparameter *httpd-port* 9090)     ; The port Hunchentoot will be listening on
(defparameter *shutdown-port* 6700)  ; The port CCL will be listening for shutdown
                                     ; this port is the same used in /etc/init.d/hunchentoot
(defparameter *swank-port* 5016)     ; The port used for remote interaction with slime

;; Start the Swank server
(defparameter *swank-server*
  (swank:create-server :port *swank-port* :dont-close t))

(require 'YOUR-PACKAGE)
(YOUR-PACKAGE:YOUR-STARTING-FUNCTION)

(princ "Hunchentoot started on port ")
(princ *httpd-port*)
(terpri)

(let* ((socket (make-socket :connect :passive :local-host "127.0.0.1" :local-port *shutdown-port* :reuse-address t))
       (stream (accept-connection socket)))
  (close stream)
  (close socket))

(print "Stopping Hunchentoot...")
(YOUR-PACKAGE:YOUR-STOPPING-FUNCTION)

(dolist (proc (all-processes))
  (unless (equal proc *current-process*)
    (process-kill proc)))
(sleep 1)
(quit)

这个想法是,您可以通过指定 swank 使用的端口来使用 slime 连接到正在运行的系统。我使用了几次,例如动态更改数据库链接,并且对这种可能性的力量印象深刻。

正在运行的系统可以通过以下方式终止:

telnet 127.0.0.1 6700

并由以下内容发起:

nohup ccl -l initcclserver.lisp >& server.out &

在以前版本的脚本中,我找到了特定于 SBCL 的部分,因此如果您使用它,您可以修改脚本。

接受终止连接:

(sb-bsd-sockets:socket-bind socket #(127 0 0 1) *shutdown-port*)
(sb-bsd-sockets:socket-listen socket 1)
(multiple-value-bind (client-socket addr port)
  (sb-bsd-sockets:socket-accept socket)
(sb-bsd-sockets:socket-close client-socket)
(sb-bsd-sockets:socket-close socket)))

要关闭系统:

(dolist (thread (sb-thread:list-all-threads))
  (unless (equal sb-thread:*current-thread* thread)
    (sb-thread:terminate-thread thread)))
(sleep 1)
(sb-ext:quit)

希望这能有所帮助。

于 2018-01-05T08:15:45.280 回答