diff options
Diffstat (limited to 'src/couch_event/src/couch_event_listener.erl')
-rw-r--r-- | src/couch_event/src/couch_event_listener.erl | 76 |
1 files changed, 28 insertions, 48 deletions
diff --git a/src/couch_event/src/couch_event_listener.erl b/src/couch_event/src/couch_event_listener.erl index a9ed33199..40f1a5c65 100644 --- a/src/couch_event/src/couch_event_listener.erl +++ b/src/couch_event/src/couch_event_listener.erl @@ -12,7 +12,6 @@ -module(couch_event_listener). - -export([ start/3, start/4, @@ -27,13 +26,11 @@ loop/2 ]). - -record(st, { module, state }). - -callback init(Arg :: term()) -> term(). @@ -49,12 +46,10 @@ -callback handle_info(Message :: term(), State :: term()) -> term(). - start(Mod, Arg, Options) -> Pid = erlang:spawn(?MODULE, do_init, [Mod, Arg, Options]), {ok, Pid}. - start(Name, Mod, Arg, Options) -> case where(Name) of undefined -> @@ -63,12 +58,10 @@ start(Name, Mod, Arg, Options) -> {error, {already_started, Pid}} end. - start_link(Mod, Arg, Options) -> Pid = erlang:spawn_link(?MODULE, do_init, [Mod, Arg, Options]), {ok, Pid}. - start_link(Name, Mod, Arg, Options) -> case where(Name) of undefined -> @@ -77,30 +70,26 @@ start_link(Name, Mod, Arg, Options) -> {error, {already_started, Pid}} end. - enter_loop(Module, State, Options) -> ok = register_listeners(Options), - ?MODULE:loop(#st{module=Module, state=State}, infinity). - + ?MODULE:loop(#st{module = Module, state = State}, infinity). cast(Pid, Message) -> Pid ! {'$couch_event_cast', Message}, ok. - do_init(Module, Arg, Options) -> ok = maybe_name_process(Options), ok = register_listeners(Options), case (catch Module:init(Arg)) of {ok, State} -> - ?MODULE:loop(#st{module=Module, state=State}, infinity); + ?MODULE:loop(#st{module = Module, state = State}, infinity); {ok, State, Timeout} when is_integer(Timeout), Timeout >= 0 -> - ?MODULE:loop(#st{module=Module, state=State}, Timeout); + ?MODULE:loop(#st{module = Module, state = State}, Timeout); Else -> erlang:exit(Else) end. - loop(St, Timeout) -> receive {'$couch_event', DbName, Event} -> @@ -113,7 +102,6 @@ loop(St, Timeout) -> do_info(St, timeout) end. - maybe_name_process(Options) -> case proplists:lookup(name, Options) of {name, Name} -> @@ -127,7 +115,6 @@ maybe_name_process(Options) -> ok end. - register_listeners(Options) -> case get_all_dbnames(Options) of all_dbs -> @@ -137,85 +124,79 @@ register_listeners(Options) -> end, ok. - -do_event(#st{module=Module, state=State}=St, DbName, Event) -> +do_event(#st{module = Module, state = State} = St, DbName, Event) -> case (catch Module:handle_event(DbName, Event, State)) of {ok, NewState} -> - ?MODULE:loop(St#st{state=NewState}, infinity); + ?MODULE:loop(St#st{state = NewState}, infinity); {ok, NewState, Timeout} when is_integer(Timeout), Timeout >= 0 -> - ?MODULE:loop(St#st{state=NewState}, Timeout); + ?MODULE:loop(St#st{state = NewState}, Timeout); {stop, Reason, NewState} -> - do_terminate(Reason, St#st{state=NewState}); + do_terminate(Reason, St#st{state = NewState}); Else -> erlang:error(Else) end. - -do_cast(#st{module=Module, state=State}=St, Message) -> +do_cast(#st{module = Module, state = State} = St, Message) -> case (catch Module:handle_cast(Message, State)) of {ok, NewState} -> - ?MODULE:loop(St#st{state=NewState}, infinity); + ?MODULE:loop(St#st{state = NewState}, infinity); {ok, NewState, Timeout} when is_integer(Timeout), Timeout >= 0 -> - ?MODULE:loop(St#st{state=NewState}, Timeout); + ?MODULE:loop(St#st{state = NewState}, Timeout); {stop, Reason, NewState} -> - do_terminate(Reason, St#st{state=NewState}); + do_terminate(Reason, St#st{state = NewState}); Else -> erlang:error(Else) end. - -do_info(#st{module=Module, state=State}=St, Message) -> +do_info(#st{module = Module, state = State} = St, Message) -> case (catch Module:handle_info(Message, State)) of {ok, NewState} -> - ?MODULE:loop(St#st{state=NewState}, infinity); + ?MODULE:loop(St#st{state = NewState}, infinity); {ok, NewState, Timeout} when is_integer(Timeout), Timeout >= 0 -> - ?MODULE:loop(St#st{state=NewState}, Timeout); + ?MODULE:loop(St#st{state = NewState}, Timeout); {stop, Reason, NewState} -> - do_terminate(Reason, St#st{state=NewState}); + do_terminate(Reason, St#st{state = NewState}); Else -> erlang:error(Else) end. - -do_terminate(Reason, #st{module=Module, state=State}) -> +do_terminate(Reason, #st{module = Module, state = State}) -> % Order matters. We want to make sure Module:terminate/1 % is called even if couch_event:unregister/1 hangs % indefinitely. catch Module:terminate(Reason, State), catch couch_event:unregister(self()), - Status = case Reason of - normal -> normal; - shutdown -> normal; - ignore -> normal; - Else -> Else - end, + Status = + case Reason of + normal -> normal; + shutdown -> normal; + ignore -> normal; + Else -> Else + end, erlang:exit(Status). - where({global, Name}) -> global:whereis_name(Name); where({local, Name}) -> whereis(Name). - -name_register({global, Name}=GN) -> +name_register({global, Name} = GN) -> case global:register_name(Name, self()) of yes -> true; no -> {false, where(GN)} end; -name_register({local, Name}=LN) -> +name_register({local, Name} = LN) -> try register(Name, self()) of true -> true - catch error:_ -> - {false, where(LN)} + catch + error:_ -> + {false, where(LN)} end. - get_all_dbnames(Options) -> case proplists:get_value(all_dbs, Options) of true -> all_dbs; _ -> get_all_dbnames(Options, []) end. - get_all_dbnames([], []) -> erlang:error(no_dbnames_provided); get_all_dbnames([], Acc) -> @@ -227,7 +208,6 @@ get_all_dbnames([{dbnames, DbNames} | Rest], Acc) when is_list(DbNames) -> get_all_dbnames([_Ignored | Rest], Acc) -> get_all_dbnames(Rest, Acc). - convert_dbname_list([]) -> []; convert_dbname_list([DbName | Rest]) when is_binary(DbName) -> |