You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@couchdb.apache.org by GitBox <gi...@apache.org> on 2020/05/01 14:49:52 UTC

[GitHub] [couchdb] nickva commented on a change in pull request #2857: Background database deletion

nickva commented on a change in pull request #2857:
URL: https://github.com/apache/couchdb/pull/2857#discussion_r418326689



##########
File path: src/fabric/src/fabric2_db_expiration.erl
##########
@@ -0,0 +1,218 @@
+% Licensed under the Apache License, Version 2.0 (the "License"); you may not
+% use this file except in compliance with the License. You may obtain a copy of
+% the License at
+%
+%   http://www.apache.org/licenses/LICENSE-2.0
+%
+% Unless required by applicable law or agreed to in writing, software
+% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+% License for the specific language governing permissions and limitations under
+% the License.
+
+-module(fabric2_db_expiration).
+
+
+-behaviour(gen_server).
+
+
+-export([
+    start_link/0
+]).
+
+-export([
+    init/1,
+    handle_call/3,
+    handle_cast/2,
+    handle_info/2,
+    terminate/2,
+    code_change/3
+]).
+
+
+-include_lib("couch/include/couch_db.hrl").
+-include_lib("fabric/include/fabric2.hrl").
+
+
+start_link() ->
+    gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
+
+
+init(_) ->
+    start_timer(),
+    {ok, nil}.
+
+
+terminate(_M, _St) ->
+    ok.
+
+
+handle_call(Msg, _From, St) ->
+    {stop, {bad_call, Msg}, {bad_call, Msg}, St}.
+
+
+handle_cast(timeout, St) ->
+    proc_lib:init_ack({ok, self()}),
+    try
+        couch_jobs:set_type_timeout(?DB_EXPIRATION_JOB_TYPE, 6),
+        case add_or_get_job() of
+            ok -> ok;
+            retry -> add_or_get_job()
+        end,
+        case run_loop() of
+            ok -> ok;
+            retry -> run_loop()
+        end,
+        {noreply, St}
+    catch
+        _:_->
+            start_timer(),
+            {noreply, St}
+    end;
+handle_cast(Msg, St) ->
+    {stop, {bad_cast, Msg}, St}.
+
+
+handle_info(Msg, St) ->
+    {stop, {bad_info, Msg}, St}.
+
+
+code_change(_OldVsn, St, _Extra) ->
+    {ok, St}.
+
+
+start_timer() ->
+    After = 10,
+    case timer:apply_after(After, gen_server, cast, [self(), timeout]) of
+        {ok, Ref} ->
+            Ref;
+        _Error ->
+            nil
+    end.
+
+
+add_or_get_job() ->
+    couch_jobs:set_type_timeout(?DB_EXPIRATION_JOB_TYPE, 6),

Review comment:
       For checking couch_jobs application is running we could use this trick as well:
   
   ```
    lists:keysearch(couch_jobs, 1, application:which_applications()).
   ```
   
   The contract in the `which_applications/0` says that it returns running applications. One thing to check first would be if we start returning the application while it is still initializing or only after it finished and is all ready to go. The last option would be idea. So then before add_or_get_job() we'd add something like "wait_for_couch_jobs_app()" loop  perhaps.
   
   I checked this by adding a sleep in one of the init function in fabric2 and then ran `application:which_applications()` and it didn't appear there while it was still initializing. So we should be good using this




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org