Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Detect when application is blocked



Permlink Replies: 5 - Last Post: Apr 18, 2018 12:15 AM Last Post By: Roy Lambert Threads: [ Previous | Next ]
Christian Kaufm...

Posts: 68
Registered: 7/26/98
Detect when application is blocked
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 15, 2018 12:58 PM
Hi,

I run a delphi application on a server, which has several http services
(using Indy). Sometimes this application runs fine for weeks but from
time to time it runs "out of memory" or blocks completely for reasons
I'm not able to track down.

So I need a solution to control this application and in case of
blocking, restart it. What is the best way to do that?

My second problem: It's still an application but I would like to turn
it in a windows service. But I'm a bit afraid, if handling this
blocking problems will not become even more difficult.

Any tips on how to solve these problems?

cu ck
Roy Lambert

Posts: 40
Registered: 10/21/99
Re: Detect when application is blocked
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 16, 2018 12:04 AM   in response to: Christian Kaufm... in response to: Christian Kaufm...
Christian

First thing I'd suggest is include MadExcept or EurekaLog to help identify the errors. Secondly I'd enable FastMM debug mode to see if you have any memory leaks. Your out of memory could be something nasty or it could be bad code not freeing something.

The normal way to handle exceptions is a try..except block. They do have some performance penalties so use only where necessary. What I've done in my email app is the chunk that sends / receives emails in the background is shown below. Essentially if I catch an error I close and restart. The biggest problem is making sure you reset everything that is needed

procedure TBackgroundEMail.Execute;
var
CrashCount: integer;
wResult: TWaitResult;
begin
CrashCount := 0;
PostMessage(iParams.ReportBack, EMailServiceStarted, 0, 0);
iOtherDate := Date;
while not Terminated do begin
try
if not SystemIsolated then begin
iCrashPoint := 0;
PostMessage(iParams.ReportBack, EMailServiceStarted, 0, 0);
SendQueuedEmails;
PostMessage(iParams.ReportBack, EMailServiceStarted, 0, 0);
if not SystemIsolated then FetchAnyEmails;
PostMessage(iParams.ReportBack, EMailServiceStarted, 0, 0);
if (not SystemIsolated) then OtherWork;
iCrashPoint := 0;
if not Terminated then begin
PostMessage(iParams.ReportBack, EMailServiceInactive, 0, 0);
if (not Terminated) and Assigned(iSignal) then begin
wResult := iSignal.WaitFor(iParams.LoopTime);
if wResult <> wrTimeout then begin
ReleaseService;
Terminate;
end;
end;
end;
end else begin
ReleaseService;
if not (Terminated) and Assigned(iSignal) then begin
wResult := iSignal.WaitFor(iParams.LoopTime);
if wResult = wrTimeout then begin
if not SystemIsolated then InitialiseService;
end else begin
Terminate;
end;
end;
end;
except
inc(CrashCount);
ReleaseService;
if CrashCount <= 2 then begin
{try and catch error}
elTfR('Background', 4001, 'Sending restart signal', 'Reason - crash at: ' + IntToStr(iCrashPoint));
PostMessage(iParams.ReportBack, EMailServiceRestartWanted, 0, 0);
Terminate;
{/try and catch error}
elTfR('Background', 0, 'Crash at: ' + IntToStr(iCrashPoint), 'Count: ' + IntToStr(CrashCount));
InitialiseService;
PostMessage(iParams.ReportBack, EMailServiceRestartWanted, 0, 0);
end else begin
elTfR('Background', 0, 'Crash at: ' + IntToStr(iCrashPoint), 'Application closed');
PostMessage(iParams.ReportBack, EMailServiceCrash, iCrashPoint, 0);
Terminate;
end;
end;
end;
PostMessage(iParams.ReportBack, bgndClosed, 0, 0);
end;

Roy Lambert

Alex Belo

Posts: 626
Registered: 10/8/06
Re: Detect when application is blocked
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 16, 2018 7:29 AM   in response to: Roy Lambert in response to: Roy Lambert
Roy Lambert wrote:

Your out of memory could be something nasty or it could be bad code
not freeing something.

Another (highly) possible reason is memory fragmentation.

--
Alex
Roy Lambert

Posts: 40
Registered: 10/21/99
Re: Detect when application is blocked
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 17, 2018 1:00 AM   in response to: Alex Belo in response to: Alex Belo
Alex

Your out of memory could be something nasty or it could be bad code
not freeing something.

Another (highly) possible reason is memory fragmentation.

Been a long while since that one bit me but yes a definite possibility for a long running app with no chance to wipe and refresh.

Roy
Christian Kaufm...

Posts: 68
Registered: 7/26/98
Re: Detect when application is blocked
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 17, 2018 5:05 AM   in response to: Roy Lambert in response to: Roy Lambert
Hi Roy,

I already use MadExcept, many temporary objects I have are referenced
by interface with TInterfacedObject as base class and for threads I use
a base class with a try except handler arround the execute method.

I was thinking of a second application, that sends a message and if
there is no answer anymore, I kill the process of my main application
and restart it. But what if the control application is blocked?

And how to kill / restart services?

I looked at your code but some important variables/methods are unclear
for me: SystemIsolated? ReleaseService? iParams? iCrashPoint?
InitializeService? eltfr?

cu ck
Roy Lambert

Posts: 40
Registered: 10/21/99
Re: Detect when application is blocked
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 18, 2018 12:15 AM   in response to: Christian Kaufm... in response to: Christian Kaufm...
Christian

I looked at your code but some important variables/methods are unclear
for me:

That's because they're simple control variables or specific to my solution / application.

eg

SystemIsolated - check to see if other users are accessing system
ReleaseService - does all the freeing up and closing down of my stuff
iParams - record passed in for initial settings
iCrashPoint: set at various places so I have a cat in hell's chance of finding where the error came from
InitializeService - sets everything up
eltfr - logs the error

Roy

Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02