0

I created a program who reads tasks (send email/send sms etc) from a database and executes each task on the specific time it should. The program works and executes all the tasks. I am using error handling but I want to take extra precautions so if the program crashes for any reason it will notify me and restart the program automatically and execute the tasks that didn't execute because of the crash. The program runs on windows.

Edit: I'll explain more about how the program works. The program is constantly reading from the sql table and adds reminders to a List that answer this parameters: IsExecuted=False And DateTimeStart is within 1 minute range from DateTime.Now. Once I finished reading the table and added relevant reminders to the list. I execute all the reminders (asynchronously)-> remove them from the list -> and change IsExecuted=True in the sql table so it won't add it again. Once I finish sending all the reminders i repeat this process infinitely.

What is the best way to achieve this goal? How do I create a service/program that not only restart the main program but also checks what reminders should have executed but didn't?

Thanks.

Ron D
  • 23
  • 2
  • 6
  • You can use windows task scheduler, put a task to check if your process is running if not you just start over, you can make this configuration in the moment the user accept to install your application (with privileged user, using msi), for generate the msi installer I recommend wixtoolset (https://wixtoolset.org/) – Danizavtz May 05 '20 at 09:16
  • Can you please give us a bit more detail? What do you want to do with those "tasks" that failed to process? Do you want to try to process them again? Or do you want to put them into a dead letter queue? Did you considered to use Windows Service? Or are you looking for some self-hosting solution? Are we talking about .NET Framework or .NET Core? etc... Please give us more detail to be able to help you. – Peter Csala May 05 '20 at 09:17
  • @PeterCsala Hi, Peter thanks. I edited the question so I it will be more clear how the program works. – Ron D May 05 '20 at 09:45

2 Answers2

0

You can install your program as a service. You can then configure recovery options either by hand or by powershell.

If needed you can install a monitor-program as a service, and let this run the main program. But I do not see the need for this if failures in the main program cannot result in some inconsistent state that cannot be recovered from.

JonasH
  • 28,608
  • 2
  • 10
  • 23
0

Most probably the easiest way to "self-host" a console application is to wrap it into a Windows Service. It can be automatically restarted whenever the service crashes. You should check this SO discussion for further details about this.


There is also another thing, which should be considered. According to my understanding your applications workflow can be described like this:
1) Reads unprocessed data
2/A) Process read data
2/B) Mark data as processed

I've used A and B to indicate that they are executed concurrently. But there is a problem with this, what happens either 2/A or 2/B fails.

  • If 2/A fails but 2/B succeeded then the job failed, but it is reported as success (no retry will be initiated)
  • If 2/B fails but 2/A succeeded then the job is done, but is is not reported as success (so it will be retried)

A slightly better approach would be use semantic locking by introducing ProcessStarted and ProcessFinished states. With that in mind the workflow would look like this:

1) Read unprocessed data and mark them as ProcessStarted
2) Process data
3) Mark processed data as ProcessFinished

With this approach you can easily determine which data was started to process but failed to finish, by querying those where the state is ProcessStarted and the last modified date is greater than a predefined threshold.

There is one more thing to note that in case of batch processing you should be able to retry the whole batch, not just a single data.

Peter Csala
  • 17,736
  • 16
  • 35
  • 75