Merge pull request #18853 from thematthopkins/windows_service_speed_up

Significantly speeds up highstate on Windows by reducing the calls to query all services.
This commit is contained in:
David Boucha 2014-12-09 23:42:47 -07:00
commit 5df4135d36

View File

@ -54,29 +54,7 @@ def get_enabled():
salt '*' service.get_enabled
'''
if has_powershell():
cmd = 'Get-WmiObject win32_service | where {$_.startmode -eq "Auto"} | select-object name'
lines = __salt__['cmd.run'](cmd, shell='POWERSHELL').splitlines()
return sorted([line.strip() for line in lines[3:]])
else:
ret = set()
services = []
cmd = list2cmdline(['sc', 'query', 'type=', 'service', 'state=', 'all', 'bufsize=', str(BUFFSIZE)])
lines = __salt__['cmd.run'](cmd).splitlines()
for line in lines:
if 'SERVICE_NAME:' in line:
comps = line.split(':', 1)
if not len(comps) > 1:
continue
services.append(comps[1].strip())
for service in services:
cmd2 = list2cmdline(['sc', 'qc', service, str(BUFFSIZE)])
lines = __salt__['cmd.run'](cmd2).splitlines()
for line in lines:
if 'AUTO_START' in line:
ret.add(service)
return sorted(ret)
return sorted([service for service in get_all() if enabled(service)])
def get_disabled():
@ -89,30 +67,7 @@ def get_disabled():
salt '*' service.get_disabled
'''
if has_powershell():
cmd = 'Get-WmiObject win32_service | where {$_.startmode -ne "Auto"} | select-object name'
lines = __salt__['cmd.run'](cmd, shell='POWERSHELL').splitlines()
return sorted([line.strip() for line in lines[3:]])
else:
ret = set()
services = []
cmd = list2cmdline(['sc', 'query', 'type=', 'service', 'state=', 'all', 'bufsize=', str(BUFFSIZE)])
lines = __salt__['cmd.run'](cmd).splitlines()
for line in lines:
if 'SERVICE_NAME:' in line:
comps = line.split(':', 1)
if not len(comps) > 1:
continue
services.append(comps[1].strip())
for service in services:
cmd2 = list2cmdline(['sc', 'qc', service])
lines = __salt__['cmd.run'](cmd2).splitlines()
for line in lines:
if 'DEMAND_START' in line:
ret.add(service)
elif 'DISABLED' in line:
ret.add(service)
return sorted(ret)
return sorted([service for service in get_all() if disabled(service)])
def available(name):
@ -154,7 +109,16 @@ def get_all():
salt '*' service.get_all
'''
return sorted(get_enabled() + get_disabled())
ret = set()
cmd = list2cmdline(['sc', 'query', 'type=', 'service', 'state=', 'all', 'bufsize=', str(BUFFSIZE)])
lines = __salt__['cmd.run'](cmd).splitlines()
for line in lines:
if 'SERVICE_NAME:' in line:
comps = line.split(':', 1)
if not len(comps) > 1:
continue
ret.add(comps[1].strip())
return sorted(ret)
def get_service_name(*args):
@ -347,7 +311,12 @@ def enabled(name, **kwargs):
salt '*' service.enabled <service name>
'''
return name in get_enabled()
cmd = list2cmdline(['sc', 'qc', name])
lines = __salt__['cmd.run'](cmd).splitlines()
for line in lines:
if 'AUTO_START' in line:
return True
return False
def disabled(name):
@ -360,4 +329,11 @@ def disabled(name):
salt '*' service.disabled <service name>
'''
return name in get_disabled()
cmd = list2cmdline(['sc', 'qc', name])
lines = __salt__['cmd.run'](cmd).splitlines()
for line in lines:
if 'DEMAND_START' in line:
return True
elif 'DISABLED' in line:
return True
return False