marcelotduarte / cx_Freeze

cx_Freeze creates standalone executables from Python scripts, with the same performance, is cross-platform and should work on any platform that Python itself works on.
https://marcelotduarte.github.io/cx_Freeze/
Other
1.32k stars 215 forks source link

Request additional time when stopping a windows service #2489

Open l0ner opened 2 months ago

l0ner commented 2 months ago

Hi

I have written a windows service that periodically processes a lot data. Because of how it works and what it does, when a stop gets requested it needs to finish processing, and only then it can stop.

Currently if i request the service to stop while it is processing the data the windows will throw an error sayig that the service did not stop in timely manner.

In c# one can call RequestAdditionalTime() function, to keep the windows happy while the service is stopping. Can this be somehow be achieved in cx_freeze generated windows services?

This could also be useful when starting a service that takes a long time for initialization.

marcelotduarte commented 1 month ago

In c# one can call RequestAdditionalTime() function, to keep the windows happy while the service is stopping. Can this be somehow be achieved in cx_freeze generated windows services?

We can implement this. Can you do it (if you use C) or at least find the equivalent function in C so I can implement it?

l0ner commented 1 month ago

Unfortunately I'm not a C programmer.

But i guess the equivalent functionality could be achieved by calling SetServiceStatus with value of SERVICE_STOP_PENDING repedately.

I have since implemented my service using the win32service library, and doing this works:

while has_running_tasks():
    logger.info("Waiting for still running tasks to finish")
    time.sleep(5)
    self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)

The windows still will report that the service "did not stop in a timely manner" (but will take more time than normally), and will not kill it forcefully, allowing the service to stop properely (i had one taks that took additional 30 minutes to complete after asking windows to stop the service and the task finished normally and then the service did stop).