1
% Copyright 2007, 2008 Damien Katz <damien_katz@yahoo.com>
3
% Licensed under the Apache License, Version 2.0 (the "License");
4
% you may not use this file except in compliance with the License.
5
% You may obtain a copy of the License at
7
% http://www.apache.org/licenses/LICENSE-2.0
9
% Unless required by applicable law or agreed to in writing, software
10
% distributed under the License is distributed on an "AS IS" BASIS,
11
% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
% See the License for the specific language governing permissions and
13
% limitations under the License.
16
% This causes an OS process to spawned and it is notified every time a database is updated.
18
% The notifications are in the form of a the database name sent as a line of text to the OS processes
22
-module(couch_db_update_notifier).
24
-behaviour(gen_event).
26
-export([start_link/1, notify_all/1]).
27
-export([init/1, terminate/2, handle_event/2, handle_call/2, handle_info/2, code_change/3,stop/1]).
29
-define(ERR_HANDLE, {Port, {exit_status, Status}} -> {stop, {unknown_error, Status}, {unknown_error, Status}, Port}).
32
couch_event_sup:start_link(couch_db_update, {couch_db_update_notifier, make_ref()}, Exec).
35
gen_event:notify(couch_db_update, {db_updated, DbName}).
38
couch_event_sup:stop(Pid).
41
Port = open_port({spawn, Exec}, [stream, exit_status, hide]),
44
terminate(_Reason, _Port) ->
47
handle_event({db_updated, DbName}, Port) ->
48
%% send a notification
49
true = port_command(Port, DbName ++ "\n"),
52
handle_call(_Request, State) ->
55
handle_info({'EXIT', _, _Reason}, _Port) ->
58
code_change(_OldVsn, State, _Extra) ->