You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by ei...@apache.org on 2019/11/22 15:51:34 UTC

[couchdb] 01/01: Add a new end-point /_ready

This is an automated email from the ASF dual-hosted git repository.

eiri pushed a commit to branch prototype/readiness-end-point
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit 3ea420ab1e087a5a7217a4e0a92517aeb1a9ae36
Author: Eric Avdey <ei...@eiri.ca>
AuthorDate: Mon Aug 26 16:54:38 2019 -0300

    Add a new end-point /_ready
    
    Add a new end-point /_ready similar to /_up,
    but with added check for a fdb backend availability.
---
 src/chttpd/src/chttpd_auth_request.erl   |  2 ++
 src/chttpd/src/chttpd_httpd_handlers.erl |  1 +
 src/chttpd/src/chttpd_misc.erl           | 14 ++++++++++++++
 test/elixir/test/basics_test.exs         |  6 ++++++
 4 files changed, 23 insertions(+)

diff --git a/src/chttpd/src/chttpd_auth_request.erl b/src/chttpd/src/chttpd_auth_request.erl
index 26fee17..d4ae060 100644
--- a/src/chttpd/src/chttpd_auth_request.erl
+++ b/src/chttpd/src/chttpd_auth_request.erl
@@ -84,6 +84,8 @@ authorize_request_int(#httpd{path_parts=[_DbName|_]}=Req) ->
 
 server_authorization_check(#httpd{path_parts=[<<"_up">>]}=Req) ->
     Req;
+server_authorization_check(#httpd{path_parts=[<<"_ready">>]}=Req) ->
+    Req;
 server_authorization_check(#httpd{path_parts=[<<"_uuids">>]}=Req) ->
     Req;
 server_authorization_check(#httpd{path_parts=[<<"_session">>]}=Req) ->
diff --git a/src/chttpd/src/chttpd_httpd_handlers.erl b/src/chttpd/src/chttpd_httpd_handlers.erl
index 7c657f5..c4b7f4c 100644
--- a/src/chttpd/src/chttpd_httpd_handlers.erl
+++ b/src/chttpd/src/chttpd_httpd_handlers.erl
@@ -30,6 +30,7 @@ url_handler(<<"_replicate">>)      -> fun chttpd_misc:handle_replicate_req/1;
 url_handler(<<"_uuids">>)          -> fun chttpd_misc:handle_uuids_req/1;
 url_handler(<<"_session">>)        -> fun chttpd_auth:handle_session_req/1;
 url_handler(<<"_up">>)             -> fun chttpd_misc:handle_up_req/1;
+url_handler(<<"_ready">>)          -> fun chttpd_misc:handle_ready_req/1;
 url_handler(_) -> no_match.
 
 db_handler(<<"_view_cleanup">>) -> fun chttpd_db:handle_view_cleanup_req/2;
diff --git a/src/chttpd/src/chttpd_misc.erl b/src/chttpd/src/chttpd_misc.erl
index 11d2c5b..57a15f0 100644
--- a/src/chttpd/src/chttpd_misc.erl
+++ b/src/chttpd/src/chttpd_misc.erl
@@ -23,6 +23,7 @@
     handle_system_req/1,
     handle_task_status_req/1,
     handle_up_req/1,
+    handle_ready_req/1,
     handle_utils_dir_req/1,
     handle_utils_dir_req/2,
     handle_uuids_req/1,
@@ -510,6 +511,19 @@ handle_up_req(#httpd{method='GET'} = Req) ->
 handle_up_req(Req) ->
     send_method_not_allowed(Req, "GET,HEAD").
 
+
+handle_ready_req(#httpd{method='GET'} = Req) ->
+    try
+        fabric2_db:list_dbs([{limit, 0}]),
+        send_json(Req, 200, {[{status, ready}]})
+    catch error:{timeout, _} ->
+        send_json(Req, 404, {[{status, unavailable}]})
+    end;
+
+handle_ready_req(Req) ->
+    send_method_not_allowed(Req, "GET,HEAD").
+
+
 message_queues(Registered) ->
     lists:map(fun(Name) ->
         Type = message_queue_len,
diff --git a/test/elixir/test/basics_test.exs b/test/elixir/test/basics_test.exs
index dda2a0c..4e78ed3 100644
--- a/test/elixir/test/basics_test.exs
+++ b/test/elixir/test/basics_test.exs
@@ -18,6 +18,12 @@ defmodule BasicsTest do
     assert Couch.get("/").body["couchdb"] == "Welcome", "Should say welcome"
   end
 
+  test "Ready endpoint" do
+    resp = Couch.get("/_ready")
+    assert resp.status_code == 200
+    assert resp.body["status"] == "ready"
+  end
+
   @tag :with_db
   test "PUT on existing DB should return 412 instead of 500", context do
     db_name = context[:db_name]