2

Imagine the following situation. Windows service from time to time checks the data in database table. When some new data appears, it starts processing each new row.

The processing consists of several logical stages, let it be:

  • get some additional data from web service;
  • find an existins object via web service by data from stage 1 or create a new object;
  • inform an interested person of the actions done (with details about the object that was found/created on stage 2);
  • do smth else.

Just for now if any exception happens, the service updates the DB row and sets a flag indicating that a mistake has happened. Some time later service will try to process the row once again... and here is the problem.

Processing will start from the very beginning, from stage 1. In this case if exception happened on stage 4 and if it will happen again and again the interested person from stage 3 will be informed again and again and again...

To completely stop row processing in case of exception is not possible and not desirable in my situation. Ideally it would be nice if there was a way to start processing from the stage where it failed last time.

Now I need your advice how all these can be handled. In fact, everything is even more complicated, because there are several patterns of processing, different number of stages and so on.

Thanks in advance.

UPDATE

Yes, I have the State parameter in data rows :) It is just not used just for now. And exception handling is not a new thing for me.

The question is: what is the best way to handle state switching? In other words to make a clear logical link between stage number and processing method? The flow of execution can be very diffirent and include various number of stages and methods for different rows.

I hope, there are more pleasant ways than writing endless switch/case blocks for every new sutiation?

26071986
  • 2,134
  • 2
  • 21
  • 31
  • For reference: If your main reason for having a service is to run some stuff "from time to time", chances are what you need isn't a windows service, but a scheduled task. – cHao Jun 01 '12 at 14:33

2 Answers2

1

There are a couple of patterns that can help with each issue in your description.

  1. Windows service checking queue. Have a timer in your service that runs every 1 minute or 5 minutes or whatever, check the queue, and if any new entries, start processing. (see example here: Best Timer for using in a Windows service)
  2. Following a series of steps is generally called a workflow. In a workflow, you have a current status, and you update that status in each stage. So every row would begin in stage = 1. After first step, stage = 2, etc, etc. On exception, it will be on the stage it left off, which will then start the process over again on that stage, or whatever your logic is. This status would be stored with each row, and the dispatch code would check the status and send the service to the correct starting code for the current stage. I.e., think of a set of If statements based on the status.
  3. Handling exceptions is very simple. Every unit of work should be wrapped in a try...catch block. On error, log the exception, and mark the row according to your business rules.

As far as the implementation, use programming best practices to keep your code clean, modular, neat and organized. As you develop the solution, bring specific questions back for more help.

Community
  • 1
  • 1
mellamokb
  • 53,762
  • 11
  • 101
  • 131
  • Yes, all these is obvious. What worries me is managing the workflow and all the stage switching. As I have stated, processing is not typical for all the cases, and ending up with writing numerous switch blocks would not be a pleasure. I would like to know, is there some flexible and more intelligent way to connect the stage of execution with some concrete method? – 26071986 Jun 01 '12 at 14:43
  • Technically I think that's what [Windows Workflow Foundation](http://msdn.microsoft.com/en-us/netframework/aa663328.aspx) is for, but I've never used it. – mellamokb Jun 01 '12 at 14:45
  • If your workflow is much more complicated than the example in your question, you should consider [Windows Workflow Foundation](http://msdn.microsoft.com/en-us/library/dd851337.aspx) – Eren Ersönmez Jun 01 '12 at 14:48
  • Eren Ersönmez, I have never ever worked with WWF, but I've read about it today. The thing that's not clear for me: now, by using inheritance and so one, I can use overriding to expand existing methods in base class or to change their behaviour. Is something like this possible in workflows or I will end up creating a complete workflow every time I need only a slight change? – 26071986 Jun 01 '12 at 14:56
0

Add a field to your database table which tracks the state of each row. You could call this new field ProcessingState for example.

As the row goes through each logical state you can update this ProcessingState field to identify which state the row is in.

Each logical step in your service should just work rows that are in the appropriate state.

Here is an example, lets say you have five logical steps to work through. You could have the following states;

  1. Waiting State 1.
  2. State 1 complete
  3. Waiting State 2
  4. State 2 complete

etc..

Good luck.

RDotLee
  • 1,021
  • 2
  • 15
  • 31