Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
Introduce 'couch_epi:register_service/1'
COUCHDB-2796
  • Loading branch information
iilyak committed Sep 2, 2015
1 parent d7eabf6 commit fa1260878f631d17119d3be788a19479dd982364
Showing 5 changed files with 42 additions and 1 deletion.
@@ -14,6 +14,7 @@

%% subscribtion management
-export([subscribe/5, unsubscribe/1, get_handle/1]).
-export([register_service/1]).

%% queries and introspection
-export([
@@ -164,3 +165,16 @@ all(Handle, ServiceId, Function, Args, Opts) ->

is_configured(Handle, Function, Arity) ->
[] /= couch_epi_functions_gen:modules(Handle, Function, Arity).


-spec register_service({ServiceId :: service_id(), Key :: key()}) -> ok;
(ServiceId :: service_id()) -> ok.

register_service({_ServiceId, _Key} = EPIKey) ->
register_service(couch_epi_data_gen, EPIKey);
register_service(ServiceId) when is_atom(ServiceId) ->
register_service(couch_epi_functions_gen, ServiceId).

register_service(Codegen, Key) ->
Handle = Codegen:get_handle(Key),
couch_epi_module_keeper:register_service(Codegen, Handle).
@@ -185,6 +185,11 @@ is_updated(Handle, Source, Data) ->
throw({Class, {Source, Reason}})
end.

save(Handle, undefined, []) ->
case get_current_data(Handle) of
[] -> generate(Handle, []);
_Else -> ok
end;
save(Handle, Source, Data) ->
CurrentData = get_current_data(Handle),
NewDefs = lists:keystore(Source, 1, CurrentData, {Source, Data}),
@@ -201,6 +201,11 @@ is_updated(Handle, Source, Modules) ->
throw({Class, {Source, Reason}})
end.

save(Handle, undefined, []) ->
case get_current_definitions(Handle) of
[] -> generate(Handle, []);
_Else -> ok
end;
save(Handle, Source, Modules) ->
CurrentDefs = get_current_definitions(Handle),
Definitions = definitions(Source, Modules),
@@ -19,6 +19,8 @@
%% ------------------------------------------------------------------

-export([maybe_start_keeper/2]).
-export([register_service/2]).

-export([start_link/2, save/3]).
-export([stop/1]).

@@ -35,8 +37,17 @@
%% API Function Definitions
%% ------------------------------------------------------------------

register_service(Codegen, Module) ->
{ok, Server} = maybe_start_keeper(Codegen, Module),
compile_dummy_module(Server).

maybe_start_keeper(Codegen, Module) ->
catch couch_epi_keeper_sup:start_child(Codegen, Module).
case couch_epi_keeper_sup:start_child(Codegen, Module) of
{ok, Pid} ->
{ok, Pid};
{error, {already_started, Pid}} ->
{ok, Pid}
end.

start_link(Codegen, Module) ->
gen_server:start_link({local, Module}, ?MODULE, [Codegen, Module], []).
@@ -76,3 +87,6 @@ code_change(_OldVsn, State, _Extra) ->
%% ------------------------------------------------------------------
%% Internal Function Definitions
%% ------------------------------------------------------------------

compile_dummy_module(Server) ->
save(Server, undefined, []).
@@ -91,6 +91,7 @@ setup(couch_epi_data_source) ->
test_app, {epi_key, Key}, {file, File}, [{interval, 100}]),
ok = couch_epi_data_source:wait(Pid),
KV = state_storage(),
ok = couch_epi:register_service(Key),
#ctx{
file = File,
key = Key,
@@ -108,6 +109,7 @@ setup(couch_epi_data) ->
test_app, {epi_key, Key}, provider, []),
ok = couch_epi_data:wait(Pid),
KV = state_storage(),
ok = couch_epi:register_service(Key),
#ctx{
key = Key,
handle = couch_epi:get_handle(Key),
@@ -126,6 +128,7 @@ setup(couch_epi_functions) ->
[{interval, 100}]),
ok = couch_epi_functions:wait(Pid),
KV = state_storage(),
ok = couch_epi:register_service(Key),
#ctx{
key = Key,
handle = couch_epi:get_handle(Key),

0 comments on commit fa12608

Please sign in to comment.