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

rnewson pushed a commit to branch auto-reload-config
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit 4809d74cef006fccdd0f9f2405d67728b3ad007d
Author: Robert Newson <[email protected]>
AuthorDate: Tue Nov 25 15:47:27 2025 +0000

    auto-reload config on file change
---
 rel/overlay/etc/default.ini |  5 +++++
 src/config/src/config.erl   | 20 +++++++++++++++++++-
 2 files changed, 24 insertions(+), 1 deletion(-)

diff --git a/rel/overlay/etc/default.ini b/rel/overlay/etc/default.ini
index 66747fc88..1731e197f 100644
--- a/rel/overlay/etc/default.ini
+++ b/rel/overlay/etc/default.ini
@@ -1233,3 +1233,8 @@ url = {{nouveau_url}}
 ; from the _first_ authentication failure.
 ; note: changing this setting requires a couchdb restart.
 ;max_lifetime = 300000
+
+[config]
+; periodically reload configuration from file.
+; Leave undefined to disable this.
+;auto_reload_secs =
\ No newline at end of file
diff --git a/src/config/src/config.erl b/src/config/src/config.erl
index 70b8883f9..2b177f4e0 100644
--- a/src/config/src/config.erl
+++ b/src/config/src/config.erl
@@ -38,7 +38,7 @@
 -export([subscribe_for_changes/1]).
 
 -export([init/1]).
--export([handle_call/3, handle_cast/2]).
+-export([handle_call/3, handle_cast/2, handle_info/2]).
 -export([terminate/2]).
 
 -export([is_sensitive/2]).
@@ -294,6 +294,7 @@ init(IniFilesDirs) ->
     % correct node name and distribution mode.
     case check_distribution_mode() of
         ok ->
+            maybe_schedule_reload(),
             {ok, Config};
         {error, Msg} ->
             erlang:display(Msg),
@@ -314,6 +315,7 @@ handle_call({set, Sec, Key, Val, Opts}, _From, Config) ->
             {reply, {error, ValidationError}, Config};
         ok ->
             put_value(Sec, Key, Val),
+            maybe_schedule_reload(),
             couch_log:notice(
                 "~p: [~s] ~s set to ~s for reason ~p",
                 [?MODULE, Sec, Key, LogVal, Reason]
@@ -406,6 +408,13 @@ handle_call(reload, _From, #config{} = Config) ->
 handle_cast(_Msg, State) ->
     {noreply, State}.
 
+handle_info(reload, State0) ->
+    {reply, ok, State1} = handle_call(reload, nil, State0),
+    maybe_schedule_reload(),
+    {noreply, State1};
+handle_info(_Msg, State) ->
+    {noreply, State}.
+
 terminate(_Reason, _State) ->
     erase_all().
 
@@ -615,6 +624,15 @@ settings_fmap_fun({{?MODULE, ?SETTINGS, Sec, Key}, Val}) ->
 settings_fmap_fun(_) ->
     false.
 
+maybe_schedule_reload() ->
+    case get_integer("config", "auto_reload_secs", 0) of
+        0 ->
+            ignore;
+        AutoReloadSecs when AutoReloadSecs > 0 ->
+            AutoReloadMS = erlang:convert_time_unit(AutoReloadSecs, second, 
millisecond),
+            erlang:send_after(AutoReloadMS, ?MODULE, reload)
+    end.
+
 -ifdef(TEST).
 -include_lib("couch/include/couch_eunit.hrl").
 

Reply via email to