Link to home
Start Free TrialLog in
Avatar of Norm-al
Norm-alFlag for United States of America

asked on

need to stop a service if procedure fails

I have a process that does 2 steps:
(1) Moves files to a specified location on the network based on its name and (2) Saves the network path to a sql table.

The problem I have is that the service works for a while as it should then switches to just doing step (1) and never does step (2) UNTIL I restart the service.

After days of troubleshooting, I realized that users were typing incorrect file names and the service was unable to do step (2) after that... basically after an error, it just continues to move the files to their appropriate locations on the network but no longer saves the network path... until I restart the service then I have to reprocess all those files that did not get their links created.

I thought the best way for me to no longer have this problem is that IF the service fails in moving the file due to a typo(step 1) to STOP the service and wait for me or another admin to research and correct the error, then start up the service once the filename has been fixed.

I just didn't know where in the service code to plug this in (this is not my original code so I am not too familiar with it)

If possible, I would like the service to STOP immediately upon failure to move a file.

Attached are the 2 procedures in question...  Thanks!
// move the files and save the link
      if MoveFiles(sInboundFolder + SearchFiles.Files[I]) then
        SaveLink(sFileDestinationFolder + Files[I]);

Procedure MoveFiles;
begin
    Result := False;  // default failure.
    with FileOp do begin
      // copy files
      Operation   := fopCopy;
      Destination := sFileDestinationFolder;
      SourceFiles.Clear;
      SourceFiles.Add(sFileOrigin);
      if Execute then begin
        // if copy success, delete files
        Operation := fopDelete;
        Destination := '';
        Execute;
        LogFileProperties;
        Result := True;       // move successful
        AppLog.Msg := '  moved file: ' + sFileOrigin;
      end
      else
        AppLog.Msg := '*!*!*  ERROR MOVING FILE: ' + sFileOrigin;
    end;

Function SaveLink;
begin
    // see if link already exists...
    with qryLinkSearch do begin
      Close;
      SQL.Clear;
      if wonum = '' then
        sStatement := 'SELECT ' + sLinkField + ' FROM ' + sLinkTable + sWhereClause
      else
        sStatement := 'SELECT ' + sLinkField + ' FROM ' + sLinkTable + ' WHERE wonum = ''' + woNum + '''';
      SQL.Add(sStatement);
      Open;
      wonum := '';
      // Specified recordID does not exist
      if Eof {or (qryLinkSearch.FieldByName(sLinkField).AsString <> '')} then begin
        AppLog.Msg := '*** Cannot locate record to link to ***';
        Result := False; //didn't save the link...
        exit;
      end;
      // too many records match ID
      if qryLinkSearch.RecordCount > 1 then begin
        Result := False;  //not a distinct record.
        AppLog.Msg := '*** Duplicate workorder number ***';
        exit;
      end;

      // save the link (record exists and field is blank
      edit;
      FieldByName(sLinkField).AsString := sFileName;
      Post;
      AppLog.Msg := '  Saved link: ' + sFileName;
    end;
  end;

Open in new window

Avatar of Thommy
Thommy
Flag of Germany image

If moving the file fails, the service already writes an error message into the AppLog:

Procedure MoveFiles;
begin
    Result := False;  // default failure.
    with FileOp do begin
      // copy files
      Operation   := fopCopy;
      Destination := sFileDestinationFolder;
      SourceFiles.Clear;
      SourceFiles.Add(sFileOrigin);
      if Execute then begin
        // if copy success, delete files
        Operation := fopDelete;
        Destination := '';
        Execute;
        LogFileProperties;
        Result := True;       // move successful
        AppLog.Msg := '  moved file: ' + sFileOrigin;
      end
      else
        AppLog.Msg := '*!*!*  ERROR MOVING FILE: ' + sFileOrigin;    end;

To stop the service after move failures, you should extend the code as follows:

Procedure MoveFiles;
begin
    Result := False;  // default failure.
    with FileOp do begin
      // copy files
      Operation   := fopCopy;
      Destination := sFileDestinationFolder;
      SourceFiles.Clear;
      SourceFiles.Add(sFileOrigin);
      if Execute then begin
        // if copy success, delete files
        Operation := fopDelete;
        Destination := '';
        Execute;
        LogFileProperties;
        Result := True;       // move successful
        AppLog.Msg := '  moved file: ' + sFileOrigin;
      end
      else begin
        AppLog.Msg := '*!*!*  ERROR MOVING FILE: ' + sFileOrigin;
        Application.Terminate;
      end;
    end;

Sorry, did not notice that we are talking about a service!!!

Application.Terminate will not work for a service.

Try this to stop a service from within itself and restart it if necessary...

How can I restart a Windows service application written in Delphi?
http://stackoverflow.com/questions/667588/how-can-i-restart-a-windows-service-application-written-in-delphi
ASKER CERTIFIED SOLUTION
Avatar of Thommy
Thommy
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Norm-al

ASKER

Yikes! I actually put that there to Pause the service but it wasn't working until I realized I forgot the BEGIN! :P
Thanks :)