-
Notifications
You must be signed in to change notification settings - Fork 2
/
runes_app.erl
69 lines (59 loc) · 1.75 KB
/
runes_app.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
-module(runes_app).
-behaviour(application).
-export([start/2,stop/1]).
-define(WAIT_FOR_RESOURCES,2500).
start(_StartType, _StartArgs) ->
ok = ensure_contact(),
resource_discovery:add_local_resource(runes,node()),
resource_discovery:add_target_resource_type(runes),
resource_discovery:trade_resources(),
timer:sleep(?WAIT_FOR_RESOURCES),
runes_kb:init(),
io:format("success"),
case runes_sup:start_link() of
{ok,Pid} ->
io:format("success"),
runes_agenda:start(),
{ok, Pid};
Other->
{error,Other}
end.
stop(_State) ->
ok.
ensure_contact() ->
DefaultNodes = ['contact@202.38.95.139'],
case get_env(runes, contact_nodes, DefaultNodes) of
[] ->
{error, no_contact_nodes};
ContactNodes ->
ensure_contact(ContactNodes)
end.
ensure_contact(ContactNodes) ->
Answering = [N || N <- ContactNodes, net_adm:ping(N) =:= pong],
case Answering of
[] ->
{error, no_contact_nodes_reachable};
_ ->
DefaultTime = 3000,
WaitTime = get_env(runes, wait_time, DefaultTime),
wait_for_nodes(length(Answering), WaitTime)
end.
wait_for_nodes(MinNodes, WaitTime) ->
Slices = 10,
SliceTime = round(WaitTime/Slices),
wait_for_nodes(MinNodes, SliceTime, Slices).
wait_for_nodes(_MinNodes, _SliceTime, 0) ->
ok;
wait_for_nodes(MinNodes, SliceTime, Iterations) ->
case length(nodes()) > MinNodes of
true ->
ok;
false ->
timer:sleep(SliceTime),
wait_for_nodes(MinNodes, SliceTime, Iterations - 1)
end.
get_env(AppName, Key, Default) ->
case application:get_env(AppName, Key) of
undefined -> Default;
{ok, Value} -> Value
end.