~ubuntu-branches/debian/wheezy/couchdb/wheezy

« back to all changes in this revision

Viewing changes to src/CouchDB/couch_db_update_notifier.erl

  • Committer: Bazaar Package Importer
  • Author(s): Noah Slater
  • Date: 2008-02-06 17:03:38 UTC
  • Revision ID: james.westby@ubuntu.com-20080206170338-y411anylx3oplqid
Tags: upstream-0.7.3~svn684
ImportĀ upstreamĀ versionĀ 0.7.3~svn684

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
%   Copyright 2007, 2008 Damien Katz <damien_katz@yahoo.com>
 
2
%
 
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
 
6
%
 
7
%       http://www.apache.org/licenses/LICENSE-2.0
 
8
%
 
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.
 
14
 
 
15
%
 
16
% This causes an OS process to spawned and it is notified every time a database is updated.
 
17
%
 
18
% The notifications are in the form of a the database name sent as a line of text to the OS processes
 
19
% stdout.
 
20
%
 
21
 
 
22
-module(couch_db_update_notifier).
 
23
 
 
24
-behaviour(gen_event).
 
25
 
 
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]).
 
28
 
 
29
-define(ERR_HANDLE, {Port, {exit_status, Status}} -> {stop, {unknown_error, Status}, {unknown_error, Status}, Port}).
 
30
 
 
31
start_link(Exec) ->
 
32
    couch_event_sup:start_link(couch_db_update, {couch_db_update_notifier, make_ref()}, Exec).
 
33
 
 
34
notify_all(DbName) ->
 
35
    gen_event:notify(couch_db_update, {db_updated, DbName}).
 
36
 
 
37
stop(Pid) ->
 
38
    couch_event_sup:stop(Pid).
 
39
 
 
40
init(Exec) ->
 
41
    Port = open_port({spawn, Exec}, [stream, exit_status, hide]),
 
42
    {ok, Port}.
 
43
 
 
44
terminate(_Reason, _Port) ->
 
45
    ok.
 
46
 
 
47
handle_event({db_updated, DbName}, Port) ->
 
48
    %% send a notification
 
49
    true = port_command(Port, DbName ++ "\n"),
 
50
    {ok, Port}.
 
51
 
 
52
handle_call(_Request, State) ->
 
53
    {ok, ok, State}.
 
54
 
 
55
handle_info({'EXIT', _, _Reason}, _Port) ->
 
56
    remove_handler.
 
57
 
 
58
code_change(_OldVsn, State, _Extra) ->
 
59
    {ok, State}.