Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
* Simplify the application
* Fixed 'topstories_interval'
* Small refactoring
  • Loading branch information
vkatsuba committed May 17, 2021
1 parent bcded98 commit f00b191
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 56 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ $ ./rebar3 edoc
* Edit file **rebar.config**:
```erlang
{deps, [
{hacker_news, "0.0.1"},
{hacker_news, "0.0.2"},
]}.
```

Expand Down
2 changes: 1 addition & 1 deletion src/hacker_news.app.src
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
{env,[
{topstories_url, "https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty"},
{topstories_total, 50},
{topstories_interval, 300},
{topstories_interval, 300000},
{topstories_retry, 10}
]},
{modules, []},
Expand Down
6 changes: 3 additions & 3 deletions src/hacker_news_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ init([]) ->
period => 1
},
ChildSpec = #{
id => hacker_news_topstories_sup,
start => {hacker_news_topstories_sup, start_link, []},
type => supervisor,
id => hacker_news_topstories,
start => {hacker_news_topstories, start_link, []},
type => worker,
restart => permanent,
shutdown => 5000
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@

-define(DEFAULT_STORIES_TOTAL, 50).
-define(REQ_INTERVAL, 5 * 60 * 1000).
-define(CACHE, cache_topstories).
-define(HACKER_NEWS, hacker_news).

%%% ============================================================================
%%% API functions
Expand All @@ -61,24 +63,24 @@ start_link() ->

get() ->
try
persistent_term:get(cache_topstories)
persistent_term:get(?CACHE)
catch
_:_ ->
get_topstories()
end.

init([]) ->
gen_server:cast(?MODULE, cache_topstories),
gen_server:cast(?MODULE, ?CACHE),
{ok, #{}}.

handle_call(_Request, _From, State) ->
{reply, ignored, State}.

handle_cast(cache_topstories = Key, State) ->
handle_cast(?CACHE = Key, State) ->
Res = get_topstories(),
ok = persistent_term:put(Key, Res),
Interval = application:get_env(hacker_news, topstories_interval, ?REQ_INTERVAL),
timer:apply_after(Interval, gen_server, cast, [?MODULE, Key]),
Interval = application:get_env(?HACKER_NEWS, topstories_interval, ?REQ_INTERVAL),
{ok, _} = timer:apply_after(Interval, gen_server, cast, [?MODULE, Key]),
{noreply, State#{Key => Res}};
handle_cast(_Msg, State) ->
{noreply, State}.
Expand All @@ -103,9 +105,9 @@ terminate(_Reason, _State) ->

get_topstories() ->
try
[_|_] = Url = application:get_env(hacker_news, topstories_url, undefined),
Total = application:get_env(hacker_news, topstories_total, ?DEFAULT_STORIES_TOTAL),
Retry = application:get_env(hacker_news, topstories_retry, 10),
[_|_] = Url = application:get_env(?HACKER_NEWS, topstories_url, undefined),
Total = application:get_env(?HACKER_NEWS, topstories_total, ?DEFAULT_STORIES_TOTAL),
Retry = application:get_env(?HACKER_NEWS, topstories_retry, 10),
ok = httpc:set_options([{keep_alive_timeout, 0}]),
{ok, Data} = maybe_retry(Url, Retry),
[_ | Res] = lists:sublist(string:tokens(get_http_resp_body(Data), ", "), Total + 1),
Expand All @@ -127,8 +129,6 @@ get_topstories() ->

get_http_resp_code({{_, Code, _}, _, _}) ->
Code;
get_http_resp_code({Code, _}) ->
Code;
get_http_resp_code(_) ->
error.

Expand All @@ -142,8 +142,6 @@ get_http_resp_code(_) ->

get_http_resp_body({_, _, Body}) ->
Body;
get_http_resp_body({_, Body}) ->
Body;
get_http_resp_body(_) ->
error.

Expand All @@ -164,5 +162,6 @@ maybe_retry(Url, Retry) ->
Res
catch
_:_ ->
?LOG(warning, "GET topstories - was attempts left: ~p times", [Retry]),
maybe_retry(Url, Retry - 1)
end.
39 changes: 0 additions & 39 deletions src/topstories/hacker_news_topstories_sup.erl

This file was deleted.

0 comments on commit f00b191

Please sign in to comment.