-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhelper.erl
148 lines (117 loc) · 4.32 KB
/
helper.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
-module(helper).
-export([reload_modules/1, reload_server/1, refresh_beams/0]).
-export([start_application/1]).
-export([loud_logging/0, quiet_logging/0]).
-export([dump_mnesia/0, traverse_table_and_show/1]).
-export([atom_test/0]).
-export([add_logging/1, add_many_logging/1]).
-export([proc_mon/0]).
-export([kill_process/1]).
% Copy of reg_level_info record found in io/base/apps/bd_logger/include/bd_logger.hrl
-record(reg_level_info, {level_string, level_id, enabled, callback_module}).
-on_load(load_helper_module/0).
load_helper_module() ->
erlang:system_flag(backtrace_depth, 100),
% Do something here to load record definitions eventually.
ok.
atom_test() ->
atom_test(50000).
atom_test(0) -> ok;
atom_test(K) ->
A = list_to_atom("test"),
io:fwrite(atom_to_list(A)),
atom_test(K-1).
loud_logging() ->
bd_logger_app:enable_console("AUDIT"),
bd_logger_app:enable_console("MGMT").
quiet_logging() ->
bd_logger_app:disable_console("AUDIT"),
bd_logger_app:disable_console("MGMT").
add_many_logging([]) -> ok;
add_many_logging([LevelAtom | LevelAtomList]) ->
add_logging(LevelAtom),
add_many_logging(LevelAtomList).
add_logging(LevelAtom) ->
NewLevelInfo = #reg_level_info{
level_string=LevelAtom,
level_id=LevelAtom,
enabled=true,
callback_module=syslog_nif_impl},
bd_logger_app:register_levels([NewLevelInfo]),
bd_logger_app:enable_levels("MGMT", [LevelAtom]),
bd_logger_app:enable_levels("AUDIT", [LevelAtom]).
refresh_beams() ->
BeamsString = case file:read_file("/root/beam_updates") of
{error, _} -> "";
{ok, BeamBinString} ->
Tmp = binary_to_list(BeamBinString),
lists:filter(fun(C) -> not lists:member(C, "\n") end, Tmp)
end,
ModuleAsStringList = string:tokens(BeamsString, " "),
ModuleAsAtomList = modules_string_to_atom(ModuleAsStringList, []),
ReloadedModulesLength = length(reload_modules(ModuleAsAtomList)),
ReloadedModulesLength = length(ModuleAsAtomList),
ok = file:delete("/root/beam_updates"),
ModuleAsAtomList.
modules_string_to_atom([], AtomList) -> AtomList;
modules_string_to_atom([[] | T], AtomList) -> modules_string_to_atom(T, AtomList);
modules_string_to_atom([H | T], AtomList) -> modules_string_to_atom(T, [list_to_atom(H) | AtomList]).
reload_modules(ModuleList) ->
reload_modules(ModuleList, []).
reload_modules([], ReloadedModules) ->
ReloadedModules;
reload_modules([ H | T ], ReloadedModules) ->
reload_modules(T, reload_module(H) ++ ReloadedModules).
reload_module(ModuleName) ->
code:purge(ModuleName),
code:load_file(ModuleName),
[ModuleName].
reload_server([]) ->
[];
reload_server([ H | T ]) ->
reload_server(H) ++ reload_server(T);
reload_server(Server) ->
sys:suspend(Server),
reload_modules(Server),
sys:change_code(Server, Server, undefined, []),
sys:resume(Server),
[Server].
start_application(Application) ->
io:fwrite("Attempting to load ~s\n", [Application]),
case application:start(Application) of
ok ->
io:fwrite("Loaded ~s\n", [Application]),
ok;
{error, {not_started, DepsApplication}} ->
io:fwrite("Failed to load ~s, application ~s not loaded.\n", [Application, DepsApplication]),
start_application(DepsApplication),
start_application(Application)
end.
dump_mnesia() ->
traverse_table_and_show(bdm_config).
traverse_table_and_show(Table_name)->
Iterator = fun(Rec,_)->
case Rec of
{_, catalog_feeds, _} ->
io:format("{bdm_config, catalog_feeds, [long]}~n");
_ ->
io:format("~p~n",[Rec])
end,
[]
end,
case mnesia:is_transaction() of
true -> mnesia:foldl(Iterator,[],Table_name);
false ->
Exec = fun({Fun,Tab}) -> mnesia:foldl(Fun, [],Tab) end,
mnesia:activity(transaction,Exec,[{Iterator,Table_name}],mnesia_frag)
end.
proc_mon() ->
spawn(fun proc_mon_int/0).
proc_mon_int() ->
{{Year, Month, Day}, {Hour, Minute, Second}} = calendar:local_time(),
io:fwrite("~4..0B-~2..0B-~2..0B ~2..0B:~2..0B:~2..0B Live processes: ~B~n",
[Year, Month, Day, Hour, Minute, Second, length(processes())]),
timer:sleep(1000),
proc_mon_int().
kill_process(P) ->
exit(P, kill).