I am writing a console application that will do two tasks.
The app will be run by a Windows scheduler once a day during off-peak hours, when it does run it will do the following:
- Scan 5-6 folders with several hundred files, update a SQL table with the file names
- This app will be referenced by two other applications, these applications will utilize a public method to write exceptions to a SQL table. The exceptions would more of business rule exceptions.
There is a possibility that the app will be called by the scheduler to do its assigned task and also by the other applications at the same time. How would I design the app to ensure that there are no conflicts. I am thinking along the lines of multi threading, parallel tasks?
You do not really need to do anything. The OS handles all the parallelism for you in this situation. The task scheduler starts one process. Those other apps that use yours are running in separate processes. You can run all the processing on a single thread and both tasks can still proceed in parallel.
The only thing you need to be careful about is shared resources. For example, if you’re writing to a hard-coded log file while doing this, one of the tasks will either fail, or will have to wait for the log file to become writable.
Unless you’re doing something silly, the SQL access should be no problem either; just make sure you run all the queries that pertain to a task in a transaction, leaving the database in a consistent state at the end of the transaction.
One potential source of problem is the pretend-serializable transactions, as I like to call them. In MSSQL they are called SNAPSHOT transactions. To be absolutely fully safe, make sure you use serializable transactions and be ready to retry the whole thing if the commit fails due to a conflict.