Skip to content

Commit 1a17096

Browse files
committed
Stop server on SIGTERM by default
Fix #174
1 parent af3c9bf commit 1a17096

File tree

3 files changed

+8
-11
lines changed

3 files changed

+8
-11
lines changed

example/z-playground/server/playground.ml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -479,12 +479,6 @@ let rec gc ?(initial = true) () =
479479
let () =
480480
Dream.log "Starting playground";
481481

482-
(* Stop when systemd sends SIGTERM. *)
483-
let stop, signal_stop = Lwt.wait () in
484-
Lwt_unix.on_signal Sys.sigterm (fun _signal ->
485-
Lwt.wakeup_later signal_stop ())
486-
|> ignore;
487-
488482
(* Build the base image. *)
489483
Lwt_main.run begin
490484
Lwt_io.(with_file ~mode:Output "Dockerfile" (fun channel ->
@@ -520,7 +514,7 @@ let () =
520514
Dream.html (Client.html example)
521515
in
522516

523-
Dream.run ~interface:"0.0.0.0" ~port:80 ~stop ~adjust_terminal:false
517+
Dream.run ~interface:"0.0.0.0" ~port:80 ~adjust_terminal:false
524518
@@ Dream.logger
525519
@@ Dream.router [
526520

src/dream.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2129,7 +2129,7 @@ val run :
21292129
- [~stop] is a promise that causes the server to stop accepting new
21302130
requests, and {!Dream.run} to return. Requests that have already entered
21312131
the Web application continue to be processed. The default value is a
2132-
promise that never resolves.
2132+
promise that resolves when the [TERM] signal is received.
21332133
- [~error_handler] handles all errors, both from the application, and
21342134
low-level errors. See {!section-errors} and example
21352135
{{:https://github.com/aantron/dream/tree/master/example/9-error#folders-and-files}

src/http/http.ml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,10 @@ let serve_with_maybe_https
668668

669669
let default_interface = "localhost"
670670
let default_port = 8080
671-
let never = fst (Lwt.wait ())
671+
let on_sigterm =
672+
let promise, resolve = Lwt.wait () in
673+
ignore (Lwt_unix.on_signal Sys.sigterm (fun _ -> Lwt.wakeup_later signal_stop ()));
674+
promise
672675

673676
let network ~port ~socket_path =
674677
match socket_path with
@@ -679,7 +682,7 @@ let serve
679682
?(interface = default_interface)
680683
?(port = default_port)
681684
?socket_path
682-
?(stop = never)
685+
?(stop = on_sigterm)
683686
?(error_handler = Error_handler.default)
684687
?(tls = false)
685688
?certificate_file
@@ -707,7 +710,7 @@ let run
707710
?(interface = default_interface)
708711
?(port = default_port)
709712
?socket_path
710-
?(stop = never)
713+
?(stop = on_sigterm)
711714
?(error_handler = Error_handler.default)
712715
?(tls = false)
713716
?certificate_file

0 commit comments

Comments
 (0)