|
| 1 | +-module(es_projection_mgr). |
| 2 | + |
| 3 | +-moduledoc """ |
| 4 | +Singleton manager for projection runner processes. |
| 5 | +""". |
| 6 | + |
| 7 | +-behaviour(gen_server). |
| 8 | + |
| 9 | +-export([ |
| 10 | + start_link/0, |
| 11 | + stop/0, |
| 12 | + start_projection/3, |
| 13 | + stop_projection/1, |
| 14 | + lookup/1 |
| 15 | +]). |
| 16 | + |
| 17 | +-export([ |
| 18 | + init/1, |
| 19 | + handle_call/3, |
| 20 | + handle_cast/2, |
| 21 | + handle_info/2, |
| 22 | + terminate/2, |
| 23 | + code_change/3 |
| 24 | +]). |
| 25 | + |
| 26 | +-record(state, { |
| 27 | + pids = #{} :: #{atom() => pid()}, |
| 28 | + monitors = #{} :: #{reference() => atom()} |
| 29 | +}). |
| 30 | + |
| 31 | +-opaque state() :: #state{}. |
| 32 | +-export_type([state/0]). |
| 33 | + |
| 34 | +-spec start_link() -> gen_server:start_ret(). |
| 35 | +start_link() -> |
| 36 | + gen_server:start_link({local, ?MODULE}, ?MODULE, [], []). |
| 37 | + |
| 38 | +-spec stop() -> ok. |
| 39 | +stop() -> |
| 40 | + gen_server:stop(?MODULE). |
| 41 | + |
| 42 | +-spec start_projection(StoreContext, ProjectionModule, Options) -> |
| 43 | + {ok, pid()} | {error, Reason} |
| 44 | +when |
| 45 | + StoreContext :: es_kernel_store:store_context(), |
| 46 | + ProjectionModule :: module(), |
| 47 | + Options :: es_projection:options(), |
| 48 | + Reason :: term(). |
| 49 | +start_projection(StoreContext, ProjectionModule, Options) -> |
| 50 | + gen_server:call(?MODULE, {start_projection, StoreContext, ProjectionModule, Options}). |
| 51 | + |
| 52 | +-spec stop_projection(atom()) -> ok | {error, not_found}. |
| 53 | +stop_projection(ProjectionName) -> |
| 54 | + gen_server:call(?MODULE, {stop_projection, ProjectionName}). |
| 55 | + |
| 56 | +-spec lookup(atom()) -> {ok, pid()} | {error, not_found}. |
| 57 | +lookup(ProjectionName) -> |
| 58 | + gen_server:call(?MODULE, {lookup, ProjectionName}). |
| 59 | + |
| 60 | +-spec init([]) -> {ok, state()}. |
| 61 | +init([]) -> |
| 62 | + {ok, #state{}}. |
| 63 | + |
| 64 | +-spec handle_call(term(), {pid(), term()}, state()) -> |
| 65 | + {reply, term(), state()}. |
| 66 | +handle_call({start_projection, StoreContext, ProjectionModule, Options}, _From, State) -> |
| 67 | + ProjectionName = ProjectionModule:name(), |
| 68 | + case maps:find(ProjectionName, State#state.pids) of |
| 69 | + {ok, Pid} -> |
| 70 | + {reply, {ok, Pid}, State}; |
| 71 | + error -> |
| 72 | + start_and_monitor(ProjectionName, StoreContext, ProjectionModule, Options, State) |
| 73 | + end; |
| 74 | +handle_call({stop_projection, ProjectionName}, _From, State) -> |
| 75 | + case maps:find(ProjectionName, State#state.pids) of |
| 76 | + {ok, Pid} -> |
| 77 | + ok = es_projection:stop(Pid), |
| 78 | + {reply, ok, remove_projection(ProjectionName, State)}; |
| 79 | + error -> |
| 80 | + {reply, {error, not_found}, State} |
| 81 | + end; |
| 82 | +handle_call({lookup, ProjectionName}, _From, State) -> |
| 83 | + case maps:find(ProjectionName, State#state.pids) of |
| 84 | + {ok, Pid} -> |
| 85 | + {reply, {ok, Pid}, State}; |
| 86 | + error -> |
| 87 | + {reply, {error, not_found}, State} |
| 88 | + end; |
| 89 | +handle_call(_Request, _From, State) -> |
| 90 | + {reply, {error, unknown_call}, State}. |
| 91 | + |
| 92 | +-spec handle_cast(term(), state()) -> {noreply, state()}. |
| 93 | +handle_cast(_Request, State) -> |
| 94 | + {noreply, State}. |
| 95 | + |
| 96 | +-spec handle_info(term(), state()) -> {noreply, state()}. |
| 97 | +handle_info({'DOWN', MonitorRef, process, _Pid, _Reason}, State) -> |
| 98 | + case maps:find(MonitorRef, State#state.monitors) of |
| 99 | + {ok, ProjectionName} -> |
| 100 | + {noreply, remove_projection(ProjectionName, State)}; |
| 101 | + error -> |
| 102 | + {noreply, State} |
| 103 | + end; |
| 104 | +handle_info(_Info, State) -> |
| 105 | + {noreply, State}. |
| 106 | + |
| 107 | +-spec terminate(term(), state()) -> ok. |
| 108 | +terminate(_Reason, _State) -> |
| 109 | + ok. |
| 110 | + |
| 111 | +-spec code_change(term(), state(), term()) -> {ok, state()}. |
| 112 | +code_change(_OldVsn, State, _Extra) -> |
| 113 | + {ok, State}. |
| 114 | + |
| 115 | +-spec start_and_monitor( |
| 116 | + atom(), es_kernel_store:store_context(), module(), es_projection:options(), state() |
| 117 | +) -> |
| 118 | + {reply, {ok, pid()} | {error, term()}, state()}. |
| 119 | +start_and_monitor(ProjectionName, StoreContext, ProjectionModule, Options, State) -> |
| 120 | + case es_projection_sup:start_projection(StoreContext, ProjectionModule, Options) of |
| 121 | + {ok, Pid} -> |
| 122 | + MonitorRef = erlang:monitor(process, Pid), |
| 123 | + NewState = State#state{ |
| 124 | + pids = (State#state.pids)#{ProjectionName => Pid}, |
| 125 | + monitors = (State#state.monitors)#{MonitorRef => ProjectionName} |
| 126 | + }, |
| 127 | + {reply, {ok, Pid}, NewState}; |
| 128 | + {error, Reason} -> |
| 129 | + {reply, {error, Reason}, State} |
| 130 | + end. |
| 131 | + |
| 132 | +-spec remove_projection(atom(), state()) -> state(). |
| 133 | +remove_projection(ProjectionName, State) -> |
| 134 | + Pids = maps:remove(ProjectionName, State#state.pids), |
| 135 | + Monitors = maps:filter( |
| 136 | + fun(_MonitorRef, Name) -> Name =/= ProjectionName end, |
| 137 | + State#state.monitors |
| 138 | + ), |
| 139 | + State#state{pids = Pids, monitors = Monitors}. |
0 commit comments