reliably stop windows services
reliably stop windows services

`net stop` issues a stop command to the target service and waits
briefly (~30s), but will give up if the service takes too long to stop
with a misleading "service could not be stopped" message. `net stop`
will report failure, but some time later the service may actually stop
on it's own.

The common solution for really knowing when a service is stopped is to
poll using `sc query`, which is what this patch does for failed `net
stop` commands.

Adds two configuration constants which might need tweaking:

* `SERVICE_STOP_DELAY_SECONDS` - how many seconds to wait in between poll
  attempts
* `SERVICE_STOP_POLL_MAX_ATTEMPTS` - how many poll attempts to make
  before giving up

Most of the batch-file based solutions posted by windows admins are
unbounded polling, but I'm not comfortable with infinite loops so I
added the max attempts guard.

Right now I have these set to 15s and 5 attempts, so salt will wait 5*15
= 75s for services to stop, which might need some tweaking.

See also:

* http://stackoverflow.com/questions/3325081/how-to-check-if-a-service-is-running-via-batch-file-and-start-it-if-it-is-not-r
* ​http://stackoverflow.com/questions/1405372/stopping-starting-a-remote-windows-service-and-waiting-for-it-to-open-close

saltstack/salt,saltstack/salt,saltstack/salt,saltstack/salt,saltstack/salt