应用程序在Clozure CL中交付长期运行的应用程序

时间:2014-10-31 00:04:46

标签: common-lisp ccl toplevel

Application Delivery的所有基本示例都显示了如何用自己的函数替换顶层函数。完成该功能后,应用程序退出。我想知道为长时间运行的应用程序创建顶级函数的最佳方法是什么。我的代码是

(ql:quickload :my-app)

(defun main ()
  (swank:create-server :dont-close t)
  (my-app:start-server) ; Essentially creates a hunchentoot handler and returns
  (loop for x = (read-line)
     when (string= x "q") do (quit)
     do (format t "Type q to quit~%" x)))

(save-application "my-app" :toplevel-function #'main :prepend-kernel t)

有更好的方法吗?我不喜欢循环,但是释放终端的东西也没关系。

1 个答案:

答案 0 :(得分:2)

正如您所说,一旦完成主要功能,应用程序就会退出。因此,您需要保持该功能运行,直到您希望退出该应用程序。

最简单的解决方案是将主循环留在sleep

的无限循环中
(defun main ()
  (swank:create-server :dont-close t)
  (my-app:start-server)
  (loop (sleep 60)))

在启动Swank服务器时,您可能希望包含通过SLIME连接干净地退出应用程序的功能。例如,您可以使用bt-semaphore包编写类似以下的内容:

(defvar *quit-my-app* (bt-semaphore:make-semamphore))

(defun main ()
  (swank:create-server :dont-close t)
  (my-app:start-server)
  (bt-semaphore:wait-on-semaphore *quit-my-app*)
  (my-app:clean-up)) ; or whatever you need to do for cleaning up

(defun quit-my-app ()
  (bt-semaphore:signal-semaphore *quit-my-app*))

现在,您只需在SLIME连接上评估(quit-my-app)即可关闭应用程序。

您也可以使用主线程进行维护。在我的服务器中,我在那里执行简单的日志轮换:

(defun seconds-until-tomorrow ()
  (multiple-value-bind (second minute hour day month year daylight-p zone)
      (decode-universal-time (+ (get-universal-time) (* 60 60 26))) ; safely tomorrow
    (declare (ignore second minute hour daylight-p))
    (- (encode-universal-time 0 0 0 day month year zone)
       (get-universal-time))))

(defun main ()
  (swank:create-server :dont-close t)
  (let (cur-logfile
        cur-logfile-name
        ;; assuming that start-server returns the Hunchentoot acceptor
        (acpt (my-app:start-server)))
    (loop
       (let* ((lf-stem (log-file-name))
              (logfile-name (merge-pathnames lf-stem *temp-path*))
              (new-logfile (open logfile-name :direction :output 
                                              :if-exists :append 
                                              :if-does-not-exist :create)))

         (setf (hunchentoot:acceptor-message-log-destination acpt) new-logfile
               (hunchentoot:acceptor-access-log-destination acpt) new-logfile)

         (when cur-logfile
           (close cur-logfile)
           (run-program "/usr/bin/xz" (list (princ-to-string cur-logfile-name))))

         (setf cur-logfile new-logfile
               cur-logfile-name logfile-name)

         (when (bt-semaphore:wait-on-semaphore *quit-my-app* (seconds-until-tomorrow))
           (return)))))