Handle migrations and server startup better

The server part of the guix-data-service doesn't work great as a guix service,
since it often fails to start if the migrations take any time at all.

To address this, start the server before running the migrations, and serve the
pages that work without the database, plus a general 503 response. Once the
migrations have completed, switch to the normal behaviour.
This commit is contained in:
Christopher Baines 2022-06-17 12:55:05 +01:00
parent d19eb07138
commit 8e23d38660
4 changed files with 249 additions and 194 deletions

View file

@ -24,12 +24,22 @@
#:use-module (web request)
#:use-module (web uri)
#:use-module (system repl error-handling)
#:use-module (ice-9 atomic)
#:use-module (fibers web server)
#:use-module (guix-data-service web controller)
#:use-module (guix-data-service web util)
#:export (start-guix-data-service-web-server))
(define (handler request body controller secret-key-base)
(define (check-startup-completed startup-completed)
(if (atomic-box-ref startup-completed)
(begin
;; Just in case this atomic-box-ref is expensive, only do it when
;; necessary
(set! check-startup-completed (const #t))
#t)
#f))
(define (handler request body controller secret-key-base startup-completed)
(display
(format #f "~a ~a\n"
(request-method request)
@ -42,14 +52,17 @@
request-components)
mime-types
body
secret-key-base))))
secret-key-base
(check-startup-completed startup-completed)))))
(define* (start-guix-data-service-web-server port host secret-key-base)
(define* (start-guix-data-service-web-server port host secret-key-base
startup-completed)
(call-with-error-handling
(lambda ()
(run-server (lambda (request body)
(handler request body controller
secret-key-base))
secret-key-base
startup-completed))
#:host host
#:port port))
#:on-error 'backtrace