From e5d420b2db9e4abfe5e5edfe12a38a08d78d214a Mon Sep 17 00:00:00 2001 From: Remco Bloemen Date: Mon, 7 Mar 2016 10:57:20 +0100 Subject: [PATCH 1/2] Gracefull exit on sigTERM Like the sigINT that was already handled, postgrest should gracefuly shut down on a sigTERM. This is a common way of stopping processes, used amongst others by docker. See: https://stackoverflow.com/questions/4042201/how-does-sigint-relate-to-the-other-termination-signals --- CHANGELOG.md | 1 + src/PostgREST/Main.hs | 9 +++++---- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 24b704c2b..60ca2bf52 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). ## Unreleased ### Fixed +- Terminate gracefully on SIGTERM - @recmo ## [0.3.1.0] - 2016-02-28 diff --git a/src/PostgREST/Main.hs b/src/PostgREST/Main.hs index f7a09fefc..68d3a12c1 100644 --- a/src/PostgREST/Main.hs +++ b/src/PostgREST/Main.hs @@ -60,10 +60,11 @@ main = do #ifndef mingw32_HOST_OS tid <- myThreadId - void $ installHandler keyboardSignal (Catch $ do - P.release pool - throwTo tid UserInterrupt - ) Nothing + forM_ [sigINT, sigTERM] $ \sig -> + void $ installHandler sig (Catch $ do + P.release pool + throwTo tid UserInterrupt + ) Nothing #endif result <- P.use pool $ do From 82c74ed21fb7bc459c8d273c96799ad4786dd081 Mon Sep 17 00:00:00 2001 From: Remco Bloemen Date: Sun, 6 Mar 2016 08:47:37 +0100 Subject: [PATCH 2/2] Use `CMD exec` in Dockerfile Without exec the `postgrest` process is not run with PID 1 (it is a child process of the shell that starts it). This means signals send to the docker (like `docker stop` or ^C) will not be handled correctly. However, Linux treats PID 1 as special and sets the SIGTERM handler to ignore by default. It is also necessary to install a SIGTERM handler. This commit adds `exec` to resolve this problem, as per the recommendation in the Dockerfile documentation: https://docs.docker.com/engine/reference/builder/#shell-form-entrypoint-example --- CHANGELOG.md | 2 +- Dockerfile | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 60ca2bf52..5bb3cdda8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). ## Unreleased ### Fixed -- Terminate gracefully on SIGTERM - @recmo +* Terminate gracefully on SIGTERM (for use in Docker) - @recmo ## [0.3.1.0] - 2016-02-28 diff --git a/Dockerfile b/Dockerfile index 6d046ba5a..3fc7efaba 100644 --- a/Dockerfile +++ b/Dockerfile @@ -16,7 +16,7 @@ RUN wget http://github.com/begriffs/postgrest/releases/download/v${POSTGREST_VER mv postgrest /usr/local/bin/postgrest && \ rm postgrest-${POSTGREST_VERSION}-ubuntu.tar.xz -CMD postgrest postgres://${PG_ENV_POSTGRES_USER}:${PG_ENV_POSTGRES_PASSWORD}@${PG_PORT_5432_TCP_ADDR}:${PG_PORT_5432_TCP_PORT}/${PG_ENV_POSTGRES_DB} \ +CMD exec postgrest postgres://${PG_ENV_POSTGRES_USER}:${PG_ENV_POSTGRES_PASSWORD}@${PG_PORT_5432_TCP_ADDR}:${PG_PORT_5432_TCP_PORT}/${PG_ENV_POSTGRES_DB} \ --port 3000 \ --schema ${POSTGREST_SCHEMA} \ --anonymous ${POSTGREST_ANONYMOUS} \