1 |
|
-module(mongoose_graphql_server_admin_mutation). |
2 |
|
-behaviour(mongoose_graphql). |
3 |
|
|
4 |
|
-export([execute/4]). |
5 |
|
-export([await_execution/4]). |
6 |
|
|
7 |
|
-import(mongoose_graphql_helper, [make_error/2]). |
8 |
|
-ignore_xref([execute/4]). |
9 |
|
|
10 |
|
-include("../mongoose_graphql_types.hrl"). |
11 |
|
|
12 |
|
execute(#{method := cli}, server, <<"joinCluster">>, #{<<"node">> := Node}) -> |
13 |
6 |
case mongoose_server_api:join_cluster(binary_to_list(Node)) of |
14 |
|
{ok, _} = Result -> |
15 |
4 |
Result; |
16 |
|
Error -> |
17 |
2 |
make_error(Error, #{node => Node}) |
18 |
|
end; |
19 |
|
execute(#{method := http}, server, <<"joinCluster">>, #{<<"node">> := Node}) -> |
20 |
2 |
spawn(?MODULE, await_execution, |
21 |
|
[1000, mongoose_server_api, join_cluster, [binary_to_list(Node)]]), |
22 |
2 |
{ok, "JoinCluster scheduled"}; |
23 |
|
|
24 |
|
execute(#{method := cli}, server, <<"removeFromCluster">>, #{<<"node">> := Node}) -> |
25 |
2 |
case mongoose_server_api:remove_from_cluster(binary_to_list(Node)) of |
26 |
|
{ok, _} = Result -> |
27 |
2 |
Result; |
28 |
|
Error -> |
29 |
:-( |
make_error(Error, #{node => Node}) |
30 |
|
end; |
31 |
|
execute(#{method := http}, server, <<"removeFromCluster">>, #{<<"node">> := Node}) -> |
32 |
2 |
spawn(?MODULE, await_execution, |
33 |
|
[1000, mongoose_server_api, remove_from_cluster, [binary_to_list(Node)]]), |
34 |
2 |
{ok, "RemoveFromCluster scheduled"}; |
35 |
|
|
36 |
|
execute(#{method := cli}, server, <<"leaveCluster">>, #{}) -> |
37 |
5 |
case mongoose_server_api:leave_cluster() of |
38 |
|
{ok, _} = Result -> |
39 |
2 |
Result; |
40 |
|
Error -> |
41 |
3 |
make_error(Error, #{}) |
42 |
|
end; |
43 |
|
execute(#{method := http}, server, <<"leaveCluster">>, #{}) -> |
44 |
2 |
spawn(?MODULE, await_execution, [1000, mongoose_server_api, leave_cluster, []]), |
45 |
2 |
{ok, "LeaveCluster scheduled"}; |
46 |
|
execute(_Ctx, server, <<"removeNode">>, #{<<"node">> := Node}) -> |
47 |
:-( |
mongoose_server_api:remove_node(binary_to_list(Node)); |
48 |
|
execute(_Ctx, server, <<"setLoglevel">>, #{<<"level">> := LogLevel}) -> |
49 |
20 |
case mongoose_server_api:set_loglevel(LogLevel) of |
50 |
|
{ok, _} = Result -> |
51 |
20 |
Result; |
52 |
|
Error -> |
53 |
:-( |
make_error(Error, #{level => LogLevel}) |
54 |
|
end; |
55 |
|
execute(_Ctx, server, <<"stop">>, #{}) -> |
56 |
:-( |
spawn(mongoose_server_api, stop, []), |
57 |
:-( |
{ok, "Stop scheduled"}; |
58 |
|
execute(_Ctx, server, <<"restart">>, #{}) -> |
59 |
:-( |
spawn(mongoose_server_api, restart, []), |
60 |
:-( |
{ok, "Restart scheduled"}. |
61 |
|
|
62 |
|
%% Helpers |
63 |
|
|
64 |
|
await_execution(Timeout, Module, Fun, Args) -> |
65 |
6 |
timer:sleep(Timeout), |
66 |
6 |
apply(Module, Fun, Args). |