You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by rn...@apache.org on 2014/08/01 11:10:08 UTC

[12/35] git commit: Add OS process listeners

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.


Project: http://git-wip-us.apache.org/repos/asf/couchdb-couch-event/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-couch-event/commit/2e0f5438
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-couch-event/tree/2e0f5438
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-couch-event/diff/2e0f5438

Branch: refs/heads/windsor-merge
Commit: 2e0f5438d34b884733808495e7f648e2573958e7
Parents: 7e6c02c
Author: Paul J. Davis <pa...@gmail.com>
Authored: Wed Apr 24 12:24:13 2013 -0500
Committer: Robert Newson <rn...@apache.org>
Committed: Wed Jul 30 17:40:48 2014 +0100

----------------------------------------------------------------------
 src/couch_event_os_listener.erl | 75 ++++++++++++++++++++++++++++++++++++
 src/couch_event_os_sup.erl      | 73 +++++++++++++++++++++++++++++++++++
 src/couch_event_sup2.erl        |  7 ++++
 3 files changed, 155 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-couch-event/blob/2e0f5438/src/couch_event_os_listener.erl
----------------------------------------------------------------------
diff --git a/src/couch_event_os_listener.erl b/src/couch_event_os_listener.erl
new file mode 100644
index 0000000..b2014ed
--- /dev/null
+++ b/src/couch_event_os_listener.erl
@@ -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}.

http://git-wip-us.apache.org/repos/asf/couchdb-couch-event/blob/2e0f5438/src/couch_event_os_sup.erl
----------------------------------------------------------------------
diff --git a/src/couch_event_os_sup.erl b/src/couch_event_os_sup.erl
new file mode 100644
index 0000000..ff4daaa
--- /dev/null
+++ b/src/couch_event_os_sup.erl
@@ -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]
+    }.

http://git-wip-us.apache.org/repos/asf/couchdb-couch-event/blob/2e0f5438/src/couch_event_sup2.erl
----------------------------------------------------------------------
diff --git a/src/couch_event_sup2.erl b/src/couch_event_sup2.erl
index e7387e0..1a74979 100644
--- a/src/couch_event_sup2.erl
+++ b/src/couch_event_sup2.erl
@@ -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}}.