0

I have a situation where a console app (let's call it the Builder) and an NServiceBus endpoint (let's call it the Feeder) are both writing to the same data store. The Feeder processes EntityChanged messages as they come in, whereas the Builder goes to a database and gets the latest information for all Entities and writes the necessary information to the data store in one huge batch.

A problem that is immediately apparent from looking at how the Builder works is that it will clobber any changes to the data store that the Feeder makes whilst the Builder is running. So, what I need to do is stop the Feeder from processing NSB messages whilst the Builder is running and then for it to simply resume processing when the Builder finishes.

At the moment we devs do this because we run the Builder manually, ad hoc; we literally stop the Feeder service when we're about to run the Builder and then restart it afterwards. Now that we're going to run the Builder on a schedule we need to automate this process.

Stopping the Feeder service in an automated fashion is not ideal because our monitoring and alerting tools will go nuts, and anyway it's a Windows service that's set to restart automatically.

Is there a function in NServiceBus that can accomplish this for us? I originally thought of message Scheduling or Timeouts, but they are specifically configured by the sending endpoint. I need to timeout messages on the receiving endpoint.

Richiban
  • 5,569
  • 3
  • 30
  • 42
  • This would appear to be a good use case for sagas.https://docs.particular.net/nservicebus/sagas/ – tom redfern Dec 04 '18 at 12:00
  • Thanks @tomredfern, but I've read up on Sagas in the past and I'm not sure how they'd help me here. My understanding is that Sagas are a mechanism to implement a process that executes only when two (or more) messages relating to the same Entity are received. Can you explain more? – Richiban Dec 04 '18 at 14:10
  • I don't think stopping the endpoint manually or otherwise is the best way to go. How about using an event when a logical batch is ready to process? The builder would publish an event saying Batch X is ready and then the Feeder listening to that event could go ahead and process only that batch. – Hadi Eskandari Dec 05 '18 at 00:58

1 Answers1

0

Sounds like a relatively simple answer for this would be to check for concurrency issues, so when the feeder goes to commit to the database it will need to check that the data row is still at the same state as when it first read it, otherwise throw a concurrency error. Then NServiceBus would be able to retry after that occurrence. Take a look at this link and it will explain a little more in depth as it sounds like you may need an optimistic locking strategy. http://www.agiledata.org/essays/concurrencyControl.html

dmoore1181
  • 1,793
  • 1
  • 25
  • 57