summaryrefslogtreecommitdiff
path: root/src/plopconfig.erl
diff options
context:
space:
mode:
authorMagnus Ahltorp <map@kth.se>2017-01-27 02:02:39 +0100
committerMagnus Ahltorp <map@kth.se>2017-01-27 02:03:24 +0100
commitd4793aea4dfbf1862bf6ca8eb5cf4279a41b36a4 (patch)
tree0814d4181a2cc946f3ada50f3e9c2b2b43eba7b8 /src/plopconfig.erl
parent7b114604595b2e3bb0816ffb01548b02c43cdea5 (diff)
Separate erlang config file for reloadable parameters
Diffstat (limited to 'src/plopconfig.erl')
-rw-r--r--src/plopconfig.erl57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/plopconfig.erl b/src/plopconfig.erl
new file mode 100644
index 0000000..00b29df
--- /dev/null
+++ b/src/plopconfig.erl
@@ -0,0 +1,57 @@
+%%% Copyright (c) 2017, NORDUnet A/S.
+%%% See LICENSE for licensing information.
+
+-module(plopconfig).
+-export([get_env/1, get_env/2, init_table/0, load_config/0]).
+
+-define(PLOPCONFIG_TABLE, plopconfig).
+
+init_table() ->
+ case ets:info(?PLOPCONFIG_TABLE) of
+ undefined ->
+ ok;
+ _ ->
+ ets:delete(?PLOPCONFIG_TABLE)
+ end,
+ ets:new(?PLOPCONFIG_TABLE, [set, public, named_table]),
+ load_config().
+
+keys() ->
+ KeyList = lists:map(fun ({Key, _Value}) -> Key end,
+ ets:tab2list(?PLOPCONFIG_TABLE)),
+ sets:from_list(KeyList).
+
+load_config() ->
+ {ok, Filename} = application:get_env(plop, plopconfig),
+ {ok, File} = file:consult(Filename),
+ OldKeys = keys(),
+ lists:foreach(
+ fun ({Key, Value}) ->
+ true = ets:insert(?PLOPCONFIG_TABLE, {Key, Value})
+ end, hd(File)),
+ NewKeys = keys(),
+ RemovedKeys = sets:subtract(OldKeys, NewKeys),
+ lists:foreach(fun (Key) ->
+ ets:delete(?PLOPCONFIG_TABLE, Key)
+ end, sets:to_list(RemovedKeys)),
+ lager:info("loaded config: new keys ~p old keys ~p removed keys ~p",
+ [sets:to_list(NewKeys),
+ sets:to_list(OldKeys),
+ sets:to_list(RemovedKeys)]),
+ ok.
+
+get_env(Key, DefaultValue) ->
+ case get_env(Key) of
+ {ok, Value} ->
+ Value;
+ _ ->
+ DefaultValue
+ end.
+
+get_env(Key) ->
+ case ets:lookup(?PLOPCONFIG_TABLE, Key) of
+ [{_, Value}] ->
+ {ok, Value};
+ [] ->
+ undefined
+ end.