Jump to content
toufik

proper way to check server up or down in rest application

Recommended Posts

Hi all Delphier , good morning 😀😀
i have rest firemonkey app ,server , client
im trying to check if the server is up or down from the client side using the code below , the code work fine , but when i run it its freeze the app for a while
is there any proper way to do this ,thank you ,🤓☺️

try

RequestLogin.Execute;

if FrmLogin.RequestLogin.Response.Status.Success then

begin

showmessage ('server up')
do something .....

end

else

except on e : exception do

begin

showmessage (' server down.... go home ')

end

end;

Share this post


Link to post
8 hours ago, toufik said:

i have rest firemonkey app ,server , client
im trying to check if the server is up or down from the client side

The best way to handle that is to perform an actual REST request, and handle any error it may produce.

8 hours ago, toufik said:

the code work fine , but when i run it its freeze the app for a while

Move the code to a worker thread so that it doesn't block the UI thread.

  • Thanks 1

Share this post


Link to post

@Remy Lebeau i will try it ,,,,,thank you and ,,, if there any example to how to do this , it will be helpful thank you 

Share this post


Link to post
2 hours ago, toufik said:

if there any example to how to do this , it will be helpful

The simplest option is to use either TThread.CreateAnonymousThread(), eg:

uses
  ..., System.Classes;

procedure TFrmLogin.StartLogin;
begin
  TThread.CreateAnonymousThread(
    procedure
    var
      Success: Boolean;
    begin
      try
        RequestLogin.Execute;
        Success := RequestLogin.Response.Status.Success;
      except
        Success := False;
      end;
      TThread.Queue(nil,
        procedure
        begin
          AfterLogin(Success);
        end
      );
    end
  ).Start;
end;

procedure TFrmLogin.AfterLogin(Success: Boolean);
begin
  if Success then
  begin
    ShowMessage ('server up');
    do something ...
  end else
  begin
    ShowMessage ('server down');
    do something else ...
  end;
end;

Or similarly, TTask.Run():

uses
  ..., System.Threading;

procedure TFrmLogin.StartLogin;
begin
  TTask.Run(
    procedure
    var
      Success: Boolean;
    begin
      try
        RequestLogin.Execute;
        Success := RequestLogin.Response.Status.Success;
      except
        Success := False;
      end;
      TThread.Queue(nil,
        procedure
        begin
          AfterLogin(Success);
        end
      );
    end
  );
end;

procedure TFrmLogin.AfterLogin(Success: Boolean);
begin
  if Success then
  begin
    ShowMessage ('server up');
    do something ...
  end else
  begin
    ShowMessage ('server down');
    do something else ...
  end;
end;

 

Edited by Remy Lebeau
  • Thanks 1

Share this post


Link to post
On 9/15/2020 at 1:05 AM, Remy Lebeau said:

The simplest option is to use either TThread.CreateAnonymousThread(), eg:


uses
  ..., System.Classes;

procedure TFrmLogin.StartLogin;
begin
  TThread.CreateAnonymousThread(
    procedure
    var
      Success: Boolean;
    begin
      try
        RequestLogin.Execute;
        Success := RequestLogin.Response.Status.Success;
      except
        Success := False;
      end;
      TThread.Queue(nil,
        procedure
        begin
          AfterLogin(Success);
        end
      );
    end
  ).Start;
end;

procedure TFrmLogin.AfterLogin(Success: Boolean);
begin
  if Success then
  begin
    ShowMessage ('server up');
    do something ...
  end else
  begin
    ShowMessage ('server down');
    do something else ...
  end;
end;

Or similarly, TTask.Run():


uses
  ..., System.Threading;

procedure TFrmLogin.StartLogin;
begin
  TTask.Run(
    procedure
    var
      Success: Boolean;
    begin
      try
        RequestLogin.Execute;
        Success := RequestLogin.Response.Status.Success;
      except
        Success := False;
      end;
      TThread.Queue(nil,
        procedure
        begin
          AfterLogin(Success);
        end
      );
    end
  );
end;

procedure TFrmLogin.AfterLogin(Success: Boolean);
begin
  if Success then
  begin
    ShowMessage ('server up');
    do something ...
  end else
  begin
    ShowMessage ('server down');
    do something else ...
  end;
end;

 

I can't thank you  enough ,thank s a lot  

Share this post


Link to post

TRestRequest has also a method ExecuteAsync with handlers for successful completion and error handling (in addition to the event handlers OnAfterExecute and OnHTTPProtocolError). Depending on your specific needs it may boil down to just a simple call like this:

  RequestLogin.ExecuteAsync(
    procedure
    begin
      ShowMessage('server up');
    end,
    True,
    True,
    procedure
    begin
      ShowMessage('server down');
    end);

 

  • Thanks 2

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×