_site/cover/pgmp_db.COVER.html

1 %% Copyright (c) 2023 Peter Morgan <peter.james.morgan@gmail.com>
2 %%
3 %% Licensed under the Apache License, Version 2.0 (the "License");
4 %% you may not use this file except in compliance with the License.
5 %% You may obtain a copy of the License at
6 %%
7 %% http://www.apache.org/licenses/LICENSE-2.0
8 %%
9 %% Unless required by applicable law or agreed to in writing, software
10 %% distributed under the License is distributed on an "AS IS" BASIS,
11 %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 %% See the License for the specific language governing permissions and
13 %% limitations under the License.
14
15
16 -module(pgmp_db).
17
18
19 -behaviour(gen_statem).
20 -export([callback_mode/0]).
21 -export([config/1]).
22 -export([handle_event/4]).
23 -export([init/1]).
24 -export([start_link/1]).
25 -export([start_replication_on_publication/2]).
26 -export([stop_replication_on_publication/2]).
27 -export([types/1]).
28 -export([which_groups/1]).
29
30
31 start_link(Arg) ->
32 10 gen_statem:start_link(?MODULE, [Arg], envy_gen:options(?MODULE)).
33
34
35 start_replication_on_publication(Server, Publication) ->
36 5 gen_statem:call(Server, {?FUNCTION_NAME, Publication}).
37
38
39 stop_replication_on_publication(Server, Publication) ->
40
:-(
gen_statem:call(Server, {?FUNCTION_NAME, Publication}).
41
42
43 types(Server) ->
44
:-(
gen_statem:call(Server, ?FUNCTION_NAME).
45
46
47 config(Server) ->
48
:-(
gen_statem:call(Server, ?FUNCTION_NAME).
49
50
51 which_groups(Server) ->
52
:-(
gen_statem:call(Server, ?FUNCTION_NAME).
53
54
55 init([DB]) ->
56 10 {ok, ready, #{supervisor => hd(get('$ancestors')), db => DB}}.
57
58
59 callback_mode() ->
60 10 handle_event_function.
61
62
63 handle_event({call, From},
64 {start_replication_on_publication, Publication},
65 _,
66 #{supervisor := Supervisor, db := DB}) ->
67 5 {keep_state_and_data,
68 {reply,
69 From,
70 pgmp_rep_sup:start_child(
71 pgmp_sup:get_child_pid(Supervisor, rep_sup),
72 DB,
73 Publication)}};
74
75
76 handle_event({call, From},
77 {stop_replication_on_publication, Publication},
78 _,
79 #{supervisor := Supervisor}) ->
80
:-(
{keep_state_and_data,
81 {reply,
82 From,
83 pgmp_rep_sup:terminate_child(
84 pgmp_sup:get_child_pid(Supervisor, rep_sup),
85 Publication)}};
86
87 handle_event({call, From}, types, _, #{supervisor := Supervisor}) ->
88
:-(
{keep_state_and_data,
89 {reply,
90 From,
91 pgmp_sup:get_child_pid(
92 pgmp_sup:get_child_pid(Supervisor, int_sup),
93 types)}};
94
95 handle_event({call, From}, config, _, #{db := Config}) ->
96
:-(
{keep_state_and_data, {reply, From, Config}};
97
98 handle_event({call, From}, which_groups, _, #{db := #{scope := Scope}}) ->
99
:-(
{keep_state_and_data, {reply, From, pg:which_groups(Scope)}}.
Line Hits Source