How to Stop a Windows Service for long running Jobs? - c#

Currently I am using Windows Service to keep on running some jobs in the background. These jobs are scheduled via Quartz.Net. Generally these are long running jobs, spans from 5 min to 15 min. If a job is currently running and if User will try to Shutdown the Windows Service then it throws Exception: Windows service did not respond in a timely fashion.
Actually OnShutdown() method of WindowsService, I am trying to Shutdown the Quartz.Net also.
I am calling scheduler.Shutdown(true) to stop the scheduler & wait for running jobs. If I will make this False then Windows Service Stops normally but it will put the running Job in Unstable state. Please help me on this.

You get this error because your service did not complete the Stop in the agreed-upon time. So, the SCM assumes it's hung in some way and aborts it.
To tell the SCM you need more time to complete the operation, call ServiceBase.RequestAdditionalTime() in your OnStop handler. For more information see http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicebase.requestadditionaltime.aspx

Related

How to finish code execution after stopping windows service?

I'm using a simple windows service app. Some of the code takes around a minute to finish executing. If I want to rollout news changes to production, how do I ensure that even after I stopped the service, the service finished what ever it's executing?
Your new service would need to check to make sure the old process isn't running.
How can I verify if a Windows Service is running
A better way would be to fix your service so it can be shutdown in a reasonable length of time. A minute is much too long to stop a service.
Whatever it's doing needs to be able to cleanly stop or roll back on request.

WCF Service hard crashing Windows 2016

I have a web service running in IIS-10 on a Windows Server 2016 instance within a VM Hypervisor. A separate scheduled task calls functions of that web service during off peak times in order to retrieve status updates from a third party system. The scheduled task breaks the items that need to have statuses pulled into small batches and calls a function that retrieves / updates the records in parallel via Tasks and gives a return once all Tasks have completed.
Sometimes (every third time?), during this scheduled task, the app pool that the service is running on hangs. Log4Net stops logging, requests to the service do not get a response, IIS logging for the service is not updated with requests. There are no errors recorded in either my logs or in the windows event logs. When this happens, the app pool will stay in this state indefinitely. If I recycle the App Pool that the service is running on, the service will respond normally for ~30 seconds, and then the server will do a hard restart.
After the restart the event logs show the below error:
The computer has rebooted from a bugcheck. The bugcheck was: 0x00000139 (0x0000000000000003, 0xffffd60019506680, 0xffffd600195065d8, 0x0000000000000000).
The dmp file that is generated shows the same error code and identifies the file as ntoskrnl.exe.
All drivers are fully up to date. I have made sure all tasks and requests have timeouts. I have increased server resources past the point where that could be the cause. I have adjusted the batch size of items being processed.
I am out of troubleshooting ideas and would appreciate any help I can get.
I figured I would close this out in case anyone else has this very specific issue.
Digging through the dump, BHDRVX64.SYS (Symantec Antivirus) was on the stack immediately before the crash.
A 4 days later Symantec pushed an update https://support.symantec.com/en_US/article.INFO4367.html with a fix for the issue.
** If you hit a similar issue to this, start by uninstalling antivirus and seeing if the issue persists. After that, work through the list of kernel level processes returned by the 'fltmc' command in admin command prompt.

How does a windows service behaves on Windows shutdown?

Is there a timeout for stopping Windows Services when Windows is shutting down? I know there is a registry key "WaitToKillServiceTimeout" but does this timeout effect on services, when Windows is shutting down or is there another timeout?
I want to find out, how long system during shutdown waits for services when it calls the OnStop method of a service.
Can a service prevent Windows from shutting down?
From TechNet:
[WaitToKillServiceTimeout] Determines how long the system waits for services to stop after notifying the service that the system is shutting down.
Then yes, that's value used to wait for services shutdown but also note that it's shared for all services:
If all services stop before this value expires, the system shuts down...
For your question "Can a service prevent Windows from shutting down?" answer is "more or less". You can't prevent Windows to shutdown (it may be really annoying for users if they can't shutdown because a service decided they shouldn't) but:
When the value of this entry expires, the system notifies the user that the service has not stopped. The user can either force the service task to stop or continue to wait. If the user waits, this value specifies the interval between repeated user notices that the service has not stopped.
Note that:
Some services increase the value of this entry to provide more time for cleanup tasks.
In case your service is slow to shutdown you may increase such value (during installation) to give you more time. In theory using a very high value you'll postpone shutdown for a very long time (but this behavior isn't same as cancel it and don't forget it's shared for all services).
AFAIK a service can't cancel shutdown but a GUI application can do it using ShutdownBlockReasonCreate() function.

Quartz on a lightly loaded server

I have several important Quartz events that MUST go off at specific times of the night. Lately I have been noticing that not all the events are run. I have a feeling that overnight our server load is very light (ie. zero users) and that the web server kind of goes to sleep, and hence so does Quartz. Does this seem plausible? I am using Quartz.net within the web server, and not as a separate service.
Yes, that is plausible. In general, it is considered a bad practice to have IIS run scheduled tasks, that is a job better left for a Windows Service, or the built-in Windows Scheduled tasks (which has been much improved for Windows Server 2008).
Your worker process might have been shut down because there are no load. By default, IIS shuts down worker processes after 20 minutes without ingoing requests (you can alter this in the Application Pool settings). Also, worker processes are likely to be restarted after a certain amount of time or requests, or if they consume too much memory.
A quick-fix for your specific problem might be to use the Windows Task Scheduler to request the site periodically to keep it alive - or have it request a URL that triggers your task at the predefined time.
ASP.Net, by default, will shut down AppDomain's after a period of inactivity.
The recommended course of action is to implement timed events either in a Windows Service or as an executable launched from Window's Scheduled Tasks.
It is also possible to change the IIS configuration so that it will not shut down your AppDomain. How exactly this is accomplished varies between versions of IIS, but instructions can easily be found by searching.

Forcing windows to wait for a service to stop when it is shutting down

I'm having an issue that I can't figure out regarding a Windows Service I have written in C#. What I am trying to do is wait for a specified period for a child process to terminate before I am forced to kill it and stop the service. I have set my timeout for anywhere from 5 to 15 seconds and my service waits the appropriate amount of time and then kills the child and stops when I stop it via the mmc service window. However if I shutdown the computer my service gets blown away before it is able to wait and stop properly. I have read that Windows allows for a timeout of 30 seconds for a service to shutdown before it blows it away. My code is executing in much less time than 30 seconds, max is around 20 seconds or so, depending on what I set the timeout at.
I have tried using the SetServiceStatus() function in the win32 Api but it has not changed the function of the code. I have verified that the function call is succeeding. Is there any other way to force Windows to wait for my service to shutdown properly? I am testing it in Windows 7 x86, and Windows Vista Sp1 x86.
Windows will not let any service prevent shutdown, this is by-design. as Gyuri says, you have to consider a different design for your app.
This is a global Windows setting, apparently: http://support.microsoft.com/kb/305788. I would be worried if I had to rely on the users to let my service shut down. I would save my data periodically instead.
OnSTart and OnSTop are inherited from a base class which does quite a bit for you. All your implementations of these methods should do is start or stop your client code, windows will do the rest.
I'll caveat this with the fact that I use C# and .Net's implementation of ServiceBase does this, I cannot talk authoritatively about C/C++ code or APIs.

Categories

Resources