What is the safe overhead for RequestAdditionalTime ()?

I have a Windows service that spawns a set of child actions in separate threads, and this should end only after all these actions have completed successfully. I do not know in advance how long it will take to terminate after receiving a stop signal. During OnStop (), I wait at intervals for this stop signal and continue to request additional time until the system wishes to provide it.

Here is the basic structure:

class MyService : ServiceBase { private CancellationTokenSource stopAllActivities; private CountdownEvent runningActivities; protected override void OnStart(string[] args) { // ... start a set of activities that signal runningActivities // when they stop // ... initialize runningActivities to the number of activities } protected override void OnStop() { stopAllActivities.Cancel(); while (!runningActivities.Wait(10000)) { RequestAdditionalTime(15000); // NOTE: 5000 added for overhead } } } 

How much can I add β€œoverhead” in a RequestAdditionalTime call? I am concerned that the requests are cumulative and not based on the point in time when the RequestAdditionalTime call is made. If this happens, adding overhead can lead to the system eventually rejecting the request because it is too far in the future. But if I do not add unnecessary overhead, my service may be terminated before she has the opportunity to request the next block of extra time.

+5
source share
1 answer

This post was not entirely encouraging:

The MSDN documentation does not mention this, but it seems that the value specified in RequestAdditionalTime is not really extra time. Instead, it replaces the value in ServicePipeTimeout. Worse, any value greater than two minutes (120,000 milliseconds) is ignored, i.e. Closes in two minutes.

I hope this is not the case, but I am posting this as the worst answer.

UPDATE: The author of this post was kind enough to post a very detailed response to my comment, which I copied below.

Lars, the short answer is no.

I would say that now I understand that Windows Services should be designed to quickly start and complete processing when required.

As developers, we tend to focus on the implementation of the processing, and then pack it and provide it as a Windows service.

However, this is really not the right approach to designing Windows services. Services should be able to respond quickly to start and stop requests, not only when the administrator makes a request from the services console, but also when the operating system asks for a start as part of its initial processing or stop, as it shuts down,

Consider what happens when Windows is set to shut down, when the UPS indicates a power failure. Not suitable for the service to answer "I need a few more minutes ...".

It is possible to write services that respond quickly to requests termination, even when they implement lengthy processing tasks. Typically, a lengthy process will consist of batch processing of data, and processing should check to see if a stop has been requested at the smallest unit of work that ensures data consistency.

As an example, the first service where I found a stop timeout was a problem with processing a notification queue on a remote server. Processing received a notification from the queue, calling a web service to retrieve data associated with the notification object, and then writing a data file for processing by another application.

I implemented processing as a timer call for one method. After a method call, it does not return until all notifications in the queue have been processed. I realized that this is a mistake for the Windows service, because sometimes there can be tens of thousands of notifications in the queue, and processing can take several minutes.

The method is capable of processing 50 notifications per second. So what I had to do was do a check to see if a stop was requested before each notification was processed. This would allow the method to return when it completed the processing of the notification, but before it began to process the next notification. This would ensure that the service responds quickly to a stop request, and all pending notifications remain in the queue for processing when the service restarts.

+5
source

All Articles