Skip to content

Commit

Permalink
Do something with timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
lukebakken committed Dec 15, 2021
1 parent 89bb4d4 commit de8eddc
Showing 1 changed file with 23 additions and 13 deletions.
36 changes: 23 additions & 13 deletions deps/rabbit/src/rabbit_disk_monitor.erl
Expand Up @@ -237,7 +237,7 @@ get_disk_free(Dir, {win32, _}) ->
error ->
case win32_get_disk_free_pwsh(Dir) of
{ok, Free1} -> Free1;
_ -> 0
_ -> exit(could_not_determine_disk_free)
end
end.

Expand All @@ -256,31 +256,40 @@ win32_get_disk_free_fsutil(Dir) ->
Drive = string:slice(Dir, 0, 2),

% Drive: c:
FsutilCmd = "fsutil volume diskfree " ++ Drive,
FsutilCmd = "fsutil.exe volume diskfree " ++ Drive,

% C:\windows\system32>fsutil volume diskfree c:
% Total free bytes : 812,733,878,272 (756.9 GB)
% Total bytes : 1,013,310,287,872 (943.7 GB)
% Total quota free bytes : 812,733,878,272 (756.9 GB)
FsutilResult = run_cmd(FsutilCmd),
case string:slice(FsutilResult, 0, 5) of
"Error" ->
case run_cmd(FsutilCmd) of
{error, timeout} ->
error;
"Total" ->
FirstLine = hd(string:tokens(FsutilResult, "\r\n")),
{match, [FreeStr]} = re:run(FirstLine, "(\\d+,?)+", [{capture, first, list}]),
{ok, list_to_integer(lists:flatten(string:tokens(FreeStr, ",")))}
FsutilResult ->
case string:slice(FsutilResult, 0, 5) of
"Error" ->
error;
"Total" ->
FirstLine = hd(string:tokens(FsutilResult, "\r\n")),
{match, [FreeStr]} = re:run(FirstLine, "(\\d+,?)+", [{capture, first, list}]),
{ok, list_to_integer(lists:flatten(string:tokens(FreeStr, ",")))}
end
end.


win32_get_disk_free_pwsh(Dir) ->
% Dir:
% "c:/Users/username/AppData/Roaming/RabbitMQ/db/rabbit2@username-z01-mnesia"
Drive = string:slice(Dir, 0, 1),
PoshCmd = "powershell.exe -NoLogo -NoProfile -NonInteractive -Command (Get-PSDrive " ++ Drive ++ ").Free",
PoshResultStr = run_cmd(PoshCmd),
% Note: remove \r\n
PoshResult = string:slice(PoshResultStr, 0, length(PoshResultStr) - 2),
{ok, list_to_integer(PoshResult)}.
case run_cmd(PoshCmd) of
{error, timeout} ->
error;
PoshResultStr ->
% Note: remove \r\n
PoshResult = string:slice(PoshResultStr, 0, length(PoshResultStr) - 2),
{ok, list_to_integer(PoshResult)}
end.

interpret_limit({mem_relative, Relative})
when is_number(Relative) ->
Expand Down Expand Up @@ -344,5 +353,6 @@ run_cmd(Cmd) ->
CmdResult
after 5000 ->
exit(CmdPid, kill),
rabbit_log:error("Command timed out: '~s'", [Cmd]),
{error, timeout}
end.

0 comments on commit de8eddc

Please sign in to comment.