IWantToRunWhenBusStartsAndStops not for production?

New to NServiceBus (4.7.5) and just implemented the NSB hosting server host.exe (implementing IWantToRunWhenBusStartsAndStops) that detects changes in database tables and notifies subscribed web applications by posting events eg. "CustomerDataWasUpdatedEvent". In the future we will be doing the actual update with the message handlers receiving commands, obviously, but for now this publishing service is just checking the database, etc.

Everything works well, however, as I got closer to production, I noticed that David Boyke in his latest issue of "Learning NServiceBus" states that the classes that implement IWantToRunWhenBusStartsAndStops are mostly for development and rarely used in production. I have set up database change detection in the Start method and it works well, does anyone know why this is discouraging?

Here's a comment in a real book:

https://books.google.se/books?id=rvpzBgAAQBAJ&pg=PA110&lpg=PA110&dq=nservicebus+iwanttorunwhenbusstartsandstops+in+production+david+boike&source=bl&ots=U6sNII0nm3&sig=qIXffOVFhcy-_3qDnSExRpwRlD4&hl=sv&sa=X&ei=lHWRVc2_BKrWywPB65fIBw&ved=0CBsQ6AEwAA#v= onepage & q = nservicebus% 20iwanttorunwhenbusstartsandstops% 20in% 20production% 20david% 20boike & f = false

+3


source to share


2 answers


I will copy the answer I gave to Google Group Special Software ...


I'll quote myself directly here:

The IWantToRunWhenBusStartsAndStops implementation is a great place to create a quick interface for testing messages while debugging, allowing messages to be sent based on console input. In addition, they are often widely used in a production system. One possible use case for the product would be to provide the resource that the endpoint needs at startup, and then tear it off when the endpoint stops.

I think that if I could add a little emphasis it would be "widespread". I'm not trying to say that you won't / cannot have IWantToRunWhenBusStartsAndStops in production code, or that avoiding them is best practice. What I'm trying to say is that a ton of them are probably code smell.



Above this paragraph in the book, I warn you that IWantToRunWhenBusStartsAndStops doesn't have any external transactions or try / trick. This is really the key part. If you end up throwing an exception in IWantToRunWhenBusStartsAndStops, you might run into big problems. If you use something like .NET Timer and then throw an exception, you could compromise your process!

Let me tell you how I messed it up with my first ever NServiceBus system. The system (still in use today from what I hear) is responsible for ingesting over 3000 RSS feeds (probably a lot more than it is now) into the CMS. So handle each feed by splitting it into elements, resizing the images, encoding the attached video for mobile ... all of this stuff was handled by NServiceBus message handlers that were scaled across multiple servers and it was fantastic.

The problem was the scheduler. I implemented this as IWantToRunWhenBusStartsAndStops (well, actually IWantToRunAtStartup at the time) and it quickly turned into a mess. I've saved all the information about the number of chips in memory so that I can calculate when to cancel the next ProcessFeed command. I used the .NET Timer class and IIRC, I ended up having to use threading primitives like ManualResetEvent to coordinate the actions. And since I was using the .NET Timer, if the scheduler threw an exception, that endpoint failed and had to restart. Lots of weird edge cases, and it has always been a bog of errors. In addition, it was now a single-user "commander application", so when the feed / item processors could be scaled, the scheduler could not.

As I got more proficient with NServiceBus, I realized that each feed had to be a saga, starting with the FeedCreated event, driven by the PauseProcessing and ResumeProcessing commands, using timeouts to control the next processing time, and finally (possibly) via the FeedRemoved event ... It would be much simpler and everything would be done inside transaction driven message handlers.

This experience made me a little distrustful / skeptical about IWantToRunWhenBusStartsAndStops. Not to say this is bad, just something to be aware of. Always be prepared to consider if what you are trying to do might not be achieved in a different way.

+3


source


Actual quote:

... usually not widespread in a production system.

Unusually this is not the same as discouraging.

This suggests that I think the author intends to emphasize here the fact that later on on the page they argue that this is not a good place for a lot of coding, as an unhandled exception could cause the whole process to fail.



In fact, the author mentions a possible use case where you want to load a resource to work in a handler.

Ok, maybe just this scenario we have a little unusual

Agreed - there is nothing fundamentally wrong with your approach. I recently did the same thing as connecting a SqlDependency to listen for database events and then posted a message as a result. In these scenarios, there is literally nothing you can do but use IWantToRunAtStatup.

Also, David himself frequently picks the nservicebus tag, maybe he will provide a more definitive answer than mine.

+4


source







All Articles