File Handling during Auto-Text File Import via NAS

knmknm Member Posts: 170
By utilizing the NAS function, we have developed a function that will import a text file automatically the data into NAV.
We are using a Timer set to a specific frequency, and looking at a specific directory whether a file exists, or not. (The logic is executed only when there is a file).

Generally, the function works fine, but once in a while we get the following error message, which will hand the service, and requires me to restart the service.
You cannot use the file <<filename>> because it is already in use.

And I believe we are getting this error message because when NAV was trying to look at the file, it was still in the middle of being copied over to the import directory.

To avoid this happening from the future, is there a good way of skipping those files that are "locked" for any kind of purposes?

Thank you.

Comments

  • ta5ta5 Member Posts: 1,164
    Hi
    I dont know such a function, but I have often used this workaround:
    The sending program uses a filename xy.tmp as long as it is writing to it. At the end, it renames it to xy.txt. The importing program (NAS in our case) just watches files with txt extensions.
    This is also a good way to avoid corrupted files, if the sending program does not finish its process.
    Hope this helps.
    Thomas
  • einsTeIn.NETeinsTeIn.NET Member Posts: 1,050
    We use another file (wait.flg, empty) to avoid that. First the external app creates that wait flag file. Then it creates/copies the file(s) to be processed. And afterwards it deletes the wait flag file. As long as the wait flag file exists we don't touch any other file. Of course, you have to make sure you don't update your list of available files during the process because there might be additional files in the meantime. That means you have to use some kind of "snapshot" of your directory that shows the situation when you looked for the wait flag file.
    "Money is likewise the greatest chance and the greatest scourge of mankind."
  • DakkonDakkon Member Posts: 192
    One way to handle this is to encapsulate your business logic into a separate codeunit call. This way you can trap any errors that occur, thus preventing it from crashing the NAS process. For instance, assuming ProcessOutstandingFiles is the name of your encapsulating codeunit, your timer trigger may look something like the following:
    IF ProcessOutstandingFiles.RUN THEN
      LogSuccessfulExecution
    ELSE
      LogError;
    

    This of course is just pseudo code and would need some tweaks to be used in production, but hopefully you get the idea.
    Thad Ryker
    I traded my sanity for a railgun :mrgreen:
  • vaprogvaprog Member Posts: 1,140
    There's not usually any need to restart NAS just because an error occured.

    How did you set up your NAS? The same way as Job Queue and other functionality that comes with NAV?

    Also, what exactly is the state of NAS after the error occured? Service terminated? Service running but doing nothing? ...

    Nevertheless, you should implement one of the suggestions given by ta5 and einsTeIn.NET in addition to investigate and possibly fix that need to restart NAS.
  • dynamicslancerdynamicslancer Member Posts: 6
    This may help you in finding solution: dynamicslancer.blogspot.in/p/blog-page.html
Sign In or Register to comment.