Generated on 2009-08-09 21:22:56 with etap 0.3.4.
| Name | Total lines | Lines of code | Total coverage | Code coverage | ||||
| couch_external_manager | ?? | ?? | ?? |
|
....1 % Licensed under the Apache License, Version 2.0 (the "License"); you may not ....2 % use this file except in compliance with the License. You may obtain a copy of ....3 % the License at ....4 % ....5 % http://www.apache.org/licenses/LICENSE-2.0 ....6 % ....7 % Unless required by applicable law or agreed to in writing, software ....8 % distributed under the License is distributed on an "AS IS" BASIS, WITHOUT ....9 % WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the ...10 % License for the specific language governing permissions and limitations under ...11 % the License. ...12 ...13 -module(couch_external_manager). ...14 -behaviour(gen_server). ...15 ...16 -export([start_link/0, execute/2, config_change/2]). ...17 -export([init/1, terminate/2, code_change/3, handle_call/3, handle_cast/2, handle_info/2]). ...18 ...19 -include("couch_db.hrl"). ...20 ...21 start_link() -> ...22 gen_server:start_link({local, couch_external_manager}, ...23 couch_external_manager, [], []). ...24 ...25 execute(UrlName, JsonReq) -> ...26 Pid = gen_server:call(couch_external_manager, {get, UrlName}), ...27 case Pid of ...28 {error, Reason} -> ...29 Reason; ...30 _ -> ...31 couch_external_server:execute(Pid, JsonReq) ...32 end. ...33 ...34 config_change("external", UrlName) -> ...35 gen_server:call(couch_external_manager, {config, UrlName}). ...36 ...37 % gen_server API ...38 ...39 init([]) -> ...40 Handlers = ets:new(couch_external_manager_handlers, [set, private]), ...41 couch_config:register(fun config_change/2), ...42 {ok, Handlers}. ...43 ...44 terminate(_Reason, Handlers) -> ...45 ets:foldl(fun({_UrlName, Pid}, nil) -> ...46 couch_external_server:stop(Pid), ...47 nil ...48 end, nil, Handlers), ...49 ok. ...50 ...51 handle_call({get, UrlName}, _From, Handlers) -> ...52 case ets:lookup(Handlers, UrlName) of ...53 [] -> ...54 case couch_config:get("external", UrlName, nil) of ...55 nil -> ...56 Msg = lists:flatten( ...57 io_lib:format("No server configured for ~p.", [UrlName])), ...58 {reply, {error, {unknown_external_server, ?l2b(Msg)}}, Handlers}; ...59 Command -> ...60 {ok, NewPid} = couch_external_server:start_link(UrlName, Command), ...61 true = ets:insert(Handlers, {UrlName, NewPid}), ...62 {reply, NewPid, Handlers} ...63 end; ...64 [{UrlName, Pid}] -> ...65 {reply, Pid, Handlers} ...66 end; ...67 handle_call({config, UrlName}, _From, Handlers) -> ...68 % A newly added handler and a handler that had it's command ...69 % changed are treated exactly the same. ...70 ...71 % Shutdown the old handler. ...72 case ets:lookup(Handlers, UrlName) of ...73 [{UrlName, Pid}] -> ...74 couch_external_server:stop(Pid); ...75 [] -> ...76 ok ...77 end, ...78 % Wait for next request to boot the handler. ...79 {reply, ok, Handlers}. ...80 ...81 handle_cast(_Whatever, State) -> ...82 {noreply, State}. ...83 ...84 handle_info({'EXIT', Reason, Pid}, Handlers) -> ...85 ?LOG_DEBUG("EXTERNAL: Server ~p died. (reason: ~p)", [Pid, Reason]), ...86 % Remove Pid from the handlers table so we don't try closing ...87 % it a second time in terminate/2. ...88 ets:match_delete(Handlers, {'_', Pid}), ...89 {stop, Handlers}. ...90 ...91 code_change(_OldVsn, State, _Extra) -> ...92 {ok, State}. ...93
Generated using etap 0.3.4.