You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by da...@apache.org on 2014/01/18 00:07:49 UTC
[02/12] git commit: some fixes
some fixes
Project: http://git-wip-us.apache.org/repos/asf/couchdb-couch-dbupdates/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-couch-dbupdates/commit/10a49bb2
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-couch-dbupdates/tree/10a49bb2
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-couch-dbupdates/diff/10a49bb2
Branch: refs/heads/import
Commit: 10a49bb2998cf6b82fe0c99f3acbea1a17f0f474
Parents: c42f8fd
Author: benoitc <bc...@gmail.com>
Authored: Thu May 10 22:13:51 2012 +0200
Committer: benoitc <bc...@gmail.com>
Committed: Thu May 10 22:13:51 2012 +0200
----------------------------------------------------------------------
README.md | 26 ++++++++++++++++++++++++++
src/couch_dbupdates.app.src | 1 -
src/couch_dbupdates.erl | 14 +++++++++-----
src/couch_dbupdates_httpd.erl | 26 ++++++++++++--------------
4 files changed, 47 insertions(+), 20 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/couchdb-couch-dbupdates/blob/10a49bb2/README.md
----------------------------------------------------------------------
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..6d631e5
--- /dev/null
+++ b/README.md
@@ -0,0 +1,26 @@
+# couch_dbupdates
+
+`couch_dbupdates` is a simple couchdb modules to receive databases
+events in couchdb node.
+
+It's actually **supported by all the [refuge](http://refuge.io) projects**:
+
+- [refuge](https://github.com/refuge/refuge)
+- [rcouch](https://github.com/refuge/rcouch)
+- [rcouch_template](https://github.com/refuge/rcouch_template)
+
+
+## HTTP API
+
+To get db events, do a GET to `/_db_updates` .
+
+You can pass an optional query parameter `feed`. The feed can be
+`longpoll` (default) or `continuous`.
+
+
+## Example of usage
+
+ $ curl http://127.0.0.1:5984/_db_updates?feed=continuous
+ {"type":"created","db_name":"testdb"}
+ {"type":"updated","db_name":"testdb"}
+ {"type":"deleted","db_name":"testdb"}
http://git-wip-us.apache.org/repos/asf/couchdb-couch-dbupdates/blob/10a49bb2/src/couch_dbupdates.app.src
----------------------------------------------------------------------
diff --git a/src/couch_dbupdates.app.src b/src/couch_dbupdates.app.src
index 7df7f4a..f36a8d0 100644
--- a/src/couch_dbupdates.app.src
+++ b/src/couch_dbupdates.app.src
@@ -14,6 +14,5 @@
couch,
couch_httpd
]},
- {mod, { couch_dbupdates_app, []}},
{env, []}
]}.
http://git-wip-us.apache.org/repos/asf/couchdb-couch-dbupdates/blob/10a49bb2/src/couch_dbupdates.erl
----------------------------------------------------------------------
diff --git a/src/couch_dbupdates.erl b/src/couch_dbupdates.erl
index a085350..5bbdde5 100644
--- a/src/couch_dbupdates.erl
+++ b/src/couch_dbupdates.erl
@@ -5,18 +5,22 @@
handle_dbupdates(Fun, Acc) ->
NotifierPid = db_update_notifier(),
- Pid = spawn(fun() -> loop(NotifierPid, Fun, Acc) end),
- {ok, Pid}.
+ try
+ loop(Fun, Acc)
+ after
+ catch(couch_db_update_notifier:stop(NotifierPid))
+ end.
-loop(NotifierPid, Fun, Acc) ->
+loop(Fun, Acc) ->
receive
{db_updated, Event} ->
case Fun(Event, Acc) of
{ok, Acc1} ->
- loop(NotifierPid, Fun, Acc1);
+ loop(Fun, Acc1);
stop ->
- couch_db_update_notifier:stop(NotifierPid)
+ Fun(stop, Acc)
+
end
end.
http://git-wip-us.apache.org/repos/asf/couchdb-couch-dbupdates/blob/10a49bb2/src/couch_dbupdates_httpd.erl
----------------------------------------------------------------------
diff --git a/src/couch_dbupdates_httpd.erl b/src/couch_dbupdates_httpd.erl
index 8621cc8..671f77c 100644
--- a/src/couch_dbupdates_httpd.erl
+++ b/src/couch_dbupdates_httpd.erl
@@ -3,37 +3,35 @@
-export([handle_req/1]).
-include_lib("couch/include/couch_db.hrl").
+-include_lib("couch_httpd/include/couch_httpd.hrl").
-record(state, {resp, feed}).
handle_req(#httpd{method='GET'}=Req) ->
- {ok, Resp} = couch_httpd:start_json_response(Req, 200, []),
+ {ok, Resp} = couch_httpd:start_json_response(Req, 200),
Feed = proplists:get_value("feed", couch_httpd:qs(Req), "longpoll"),
State = #state{resp=Resp, feed=Feed},
- try
- {ok, Pid} = couch_dbupdates:handle_dbupdates(fun handle_update/2,
- State)
- after
- couch_httpd:end_json_response(Resp)
- end.
+ couch_dbupdates:handle_dbupdates(fun handle_update/2,
+ State).
handle_req(Req, _Db) ->
couch_httpd:send_method_not_allowed(Req, "GET").
+handle_update(stop, #state{resp=Resp}) ->
+ couch_httpd:end_json_response(Resp);
handle_update(Event, #state{resp=Resp, feed="continuous"}=State) ->
EventObj = event_obj(Event),
{ok, Resp1} = couch_httpd:send_chunk(Resp, [?JSON_ENCODE(EventObj) |
- "\n"]);
+ "\n"]),
{ok, State#state{resp=Resp1}};
-
-handle_update(Event, #state{resp=Resp, feed="longpoll"}=State) ->
- {EventProps} = even_obj(Event),
- JsonObj = {[{<<"ok">>, true} | EventProps]},
- {ok, Resp1} = couch_httpd:send_chunk(Resp, ?JSON_ENCODE(JsonObj)),
- {ok, State#state{resp=Resp1}}.
+handle_update(Event, #state{resp=Resp, feed="longpoll"}) ->
+ {Props} = event_obj(Event),
+ JsonObj = {[{<<"ok">>, true} | Props]},
+ couch_httpd:send_chunk(Resp, ?JSON_ENCODE(JsonObj)),
+ stop.
event_obj({Type, DbName}) ->
{[{<<"type">>, couch_util:to_binary(Type)},