An update on the service shutdown problem...
--- Begin Message ---
Mark,

Thought you'd like to know that after much searching, I have the answer.

Microsoft introduced SERVICE_CONTROL_PRESHUTDOWN (post XP/Svr 2003) in order to 
address precisely this problem for services needing to manage the system 
shutdown process lifetime (eg. Exchange and SQL Server). Windows does NOT 
respect waitHint values once SERVICE_CONTROL_SHUTDOWN has been dispatched to 
services by the SCM, and as discovered, the eventlog service has usually 
stopped by this time anyway.
 
It was straightforward to incorporate this functionality into your 
ServiceFramework class:
Ensure that SERVICE_ACCEPT_PRESHUTDOWN is returned by GetAcceptedControls() by 
overriding the base class.
Ensure that you receive SERVICE_CONTROL_PRESHUTDOWN events by checking for this 
in SvcOtherEx() and calling the appropriate instance method if the attribute 
exists.
The SCM now awaits on your SERVICE_STOP_PENDING calls or will timeout (default 
msec in registry) waiting on your response. Be sure to regularly respond to the 
SCM by/before your last waitHint time window elapses.
The waitHint in your SERVICE_STOP_PENDING calls *are* respected during 
preshutdown (there is actually no limit on how long the OS will wait as long as 
your service remains in the *_STOP_PENDING state and regular calls are made to 
update the SCM), and you can happily log events to the event log etc.
I appreciated your feedback. Thanks again.

HTH,

Rob

On Nov 4, 2012, at 8:33 PM, Mark Hammond <mhamm...@skippinet.com.au> wrote:

> [re-adding python-win32]
> 
> On 5/11/2012 12:21 PM, Rob McGillivray wrote:
>> Mark,
>> 
>> You are correct. I wrote to a file and the SvcShutdown() event is
>> being fired. My question is … surely the event log service would only
>> stop once all other dependent services have also stopped?
> 
> Is your service dependent on the eventlog service?  IIUC, the eventlog 
> service just immediately shuts down when it set the shutdown event.
> 
>> How do
>> other services manage to write shutdown type notifications to the
>> event log upon shutdown? Is there some Windows event log resource
>> that I can grab and hold until I've had an opportunity to log my
>> 'exit' events to prevent premature shutdown of the event log
>> service?
> 
> I guess you could explicitly make it depend on the eventlog service?
> 
> Mark
> 
> 
> 
>> 
>> Your thoughts would be appreciated.
>> 
>> Kind regards,
>> 
>> Rob
>> 
>> On Nov 4, 2012, at 8:08 PM, Rob McGillivray <r...@mymcgillivray.com>
>> wrote:
>> 
>>> Hi Mark,
>>> 
>>> Much appreciate the feedback! Many thanks.
>>> 
>>> I'll try writing something to a file and check this out … I never
>>> thought that the event log service could/would stop(?). Doesn't the
>>> event log service run continuously and support all Windows services
>>> logging requests? Or do you get some handle to an 'event logger'
>>> object upon launch of python service.exe? I do have a book on NT
>>> Services that I'll peruse a little more based on your feedback.
>>> 
>>> My understanding from your chapter on services is that the service
>>> only ceases to 'exist' once SvcDoRun ends, so I thought I was safe
>>> signaling on SvcShutdown event. If the shutdown is happening so
>>> quickly, then I am concerned, because the 'cleanup/shutdown' code
>>> may take 10-15sec to complete. I thought I could advise the SCM of
>>> this delay, since the default timeout shows a wait hint of 5sec?
>>> Clearly my understanding is a little foggy here, so any
>>> enlightenment would be appreciated.
>>> 
>>> Also appreciate the comment on the fragility of my use of events.
>>> It's my first time using them, and much of what I coded I
>>> 'translated' from the NT Services book (written in C++). Funny
>>> thing is that I started with sleep function as you describe with a
>>> timeout of 1000msec, and then 'improved' it to it's current state.
>>> I'll recode it in the light of your feedback. Thanks again.
>>> 
>>> PS: I also bought your Programming Python to teach this old(er) dog
>>> new tricks, and it's been a great read, showing a sense of humor
>>> and comic relief quite devoid most dev books on my shelf. :-)
>>> 
>>> Kind regards,
>>> 
>>> Rob
>>> 
>>> 
>>> On Nov 4, 2012, at 7:35 PM, Mark Hammond <skippy.hamm...@gmail.com>
>>> wrote:
>>> 
>>>> On 4/11/2012 1:20 PM, Rob McGillivray wrote:
>>>>> Hi All,
>>>>> 
>>>>> For the life of me I cannot figure out why SvcShutdown() is not
>>>>> being called when the OS shuts down. All the other SCM
>>>>> notifications (pause/continue/stop) work just fine. I have
>>>>> searched high & low for solutions, but nothing. What appears to
>>>>> be a slam-dunk for others completely evades me. I must be
>>>>> missing something, so I bought a copy of Programming on Win32,
>>>>> but the Services section hasn't shed any more light on the
>>>>> issue, so I humbly ask for enlightenment. :-) What the heck am
>>>>> I missing??
>>>>> 
>>>>> Your help would be much appreciated!
>>>>> 
>>>>> Thanks in advance,
>>>>> 
>>>>> Rob
>>>> 
>>>> I'm guessing here, but...
>>>> 
>>>>> def SvcShutdown(self):
>>>>> self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
>>>>> self.logEvent('**SvcShutdown event**')
>>>> 
>>>> It is possible that by the time you get here the eventlog service
>>>> has also stopped.  Have you tried to verify the call any other
>>>> way (eg, writing a line to a file?
>>>> 
>>>>> # Shutdown code here... win32event.SetEvent(self.evStop)
>>>> 
>>>> Your shutdown code probably shouldn't be there - it should be in
>>>> the main SvcDoRun function - how much of a problem this is
>>>> probably depends on how much work it does.
>>>> 
>>>> Also, your use of events seems a little fragile - would it be
>>>> possible for you to use manual reset events, so that your
>>>> notificationFromSCM() function doesn't reset the event?
>>>> 
>>>> Even better would be to remove it completely - your sleep()
>>>> function could just a WaitForMultipleObjects with a timeout of
>>>> sec*1000.
>>>> 
>>>> So if you still have problems, it might be best to demonstrate it
>>>> with one of the sample pywin32 services so (a) we can reproduce
>>>> it and (b) it eliminates some of the complexity in your real
>>>> service.
>>>> 
>>>> HTH,
>>>> 
>>>> Mark
>>>> 
>>> 
>> 
>> 
> 


--- End Message ---
_______________________________________________
python-win32 mailing list
python-win32@python.org
http://mail.python.org/mailman/listinfo/python-win32

Reply via email to