Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
Add OS process listeners
We don't use this in dbcore but if we ever merge this back to CouchDB
then we'll want to have this feature.
  • Loading branch information
davisp authored and rnewson committed Jul 30, 2014
1 parent 7e6c02c commit 2e0f5438d34b884733808495e7f648e2573958e7
Showing 3 changed files with 155 additions and 0 deletions.
@@ -0,0 +1,75 @@
% 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(couch_event_os_listener).
-behavior(gen_server).


-export([
start_link/0
]).

-export([
init/1,
terminate/2,
handle_call/3,
handle_cast/2,
handle_info/2,
code_change/3
]).


start_link(Exe) when is_list(Exe) ->
gen_server:start_link(?MODULE, Exe, []).


init(Exe) ->
process_flag(trap_exit, true),
ok = couch_event:register_all(self()),
couch_os_process:start_link(Exe, []).


terminate(_Reason, Pid) when is_pid(Pid) ->
couch_os_process:stop(Pid);
terminate(_Reason, _Pid) ->
ok.


handle_call(Msg, From, Pid) ->
couch_log:notice("~s ignoring call ~w from ~w", [?MODULE, Msg, From]),
{reply, ignored, Pid, 0}.


handle_cast(Msg, Pid) ->
couch_log:notice("~s ignoring cast ~w", [?MODULE, Msg]),
{noreply, Pid, 0}.


handle_info({'$couch_event', DbName, Event}, Pid) ->
Obj = {[
{db, DbName},
{type, list_to_binary(atom_to_list(Event))}
]},
ok = couch_os_process:send(Pid, Obj),
{noreply, Pid};

handle_info({'EXIT', Pid, Reason}, Pid) ->
couch_log:error("Update notificatio process ~w died: ~w", [Pid, Reason]),
{stop, normal, nil};

handle_info(Msg, Pid) ->
couch_log:notice("~s ignoring info ~w", [?MODULE, Msg]),
{noreply, Pid, 0}.


code_change(_OldVsn, St, _Extra) ->
{ok, St}.
@@ -0,0 +1,73 @@
% 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.


% This causes an OS process to spawned and it is notified every time a database
% is updated.
%
% The notifications are in the form of a the database name sent as a line of
% text to the OS processes stdout.


-module(couch_event_os_sup).
-behaviour(supervisor).
-behaviour(config_listener).


-export([
start_link/0,
init/1
]).

-export([
handle_config_change/5
]).


start_link() ->
supervisor:start_link({local, ?MODULE}, ?MODULE, []).


init([]) ->
ok = config:listen_for_changes(?MODULE, nil),

UpdateNotifierExes = config:get("update_notification"),
Children = [child(Id, Exe) || {Id, Exe} <- UpdateNotifierExes],

{ok, {
{one_for_one, 10, 3600},
Children
}.


handle_config_change("update_notification", Id, deleted, _, _) ->
supervisor:terminate_child(?MODULE, Id),
supervisor:delete_child(?MODULE, Id),
{ok, nil};
handle_config_change("update_notification", Id, Exe, _, _) when is_list(Exe) ->
supervisor:terminate_child(?MODULE, Id),
supervisor:delete_child(?MODULE, Id),
supervisor:start_child(?MODULE, child(Id, Exe)),
{ok, nil};
handle_config_change(_, _, _, _, _) ->
{ok, nil}.


child(Id, Arg) ->
{
Id,
{couch_event_os_listener, start_link, [Arg]},
permanent,
1000,
supervisor,
[couch_event_os_listener]
}.
@@ -45,6 +45,13 @@ init(_) ->
5000,
worker,
[couch_event_dist]
},
{couch_event_os_sup,
{couch_event_os_sup, start_link, []},
permanent,
5000,
supervisor,
[couch_event_os_sup]
}
],
{ok, {{one_for_one, 5, 10}, Children}}.

0 comments on commit 2e0f543

Please sign in to comment.