This repository has been archived by the owner. It is now read-only.
Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
3 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -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}. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -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] | ||
}. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters