Retrying a command in a function

This topic contains 1 reply, has 2 voices, and was last updated by Profile photo of Jonathan Warnken Jonathan Warnken 6 months, 3 weeks ago.

  • Author
    Posts
  • #39207
    Profile photo of Nathan W
    Nathan W
    Participant

    Hi,
    I have written an automation script for processing some data for a custom application. I have been running into issues from time to time when the process will time out because it is not able to contact the DB for some reason or another. I have found that just rerunning the script tends to resolve the issue. So I wanted to implement a retry into the function. There is one line of code that makes a call to the data ingestion application. I then look for an exit code of 0. If I do not get an exit code of 0 I want to have the script wait 5 minutes then retry calling the application to ingest the data. Here is a sample of my code in the function.

    D:\Programs\Application1\Appfolder\appfolder\appfilder\app.sh $TID
    	if($lastexitcode -ne 0)
    		{
    		Log-Write -LogPath $sLogFile -LineValue "DATA Processing Failed $LastExitCode [$([DateTime]::Now)]"
    		Log-Finish -LogPath $sLogFile -NoExit $True
    		Start-Sleep -s 10
    		# Zip ETL Logs
    		$env:DProgramFiles = "D:\Program Files"
    		set-alias sz "$env:DProgramFiles\7-Zip\7z.exe" 
    		$Source2 = Get-ChildItem -Path $DEXLogs\* -Include *.log , *.txt | % { $_.FullName }
    		$Target2 = "$DEXLogs\$ItemProcessingDate.DATA.Log.zip"
    		sz a -mx=9 $Target2 $Source2
    		# Setup and send failure email
    		$emailBody = (Get-Content $sLogPath\$ItemProcessingDate.Processing.log | out-string)
    		$attachment = "$DATALogs\$ItemProcessingDate.DATA.Log.zip"
    		Send-MailMessage -To $EmailTo -From $EmailFrom -Subject $EmailSubjectFailure -Body $emailBody -SmtpServer $emailSmtpServer -Attachments $attachment
    		Remove-Item $attachment
    		#Log-Email -LogPath $EmailLogPath -EmailFrom $EmailFrom -EmailTo $EmailTo -EmailSubject $EmailSubjectFailure
    		Exit 12
    		}
            else
            {
    		Log-Write -LogPath $sLogFile -LineValue "DATA Processing Completed [$([DateTime]::Now)]"
    		}
    
  • #39208
    Profile photo of Jonathan Warnken
    Jonathan Warnken
    Participant

    it is simple enough to use the exit code to do the retry. Ideally you would want to move to a try catch error logic but if the app does not return a error this is an option

    D:\Programs\Application1\Appfolder\appfolder\appfilder\app.sh $TID
    if($lastexitcode -ne 0){
        Log-Write -LogPath $sLogFile -LineValue "DATA Processing Failed $LastExitCode Will retry in 5 mins [$([DateTime]::Now)]"
        Start-Sleep -Seconds 300
        D:\Programs\Application1\Appfolder\appfolder\appfilder\app.sh $TID
        if($lastexitcode -ne 0){
            Log-Write -LogPath $sLogFile -LineValue "DATA Processing Retry Failed  $LastExitCode [$([DateTime]::Now)]"
            Log-Finish -LogPath $sLogFile -NoExit $True
            Start-Sleep -s 10
            # Zip ETL Logs
            $env:DProgramFiles = "D:\Program Files"
            set-alias sz "$env:DProgramFiles\7-Zip\7z.exe"
            $Source2 = Get-ChildItem -Path $DEXLogs\* -Include *.log , *.txt | % { $_.FullName }
            $Target2 = "$DEXLogs\$ItemProcessingDate.DATA.Log.zip"
            sz a -mx=9 $Target2 $Source2
            # Setup and send failure email
            $emailBody = (Get-Content $sLogPath\$ItemProcessingDate.Processing.log | out-string)
            $attachment = "$DATALogs\$ItemProcessingDate.DATA.Log.zip"
            Send-MailMessage -To $EmailTo -From $EmailFrom -Subject $EmailSubjectFailure -Body $emailBody -SmtpServer $emailSmtpServer -Attachments $attachment
            Remove-Item $attachment
            #Log-Email -LogPath $EmailLogPath -EmailFrom $EmailFrom -EmailTo $EmailTo -EmailSubject $EmailSubjectFailure
            Exit 12
        }else{
            Log-Write -LogPath $sLogFile -LineValue "DATA Processing Completed [$([DateTime]::Now)]"
        }
    
    }else{
        Log-Write -LogPath $sLogFile -LineValue "DATA Processing Completed [$([DateTime]::Now)]"
    }
    
    

You must be logged in to reply to this topic.