简体   繁体   中英

C# script run in SSIS package through SQL Server Agent sometimes triggers Exception has been thrown by the target of an invocation

What I know: A third party software runs in the background on Java. I have no control of the code in this software. Its job is to fetch new zip-files from an external server as soon as they are available. These files are supposed to come in before 0100, but the third party is not able to comply to this request. At 0100, the task I am in control over (a C# script) runs to unzip the files that have been downloaded, extract the necessary files, and zip the files we want back into a different location, and delete anything unnecessary. This program is set to run for 1 hour and take any files that already are there, as well as any file that appears in the folder during that hour. This program compiles fine, runs fine in Visual Studio, and is usually successfully run to completion through SQL Server Agent.

Sometimes though, (like 1-2 out of 5 times), it fails with the subtle message "Exception has been thrown by the target of an invocation".

What I theorise: The zip-files from the third party are quite small. On average maybe 80-90KB. They are usually downloaded in less than a second. Me and my colleagues however suspect that the server, which is SSD-based, is quick enough that while the program we have made runs to unpack anything in the target folder, sometimes it picks up a zip-file that is currently, however briefly, "occupied" by the third party downloader, or even unfinished. This results in our program crashing because it cannot access the zip-file.

(I will not bother pasting code here because since this program normally runs fine I cannot see the code being be the problem).

One of the reasons I think this could be the case is that the task is set to retry one time if it fails. Sometimes it runs to completion on the retry, other times it runs for anything between 10 seconds to 20 minutes before it crashes again. Because a second job is set to start later that night, we cannot set this task to re-run several times upon failure.

The best solution would be to alter the third party program to pause during certain hours, but we have no access to make these kinds of alterations to this program. We are also unsure of what would happen if we were to set a scheduled task to kill this process at 0100. If this program were currently downloading a zip-file, we risk this file being corrupted, which would only result in our unpacking task failing again.

A second solution we have considered is adding a try-catch clause in our program to catch this exception where we make the program wait 1 or 2 seconds, and then retry. The problem there is that we cannot seem to find the specific exception type that is cast. For all I know there may not be one. The only way I see this could be done would be to make a generic catch-any-exception clause, where a retry is cast internally. This would however mean that in the few cases where corrupt zip-files arrive, then we would only see an infinite loop going on.

What I'm asking: - Does anyone know what specific exception is being cast by this kind of problem? (If any). - Any reason why the try-catch-wait(1) approach is a really bad approach?

除了你已经做过的事情之外,没有其他人能够比你更了解你的异常,最好的方法是使用Try-Catch并创建更强大的错误处理。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM