1 |
|
%%% Textgroup server. |
2 |
|
%%% |
3 |
|
%%% Copyright (c) 2022 Holger Weiss <holger@zedat.fu-berlin.de>. |
4 |
|
%%% All rights reserved. |
5 |
|
%%% |
6 |
|
%%% Licensed under the Apache License, Version 2.0 (the "License"); |
7 |
|
%%% you may not use this file except in compliance with the License. |
8 |
|
%%% You may obtain a copy of the License at |
9 |
|
%%% |
10 |
|
%%% http://www.apache.org/licenses/LICENSE-2.0 |
11 |
|
%%% |
12 |
|
%%% Unless required by applicable law or agreed to in writing, software |
13 |
|
%%% distributed under the License is distributed on an "AS IS" BASIS, |
14 |
|
%%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15 |
|
%%% See the License for the specific language governing permissions and |
16 |
|
%%% limitations under the License. |
17 |
|
|
18 |
|
-module(textgroup_app). |
19 |
|
-behaviour(application). |
20 |
|
-export([start/2, |
21 |
|
prep_stop/1, |
22 |
|
stop/1, |
23 |
|
config_change/3]). |
24 |
|
|
25 |
|
-include_lib("kernel/include/logger.hrl"). |
26 |
|
|
27 |
|
%% API. |
28 |
|
|
29 |
|
-spec start(application:start_type(), any()) -> {ok, pid()} | {error, term()}. |
30 |
|
start(_StartType, _StartArgs) -> |
31 |
1 |
?LOG_NOTICE("Starting Textgroup ~s on Erlang/OTP ~s (ERTS ~s)", |
32 |
|
[version(), |
33 |
|
erlang:system_info(otp_release), |
34 |
:-( |
erlang:system_info(version)]), |
35 |
1 |
case textgroup_sup:start_link() of |
36 |
|
{ok, _PID} = Result -> |
37 |
1 |
ok = textgroup_systemd:ready(), |
38 |
1 |
Result; |
39 |
|
{error, _Reason} = Err -> |
40 |
:-( |
Err |
41 |
|
end. |
42 |
|
|
43 |
|
-spec prep_stop(term()) -> term(). |
44 |
|
prep_stop(State) -> |
45 |
1 |
ok = textgroup_systemd:stopping(), |
46 |
1 |
State. |
47 |
|
|
48 |
|
-spec stop(term()) -> ok. |
49 |
|
stop(_State) -> |
50 |
1 |
?LOG_NOTICE("Stopping Textgroup ~s on Erlang/OTP ~s (ERTS ~s)", |
51 |
|
[version(), |
52 |
|
erlang:system_info(otp_release), |
53 |
:-( |
erlang:system_info(version)]), |
54 |
1 |
ok. |
55 |
|
|
56 |
|
-spec config_change([{atom(), term()}], [{atom(), term()}], [atom()]) -> ok. |
57 |
|
config_change(Changed, _New, _Removed) -> |
58 |
1 |
ReopenSocket = lists:any(fun({Key, _Val}) -> |
59 |
1 |
lists:member(Key, [port, pool_size]) |
60 |
|
end, Changed), |
61 |
1 |
case ReopenSocket of |
62 |
|
true -> % Deliberately hit max. restart intensity to start new listener: |
63 |
1 |
lists:foreach( |
64 |
|
fun({_, PID, _, [textgroup_acceptor]}) -> |
65 |
5 |
exit(PID, kill) |
66 |
|
end, supervisor:which_children(textgroup_acceptor_sup)); |
67 |
|
false -> |
68 |
:-( |
ok |
69 |
|
end. |
70 |
|
|
71 |
|
%% Internal functions. |
72 |
|
|
73 |
|
-spec version() -> binary(). |
74 |
|
version() -> |
75 |
2 |
{ok, Version} = application:get_key(vsn), |
76 |
2 |
unicode:characters_to_binary(Version). |