Home > 2015

2015

Quartz.NET Tip: Logging and Troubleshooting

Monday, September 21, 2015 Category : , , 0

On one of my projects, we are using Quartz.net for scheduling jobs. There is a lot of documentation, blogs, and forums available for the same but some things you only learn by experience.

For the past few days, I have been troubleshooting memory leaks in the WCF service that is using Quartz.NET and one important learning from all that was to turn the Quartz.NET logging on whenever there is an issue. It takes some time to configure, download the Nuget packages, and fight though the dll hell of Log4Net, which, unfortunately, may be being used in other projects of your solution sharing the same bin. But once configured,it solves a lot of mysteries - the thread pool size being used, whether correct settings are being picked or not, whether any unhandled exceptions were thrown by any of the jobs, when jobs are being loaded and prepared for execution etc.

Quartz.net can be configured to log in a separate file, or inline in the application's own log4net appender.

One caveat though, if there are a lot of jobs. the log file size would increase swiftly so this should not be turned on permanently unless there is server space available, specially for continuous running services with cleanup/archiving disabled or occurring rarely.

IIS - Continuous Running Service - Recycling vs Idle Timeout

Saturday, September 19, 2015 Category : , 0

Hosting a Continuous Running Service in IIS

Generally when a .NET service or application is deployed to IIS under a new application pool, the Idle Timeout is set as 20 for the app pool and the recycling is enabled every 1740 minutes.

Idle Timeout

Time after which, if no requests are received, the app pool would release all memory and become idle. The app pool still remains active, and can start processing requests as soon as they arrive.

Recycling

Recycling defines the conditions that will trigger a release of the app pool's worker process resources, such as memory, threads etc. Recycling internally creates a new application pool and continues to process old requests using the old application pool till the new one is ready to accept requests. Once the new application pool is ready, the old application pool is gracefully shutdown, after all executing requests are done while any new requests are sent only to the new application pool.


Continuous Running Service

In case the service or application is to be kept alive forever, regardless of whether there are any requests coming in or not, these settings need to be changed to:

Idle Timeout: 0
Recycling: Off or based on memory consumption (recommended)

For example, services that are performing background tasks, like generating scheduled reports, processing data, etc. should keep running 24/7 irrespective of whether any requests are arriving for processing or not.

Recycling based on memory provides the advantage of having a continuous running application without worrying about memory leaks, in which case a continuous running application would keep consuming more and more memory and will finally start throwing unpredictable exceptions. In my case, these exceptions ranged from weird Win32 exceptions from 3rd party DLLs to OutOfMemory exceptions that were more intelligible.

Is Recycling the Solution?

Although recycling does solve the purpose of never having to find the service entirely stopped or memory choked, it is not really a solution, its is more of a checkpoint. It keeps the pool clean and does it gracefully for incoming requests, but it also stops running background threads unexpectedly. However, even if there are no background thread running, the application should be healthy and well coded enough to not need a recycle everyday.

This blog post focuses on how to identify issues with applications hosted in IIS that make them need frequent recycles to function correctly.

HTML5 Experiment - I

Saturday, April 11, 2015 Category : , , 0


This is a by-product of something I am trying to make with HTML5. Just posted here to check if blogger supports it. Click to view. If you feel like looking at the code for any reason, View Source!

Rx Observable Event Dissection

Monday, January 12, 2015 Category : , , 0


So, in the last post, we discussed a simple example of how RX can be used for subscribing to an event and receive updated whenever that event is fired. This post discusses the code for defining an "observable" a bit in detail.

So, the code that was used for defining the observable event was:
 
IObservable<EventPattern<object>> eventAsObservable = Observable.FromEventPattern(  
               ev => dg.ev += ev,  
               ev => dg.ev -= ev);   

The FromEventPattern method should be used when subscribing to an event that conforms to a standard .NET event pattern, i.e. of the form:
 
public delegate void EventHandler(object sender, EventArgs e);

But what if we don't have a standard .NET event? What if we have defined our own event with custom event arguments? For example, say the event is:
 
event MarketDataReceivedHandler DataReceived; 

Where MarketDataReceivedHandler is defined as:
 
public delegate void MarketDataReceivedHandler(MarketDataEventArgs updates); 

With MarketDataEventArgs class  defined as:
 
public class MarketDataEventArgs : EventArgs { public MarketData Payload { get; set; } }

In this case, RX provides another method for subscription - Observable.FromEvent

This method does the same thing as the FromEventPattern method. For FromEventPattern, we only defined a lambda for subscribing to the target event. For FromEvent also, we can do the same:
 
eventAsObservable = Observable.FromEvent<MarketDataReceivedHandler, MarketDataEventArgs>(
                    mktH => _provider.DataReceived += mktH,
                    mktH => _provider.DataReceived -= mktH
                    ); 

The above code translates to the following statement: "Define an observable of type MarketDataReceivedHandler, which when raised, provides contains arguments of type MarketDataEventArgs and also, remove the makeshift handler when the subscription is disposed."

Powered by Blogger.