From 2cb5947cfb287a83469221faf662113c646fccf9 Mon Sep 17 00:00:00 2001
From: Andriy Zavada <johnhommer@gmail.com>
Date: Mon, 9 Dec 2024 18:28:48 +0200
Subject: [PATCH] aae_schedulenextrebuild/2

---
 src/aae_controller.erl | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/src/aae_controller.erl b/src/aae_controller.erl
index c85fa01..824517d 100644
--- a/src/aae_controller.erl
+++ b/src/aae_controller.erl
@@ -27,6 +27,7 @@
             aae_start/7,
             aae_start/8,
             aae_nextrebuild/1,
+            aae_schedulenextrebuild/2,
             aae_put/7,
             aae_close/1,
             aae_destroy/1,
@@ -210,6 +211,13 @@ aae_start(
 aae_nextrebuild(Pid) ->
     gen_server:call(Pid, rebuild_time, ?SYNC_TIMEOUT).
 
+-spec aae_schedulenextrebuild(pid(), non_neg_integer()) -> ok.
+%% @doc
+%% Schedule the next keystore rebuild, assuming last rebuild
+%% occurred now + specified delay
+aae_schedulenextrebuild(Pid, Delay) ->
+    gen_server:call(Pid, {schedule_nextrebuild, Delay}, ?SYNC_TIMEOUT).
+
 -spec aae_put(pid(), responsible_preflist(), 
                             aae_keystore:bucket(), aae_keystore:key(),
                             version_vector(), version_vector(),
@@ -578,6 +586,11 @@ init([Opts]) ->
 
 handle_call(rebuild_time, _From, State) ->  
     {reply, State#state.next_rebuild, State};
+handle_call({schedule_rebuild, Delay}, _From, State) ->
+    {Mega, Sec, Micros} = os:timestamp(),
+    Next = schedule_rebuild({Mega, Sec + Delay, Micros},
+                            State#state.rebuild_schedule),
+    {reply, ok, State#state{next_rebuild = Next}};
 handle_call(close, _From, State) ->
     ok = maybe_flush_puts(State#state.key_store, 
                             State#state.objectspecs_queue,