我最近通过为 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 中,使用--load
and --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