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

jiahuili430 pushed a commit to branch improve-clouseau-rpc
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit 5a96b73ab0e9d62805a0607fba37623fccdc64c0
Author: Jiahui Li <[email protected]>
AuthorDate: Fri May 23 10:05:50 2025 -0500

    Improve `clouseau_rpc.erl`
    
    Add `clouseau_rpc:services/0` to check if clouseau services are alive.
---
 src/dreyfus/src/clouseau_rpc.erl | 35 ++++++++++++++++++++++++++++++++++-
 1 file changed, 34 insertions(+), 1 deletion(-)

diff --git a/src/dreyfus/src/clouseau_rpc.erl b/src/dreyfus/src/clouseau_rpc.erl
index 12520da9a..2778297ff 100644
--- a/src/dreyfus/src/clouseau_rpc.erl
+++ b/src/dreyfus/src/clouseau_rpc.erl
@@ -22,7 +22,7 @@
 -export([delete/2, update/3, cleanup/1, cleanup/2, rename/1]).
 -export([analyze/2, version/0, disk_size/1]).
 -export([set_purge_seq/2, get_purge_seq/1, get_root_dir/0]).
--export([connected/0]).
+-export([connected/0, services/0]).
 
 %% string represented as binary
 -type string_as_binary(_Value) :: nonempty_binary().
@@ -64,6 +64,8 @@
         | {string_as_binary(stopwords), [field_name()]}
     ].
 
+-define(TIMEOUT, 200).
+
 -spec open_index(Peer :: pid(), Path :: shard(), Analyzer :: analyzer()) ->
     {ok, indexer_pid()} | error().
 open_index(Peer, Path, Analyzer) ->
@@ -316,3 +318,34 @@ rpc(Ref, Msg) ->
 
 clouseau() ->
     list_to_atom(config:get("dreyfus", "name", "[email protected]")).
+
+-spec services() -> ok | error().
+services() ->
+    Services = [sup, main, analyzer, cleanup],
+    case connected() of
+        true ->
+            {ok, <<Major, _/binary>>} = version(),
+            case Major of
+                50 ->
+                    AliveServices = [is_alive(S) || S <- Services],
+                    io:fwrite("Clouseau 2.x Services: ~p~n", [AliveServices]);
+                51 ->
+                    AliveServices = [is_alive(S) || S <- [init | Services]],
+                    io:fwrite("Clouseau 3.x Services: ~p~n", [AliveServices])
+            end;
+        false ->
+            throw("Clouseau is not connected yet!")
+    end.
+
+-spec is_alive(Service :: atom()) -> boolean().
+%% clouseau 2.x: Service: sup | main | cleanup | analyzer.
+%% clouseau 3.x: Service: init | sup | main | cleanup | analyzer.
+is_alive(Service) ->
+    Ref = make_ref(),
+    {Service, clouseau()} ! {ping, self(), Ref},
+    receive
+        {pong, Ref} ->
+            {Service, pong}
+    after ?TIMEOUT ->
+        {Service, timeout}
+    end.

Reply via email to