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
|
%%% Copyright (c) 2017, NORDUnet A/S.
%%% See LICENSE for licensing information.
-module(merge_fetch_newentries).
-behaviour(gen_server).
-export([start_link/1, loop/3]).
-export([init/1, handle_call/3, terminate/2, handle_cast/2, handle_info/2,
code_change/3]).
start_link(Args) ->
gen_server:start_link(?MODULE, Args, []).
init({Name, Address, Period}) ->
lager:info("~p:~p starting", [?MODULE, Name]),
ChildPid = spawn_link(?MODULE, loop, [Name, Address, Period]),
{ok, ChildPid}.
handle_call(stop, _From, ChildPid) ->
lager:info("~p: stopping child process ~p", [?MODULE, ChildPid]),
exit(ChildPid, stop),
{stop, normal, stopped, nil}.
get_newentries(NodeName, NodeAddress) ->
DebugTag = "fetchnewentries",
URL = NodeAddress ++ "fetchnewentries",
case merge_util:request(DebugTag, URL, NodeName) of
{<<"ok">>, PropList} ->
Entries = lists:map(fun (S) -> base64:decode(S) end, proplists:get_value(<<"entries">>, PropList)),
{ok, Entries};
Err ->
throw({request_error, result, DebugTag, Err})
end.
loop(Name, Address, Period) ->
receive after Period -> ok end,
lager:info("~p:~p: asking storage node at ~p for missing entries",
[?MODULE, Name, Address]),
EntriesResult = try
get_newentries(Name, Address)
catch
throw:{request_error,request,Tag,Error2} ->
{error, Tag, Error2}
end,
case EntriesResult of
{ok, Entries} ->
lager:debug("got entries: ~p", [Entries]),
merge_fetch_ctrl:newentries(Entries, Name);
{error, _Tag, Error} ->
lager:info("failed to get entries from ~p: ~p", [Name, Error])
end,
loop(Name, Address, Period).
handle_cast(_Request, State) ->
{noreply, State}.
handle_info(_Info, State) ->
{noreply, State}.
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
terminate(_Reason, _State) ->
lager:info("~p terminating", [?MODULE]),
ok.
|