class Chef::Provider::Service::Windows

Constants

AUTO_START

Win32::Service.get_start_type

CONTINUE_PENDING
DISABLED
PAUSED
PAUSE_PENDING
RUNNING

Win32::Service.get_current_state

START_PENDING
STOPPED
STOP_PENDING
TIMEOUT

Public Instance Methods

disable_service() click to toggle source
# File lib/chef/provider/service/windows.rb, line 145
def disable_service
  if Win32::Service.exists?(@new_resource.service_name)
    if start_type == AUTO_START
      Win32::Service.configure(
        :service_name => @new_resource.service_name,
        :start_type => Win32::Service::DISABLED
      )
      @new_resource.updated_by_last_action(true)
    else
      Chef::Log.debug "#{@new_resource} already disabled - nothing to do"
    end
  else
    Chef::Log.debug "#{@new_resource} does not exist - nothing to do"
  end
end
enable_service() click to toggle source
# File lib/chef/provider/service/windows.rb, line 129
def enable_service
  if Win32::Service.exists?(@new_resource.service_name)
    if start_type == AUTO_START
      Chef::Log.debug "#{@new_resource} already enabled - nothing to do"
    else
      Win32::Service.configure(
        :service_name => @new_resource.service_name,
        :start_type => Win32::Service::AUTO_START
      )
      @new_resource.updated_by_last_action(true)
    end
  else
    Chef::Log.debug "#{@new_resource} does not exist - nothing to do"
  end
end
load_current_resource() click to toggle source
# File lib/chef/provider/service/windows.rb, line 50
def load_current_resource
  @current_resource = Chef::Resource::Service.new(@new_resource.name)
  @current_resource.service_name(@new_resource.service_name)
  @current_resource.running(current_state == RUNNING)
  Chef::Log.debug "#{@new_resource} running: #{@current_resource.running}"
  @current_resource.enabled(start_type == AUTO_START)
  Chef::Log.debug "#{@new_resource} enabled: #{@current_resource.enabled}"
  @current_resource
end
restart_service() click to toggle source
# File lib/chef/provider/service/windows.rb, line 114
def restart_service
  if Win32::Service.exists?(@new_resource.service_name)
    if @new_resource.restart_command
      Chef::Log.debug "#{@new_resource} restarting service using the given restart_command"
      shell_out!(@new_resource.restart_command)
    else
      stop_service
      start_service
    end
    @new_resource.updated_by_last_action(true)
  else
    Chef::Log.debug "#{@new_resource} does not exist - nothing to do"
  end
end
start_service() click to toggle source
# File lib/chef/provider/service/windows.rb, line 60
def start_service
  if Win32::Service.exists?(@new_resource.service_name)
    state = current_state
    if state == RUNNING
      Chef::Log.debug "#{@new_resource} already started - nothing to do"
    elsif state == START_PENDING
      Chef::Log.debug "#{@new_resource} already sent start signal - waiting for start"
      wait_for_state(RUNNING)
    elsif state == STOPPED
      if @new_resource.start_command
        Chef::Log.debug "#{@new_resource} starting service using the given start_command"
        shell_out!(@new_resource.start_command)
      else
        spawn_command_thread do
          Win32::Service.start(@new_resource.service_name)
        end
        wait_for_state(RUNNING)
      end
      @new_resource.updated_by_last_action(true)
    else
      raise Chef::Exceptions::Service, "Service #{@new_resource} can't be started from state [#{state}]"
    end
  else
    Chef::Log.debug "#{@new_resource} does not exist - nothing to do"
  end
end
stop_service() click to toggle source
# File lib/chef/provider/service/windows.rb, line 87
def stop_service
  if Win32::Service.exists?(@new_resource.service_name)
    state = current_state
    if state == RUNNING
      if @new_resource.stop_command
        Chef::Log.debug "#{@new_resource} stopping service using the given stop_command"
        shell_out!(@new_resource.stop_command)
      else
        spawn_command_thread do
          Win32::Service.stop(@new_resource.service_name)
        end
        wait_for_state(STOPPED)
      end
      @new_resource.updated_by_last_action(true)
    elsif state == STOPPED
      Chef::Log.debug "#{@new_resource} already stopped - nothing to do"
    elsif state == STOP_PENDING
      Chef::Log.debug "#{@new_resource} already sent stop signal - waiting for stop"
      wait_for_state(STOPPED)
    else
      raise Chef::Exceptions::Service, "Service #{@new_resource} can't be stopped from state [#{state}]"
    end
  else
    Chef::Log.debug "#{@new_resource} does not exist - nothing to do"
  end
end
whyrun_supported?() click to toggle source
# File lib/chef/provider/service/windows.rb, line 46
def whyrun_supported?
  false
end

Private Instance Methods

current_state() click to toggle source
# File lib/chef/provider/service/windows.rb, line 162
def current_state
  Win32::Service.status(@new_resource.service_name).current_state
end
resource_timeout() click to toggle source
# File lib/chef/provider/service/windows.rb, line 181
def resource_timeout
  @resource_timeout ||= @new_resource.timeout || TIMEOUT
end
spawn_command_thread() { || ... } click to toggle source
# File lib/chef/provider/service/windows.rb, line 185
def spawn_command_thread
  worker = Thread.new do
    yield
  end

  Timeout.timeout(resource_timeout) do
    worker.join
  end
end
start_type() click to toggle source
# File lib/chef/provider/service/windows.rb, line 166
def start_type
  Win32::Service.config_info(@new_resource.service_name).start_type
end
wait_for_state(desired_state) click to toggle source

Helper method that waits for a status to change its state since state changes aren't usually instantaneous.

# File lib/chef/provider/service/windows.rb, line 172
def wait_for_state(desired_state)
  retries = 0
  loop do
    break if current_state == desired_state
    raise Timeout::Error if ( retries += 1 ) > resource_timeout
    sleep 1
  end
end