There can be number of options to handle this. One of these could be:
Use pending
, in-progress
, error
, and done
- 4 directories.
When App starts, see if in-progress
directory has any files. If there are any - that means, the last app-start resulted in an error while processing the files - do something to make it right and move to done
OR move to error
directory if error persists.
Check in the input
directory for new files. Move those to in-progress
one by one and import them. After import is a success, move it to the done
directory. If import results in an error, and nothing can be done about that file, move to error
directory.
The other system which creates the files has to create those in the input
directory only. The other 3 directories are for the internal purpose of your app.
Other solution could be DB driven. Have a table in the DB to track the status of each file. Status could be IN_PROGRESS, ERROR and DONE.
From your input directory, process only those files that are not there in this DB table. Insert a record in this table with IN_PROGRESS and then import it, and change the status based on the import action's result.
Though, in this case, a lot of files will be there in that directory. Over a period of time, the % of already processed files will be huge. So, it will gradually take more time to get the list of files and ignore them, as compared to finding a new file and process it.
And there would be a lot of other ways to handle this, maybe even more efficiently.