-
Notifications
You must be signed in to change notification settings - Fork 0
/
aggregator_connector.erl
573 lines (504 loc) · 19 KB
/
aggregator_connector.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% %%
%% Copyright 2011 Ole Rixmann. %%
%% %%
%% This file is part of PRISMA-Aggregator. %%
%% %%
%% PRISMA-Aggregator is free software: you can redistribute it and/or %%
%% modify it under the terms of the GNU Affero General Public License %%
%% as published by the Free Software Foundation, either version 3 of %%
%% the License, or (at your option) any later version. %%
%% %%
%% PRISMA-Aggregator is distributed in the hope that it will be useful, %%
%% but WITHOUT ANY WARRANTY; without even the implied warranty of %%
%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. %%
%% See the GNU Affero General Public License for more details. %%
%% %%
%% You should have received a copy of the GNU Affero General Public License %%
%% along with PRISMA-Aggregator. If not, see <http://www.gnu.org/licenses/>. %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-module(aggregator_connector).
-behaviour(gen_server).
-define(POLL_TIMEOUT, 30000).
%% API
-export([start_link/1,
stop/1, collapse/1,
new_subscription/1,
start_worker/1,
stop_all_and_delete_mnesia/0,
rebind_all/1,
unsubscribe/1,
update_subscription/1,
emigrate/2,
immigrate/2]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
-include("prisma_aggregator.hrl").
-record(state,
{subscription = #subscription{},
callbacks}).
%%====================================================================
%% API
%%====================================================================
new_subscription(Sub = #subscription{}) ->
Id = get_id(Sub),
F = fun() -> mnesia:read(?PST, Id) end,
case mnesia:transaction(F) of
{atomic, [Entry]} -> %stream is already polled
Entry;
_ -> supervisor:start_child(?SUP, [Sub])
end.
unsubscribe(Id) ->
case get_pid_from_id(Id) of
not_found ->
catch delete_subscription(Id),
not_found;
Pid -> catch gen_server:call(Pid, unsubscribe) % catch wegen timeout...
end.
start_worker(Id) ->
supervisor:start_child(?SUP, [Id]).
start_link(Subscription) ->
gen_server:start_link(?MODULE, [Subscription], []).
stop(Id) ->
case get_pid_from_id(Id) of
not_found -> ok;
Pid -> gen_server:cast(Pid, stop)
end.
collapse(Id) ->
Pid = get_pid_from_id(Id),
?INFO_MSG("Trying to collapse worker with pid: ~n~p", [Pid]),
gen_server:cast(Pid, collapse).
stop_all_and_delete_mnesia() ->
Ids = mnesia:dirty_all_keys(?PST),
lists:map(fun(Id) -> stop(Id) end, Ids),
{atomic, ok} = mnesia:delete_table(?PST),
true = ets:delete(?SPT).
rebind_all(To) ->
Ids = mnesia:dirty_all_keys(?PST),
lists:map(fun(Id) -> rebind(To, Id) end, Ids).
rebind( To, Id) ->
case get_pid_from_id(Id) of
not_found ->
not_found;
Pid -> gen_server:cast(Pid, {rebind, To})
end.
update_subscription(Sub = #subscription{id = Id}) ->
Pid = get_pid_from_id(Id),
gen_server:cast(Pid, {update_subscription, Sub}).
emigrate(To, Id) ->
Pid = get_pid_from_id(Id),
gen_server:cast(Pid, {emigrate, To}).
immigrate(Sub, From) ->
new_subscription(Sub),
mod_prisma_aggregator:send_iq(Sub#subscription.host,
From,
"unsubscribe",
json_eep:term_to_json(Sub#subscription.id)).
%%====================================================================
%% gen_server callbacks
%%====================================================================
init([SubOrId]) ->
Id = get_id_from_subscription_or_id(SubOrId),
% log("Worker ~p starting", [Id]),
process_flag(trap_exit, true),
true = ets:insert(?SPT, {Id, self()}),
F = fun() -> case mnesia:read(?PST, Id) of
[] -> if
Id =/= SubOrId -> ok = mnesia:write(?PST, SubOrId, write)
end,
SubOrId;
[Sub] -> Sub;
Err -> ?INFO_MSG("error reading prisma_subscription_table~n~p", [Err]),
error
end
end,
{atomic, Subscription} = mnesia:transaction(F),
agr:callbacktimer(random, go_get_messages, 1),
Callbacks = ets:new(callbacks, []),
{Host, Port} = get_host_and_port_from_url(Subscription#subscription.url),
ibrowse:set_max_pipeline_size(Host, Port, agr:config_read(ibrowse_max_pipeline_size)),
ibrowse:set_max_sessions(Host, Port, agr:config_read(ibrowse_max_sessions)),
prisma_statistics_server:subscription_add(),
{ok, #state{subscription = Subscription,
callbacks = Callbacks}}.
handle_call(unsubscribe, _From, State) ->
delete_subscription(get_id(State)),
{stop, normal, unsubscribed, State};
handle_call(_Request, _From, State) ->
Reply = ignored,
log("Ignored call~n~p", [{_Request, _From, State}]),
{reply, Reply, State}.
handle_cast({emigrate, To}, State = #state{subscription = Sub}) ->
Tsub = tuple_to_list(Sub#subscription{host = ""}),
%?INFO_MSG("subscription, die emigrieren soll: ~n~poriginal sub: ~n~p", [Tsub, Sub]),
mod_prisma_aggregator:send_iq(Sub#subscription.host,
To,
"immigrate",
json_eep:term_to_json(Tsub)),
{stop, normal, State};
handle_cast({update_subscription, NSub = #subscription{}}, State = #state{subscription = OSub}) ->
Keys = OSub#subscription.last_msg_key,
NNSub = NSub#subscription{last_msg_key = Keys},
ok = mnesia:dirty_write(?PST, NNSub),
{noreply, State#state{subscription=NNSub}};
handle_cast({rebind, To}, State = #state{subscription=Sub}) ->
Nsub = Sub#subscription{accessor = To},
ok = mnesia:dirty_write(?PST, Nsub),
{noreply, State#state{subscription=Nsub}};
handle_cast(go_get_messages, State = #state{subscription = Sub}) ->
CallbackOnConfig = fun(St) ->
case agr:config_read(polling_retry_time_after_failure) of
never -> {stop, normal, St};
Val -> agr:callbacktimer(random, go_get_messages, Val),
{noreply, St}
end
end,
case
try
ibrowse:send_req(Sub#subscription.url, [], get, [], [{stream_to, self()}], agr:config_read(polling_timeout))
catch
_ : _ -> fail
end
of
{ibrowse_req_id, RequestId} ->
prisma_statistics_server:signal_httpc_ok(),
true = ets:insert(get_callbacks(State), {RequestId, {initial_get_stream, []}}),
{noreply, State};
{error, retry_later} ->
prisma_statistics_server:signal_httpc_overload(),
agr:callbacktimer(100, go_get_messages),
{noreply, State};
{error, req_timedout} ->
message_to_coordinator(create_prisma_error(list_to_binary(get_id(State)),
-2,
<<"Network error, timeout">>),
Sub),
log("opening http connection failed on worker ~p for req_timedout", [get_id(State)]),
CallbackOnConfig(State);
{error, {conn_failed, {error, timeout}}} ->
message_to_coordinator(create_prisma_error(list_to_binary(get_id(State)),
-2,
<<"Network error, connection failed -> timeout">>),
Sub),
log("opening http connection failed on worker ~p for timeout", [get_id(State)]),
CallbackOnConfig(State);
{error, {conn_failed, {error, _} = Error}} ->
message_to_coordinator(create_prisma_error(list_to_binary(get_id(State)),
-2,
<<"Network error, connection failed">>),
Sub),
log("opening http connection failed on worker ~p for Error~n~p~nUrl:~p", [get_id(State), Error, Sub#subscription.url]),
CallbackOnConfig(State);
{error, _Reason} ->
message_to_coordinator(create_prisma_error(list_to_binary(get_id(State)),
-2,
<<"Network error, undefinded">>),
Sub),
log("opening http connection failed on worker ~p for reason~n~p", [get_id(State), _Reason]),
CallbackOnConfig(State);
{'EXIT', _} -> agr:callbacktimer(5, go_get_messages);
_Val ->
message_to_coordinator(create_prisma_error(list_to_binary(get_id(State)),
-2,
<<"Network error, undefinded">>),
Sub),
log("opening http connection failed on worker ~p for Val~n~p", [get_id(State), _Val]),
CallbackOnConfig(State)
end;
handle_cast(stop, State) ->
{stop, normal, State};
handle_cast(collapse, State) ->
{stop, error, State};
handle_cast(_Msg, State) ->
log("ignored cast~n~p", [{_Msg, State}]),
{noreply, State}.
handle_info({ibrowse_async_headers, _ReqId, _, _}, State) ->
{noreply, State};
handle_info({ibrowse_async_response, ReqId, Content}, State = #state{callbacks = Callbacks}) ->
case ets:lookup(Callbacks, ReqId) of
[{_, {Hook, List}}] ->
ets:insert(get_callbacks(State), {ReqId, {Hook, [Content | List]}});
[] ->
log("~p didn't find request id in callbacks for http-async-resp!!", [get_id(State)])
end,
{noreply, State};
handle_info({ibrowse_async_response_end, ReqId} , State) ->
case ets:lookup(get_callbacks(State), ReqId) of
[{_, {Hook, List}}] ->
Content = lists:flatten(lists:reverse(List)),
ets:delete(get_callbacks(State), ReqId),
handle_http_response(Hook, Content, State);
[] ->
log("~p didn't find req-id in callbacks for http-end", [get_id(State)]),
agr:callbacktimer(agr:config_read(polling_interval), go_get_messages),
{noreply, State}
end;
handle_info({'EXIT', _Reason, normal}, State) -> %timer process died
{noreply, State};
handle_info({Ref, {error, _}} = _F, State) ->
%Content = ets:foldl(fun(_El, Acc) -> Acc + 1 end, 0, get_callbacks(State)),
log("handle info on ~p, error:~n~p", [get_id(State), _F]),
%ets:delete(get_callbacks(State), Ref),
message_to_coordinator(create_prisma_error(list_to_binary(get_id(State)),
-2,
<<"Network error, undefinded">>),
State),
ets:delete(State#state.callbacks, Ref),
case agr:config_read(polling_retry_time_after_failure) of
never -> {stop, normal, State};
Val -> agr:callbacktimer(random, go_get_messages, Val),
{noreply, State}
end;
handle_info(_Info, State) ->
%Content = ets:foldl(fun(_El, Acc) -> Acc + 1 end, 0, get_callbacks(State)),
%log("handle info, letzte klausel ~p anzahl callbacks: ~p", [get_id(State), Content]),
% log("ignored info: ~n~p", [{Info, State}]),
{noreply, State}.
terminate(_Reason, State) ->
catch ets:delete(?SPT, get_id(State)),
prisma_statistics_server:subscription_remove(),
?INFO_MSG("Worker stopping, id: ~p~n", [get_id(State)]),
ok.
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
%%--------------------------------------------------------------------
%%% Internal functions
%%--------------------------------------------------------------------
get_pid_from_id(Id) ->
try
case ets:lookup(?SPT, Id) of
[{Id, Pid}] -> Pid;
[] -> not_found
end
catch
_:_ -> not_found
end.
get_attribute(El, Attr) ->
get_attribute(El, Attr, Attr).
get_attribute(El, Attr, NewAttr) ->
{NewAttr, xml:get_path_s(El, [{elem, atom_to_list(Attr)}, cdata])}.
parse_rss(Xml) ->
try
Channel = xml:get_path_s(Xml, [{elem, "channel"}]),
{xmlelement, _, _, Rss09items} = Xml,
{xmlelement, "channel", _, ChannelBody} = Channel,
Items = [E || E = {xmlelement, "item", _, _} <- ChannelBody] ++
[E || E = {xmlelement, "item", _, _} <- Rss09items],
Mapper = fun(Item) ->
[get_attribute(Item, title),
get_attribute(Item, pubDate),
get_attribute(Item, link),
get_attribute(Item, content)]
end,
lists:map(Mapper, Items)
catch
_Err : _Reason -> {error, badxml}
end.
parse_atom(Xml) ->
try
{xmlelement, _, _, Channel} = Xml,
Items = [E || E = {xmlelement, "entry",_,_} <- Channel],
Mapper = fun(Item) ->
[get_attribute(Item, title),
get_attribute(Item, link),
get_attribute(Item, id, key),
get_attribute(Item, summary),
get_attribute(Item, content),
get_attribute(Item, updated)]
end,
Ret = lists:map(Mapper, Items),
% log("atom parsed: ~n~p", [Ret]),
Ret
catch
_Err :_Reason -> {error, badxml}
end.
chain([{Fname, Arg}|T]) ->
Fname(Arg, chain(T));
chain([]) -> [].
select_key(Streamentry) ->
HelpFn = fun(Fieldname, Fn) ->
fun() ->
case proplists:get_value(Fieldname, Streamentry) of
Val when (Val =:= undefined) or (Val =:= "") ->
Fn();
Val -> Val
end
end
end,
Fun = chain([{HelpFn, content},
{HelpFn, title},
{HelpFn, link},
{HelpFn, key},
{fun(ok, []) -> "no_key" end, ok}]),
Fun().
get_id_from_subscription_or_id(#subscription{id = Id}) ->
Id;
get_id_from_subscription_or_id(Id) ->
Id.
handle_http_response(initial_get_stream, Body, State) ->
Sub = State#state.subscription,
Ret = case xml_stream:parse_element(Body) of
{error, {_, _Reason}} ->
message_to_coordinator(create_prisma_error(list_to_binary(get_id(Sub)),
-1,
<<"Stream returned invalid XML">>),
Sub),
log("Stream returned invalid XML on ~p", [Sub#subscription.id]),
case agr:config_read(polling_retry_time_after_failure) of
never -> {stop, normal, State};
_Val -> agr:callbacktimer(agr:config_read(polling_interval), go_get_messages),
{noreply, State}
end;
Xml ->
try
Content = case Sub#subscription.source_type of
"RSS" -> parse_rss(Xml);
"ATOM" -> parse_atom(Xml);
_ -> parse_rss(Xml)
end,
NewContent = extract_new_messages(Content, Sub),
NSub = if
length(NewContent) > 0 ->
lists:map(fun(Val) ->
message_to_accessor(json_eep:term_to_json(create_prisma_message(list_to_binary(get_id(State)),
list_to_binary(proplists:get_value(title, Val)))),
State)
end,
NewContent),
EnrichedContent = lists:map(fun([H|T]) ->
[{subId, get_id(Sub)},
{feed, Sub#subscription.source_type},
{date, agr:format_date()},
H | T]
end, NewContent),
ok = store_to_couch(EnrichedContent, State),
StoreSub = Sub#subscription{last_msg_key = merge_keys(Content, Sub#subscription.last_msg_key)},
ok = mnesia:dirty_write(StoreSub),
StoreSub;
true ->
Sub
end,
agr:callbacktimer(agr:config_read(polling_interval), go_get_messages),
{noreply, State#state{subscription = NSub}}
catch
_Arg : _Error -> log("Worker ~p caught error while trying to interpret xml.~n~p : ~p", [get_id(Sub), _Arg, _Error]),
message_to_coordinator(create_prisma_error(list_to_binary(get_id(Sub)),
-1,
<<"Error while trying to interpret XML">>),
Sub),
agr:callbacktimer(agr:config_read(polling_interval), go_get_messages),
{noreply, State}
end
end,
prisma_statistics_server:sub_proceeded(),
Ret;
handle_http_response({couch_doc_store_reply, _Doclist}, _Body, State) ->
%log("Worker ~p stored to couchdb, resp-body: ~n~p", [get_id(State), _Body]),
{noreply, State};
handle_http_response(Ref, {error, _Reason}, State) ->
log("Worker ~p received an polling error: ~n~p", [get_id(State), _Reason]),
message_to_coordinator(create_prisma_error(list_to_binary(get_id(State)),
-2,
list_to_binary(_Reason)),
State),
ets:delete(State#state.callbacks, Ref),
case agr:config_read(polling_retry_time_after_failure) of
never -> {stop, normal, State};
Val -> agr:callbacktimer(random, go_get_messages, Val),
{noreply, State}
end.
extract_new_messages(Messages, #subscription{last_msg_key = KnownKeys}) ->
F = fun(El) -> fun(Key) ->
Key =:= select_key(El)
end
end,
lists:takewhile(fun(El) ->
not(lists:any(F(El), KnownKeys))
end,
Messages).
merge_keys(Items, OldKeys) ->
merge_keys(Items, OldKeys, 3).
merge_keys(Items, OldKeys, N) ->
lists:map(fun(Item) -> select_key(Item) end,
lists:sublist(lists:append(lists:sublist(Items, N), OldKeys),
N)).
get_id(#subscription{id = Id}) ->
Id;
get_id(State) ->
(State#state.subscription)#subscription.id.
get_callbacks(State) ->
State#state.callbacks.
message_to_accessor(Msg, #state{subscription = Sub}) ->
message_to_accessor(Msg, Sub);
message_to_accessor(Msg, Sub) ->
mod_prisma_aggregator:send_message(Sub#subscription.host,
Sub#subscription.accessor,
"PrismaMessage", %TODO
Msg).
message_to_coordinator(Msg, #state{subscription = Sub}) ->
message_to_coordinator(Msg, Sub);
message_to_coordinator(Msg, Sub) ->
catch mod_prisma_aggregator:send_message(Sub#subscription.host,
agr:config_read(coordinator),
"PrismaMessage", %TODO
json_eep:term_to_json(Msg)).
log(Msg, Vars) ->
?INFO_MSG(Msg, Vars).
store_to_couch(Doclist ,State) ->
Pre = doclist_to_json(Doclist),
Jstring = json_eep:term_to_json({[{<<"docs">>, Pre}]}),
{ibrowse_req_id, RequestId} = ibrowse:send_req("http://localhost:5984/prisma_docs/_bulk_docs",
[{"Content-Type", "application/json"}],
post,
Jstring,
[{stream_to, self()}],
1000),
true = ets:insert(get_callbacks(State), {RequestId, {{couch_doc_store_reply, Doclist}, []}}),
ok.
doclist_to_json(Doclist) ->
lists:map(fun(Item) ->
{lists:map(fun({K, V}) ->
Validv = case V of
{} -> <<"">>;
_ -> list_to_binary(V)
end,
{list_to_binary(atom_to_list(K)), Validv}
end,
Item)}
end,
Doclist).
get_host_and_port_from_url(Url) ->
case re:run(Url, "http://(?<Server>[^/:]+)(?:.?(?<=:)(?<Port>[0-9]+).*|.*)", [{capture, ['Server', 'Port'], list}]) of
{[], _} -> nomatch;
{match, [Server, []]} -> {Server, 80};
{match, [Server, Port]} -> {Server, list_to_integer(Port)};
nomatch -> nomatch
end.
create_prisma_message(SubId, Content) ->
{[{<<"class">>,<<"de.prisma.datamodel.message.Message">>},
{<<"id">>,null},
{<<"messageID">>,null},
{<<"messageParts">>,
[{[{<<"class">>,<<"de.prisma.datamodel.message.MessagePart">>},
{<<"content">>,
Content},
{<<"contentType">>,<<"text">>},
{<<"encoding">>,null},
{<<"id">>,null}]}]},
{<<"priority">>,null},
{<<"publicationDate">>,null},
{<<"receivingDate">>, list_to_binary(agr:format_date())},
{<<"recipient">>,null},
{<<"sender">>,null},
{<<"subscriptionID">>,SubId},
{<<"title">>,null}]}.
create_prisma_error(SubId, Type, Desc) ->
{[{<<"class">>,<<"de.prisma.datamodel.message.ErrorMessage">>},
{<<"subscriptionID">>, SubId},
{<<"errorType">>, Type},
{<<"errorDescription">>, Desc}]}.
delete_subscription(Id) ->
F = fun() -> mnesia:delete({?PST, Id})
end,
{atomic, ok} = mnesia:transaction(F).