Class Win32ServiceManager

  1. lib/win32_service_manager.rb
Parent: Object
Win32ServiceManager dot/f_3.png

Methods

public class

  1. load_dependencies
  2. new
  3. version

public instance

  1. create
  2. delete
  3. list
  4. services
  5. start
  6. status
  7. stop

Constants

VERSION = '0.1.3'

Public class methods

load_dependencies ()
[show source]
# File lib/win32_service_manager.rb, line 7
  def self.load_dependencies; require 'win32/service'; end
new (name_key = '')

Construct a new service manager, just sets up a name_key, which is used as a leading string to the name of all services managed by the instance.

[show source]
# File lib/win32_service_manager.rb, line 11
  def initialize(name_key = '')
    self.class.load_dependencies
    @name_key = name_key
  end
version ()
[show source]
# File lib/win32_service_manager.rb, line 6
  def self.version; VERSION; end

Public instance methods

create (name, command, description = nil, options = {})

Create a new service. The service name will be appended to the name_key and inserted into the registry using Win32::Service. One recommended pattern is to store persisence details about the service as yaml in the optional description field.

[show source]
# File lib/win32_service_manager.rb, line 20
  def create(name, command, description = nil, options = {})
    defaults = {
      :service_type       => Win32::Service::WIN32_OWN_PROCESS,
      :start_type         => Win32::Service::AUTO_START,
      :error_control      => Win32::Service::ERROR_NORMAL
    }
    defaults.merge!(options)
    name = n(name)
    options = defaults.merge(
      :display_name       => name,
      :description        => description || name,
      :binary_path_name   => command
    )
    Win32::Service.create(name, nil, options)
  end
delete (name)

Mark a service for deletion (note, does not terminate the service)

[show source]
# File lib/win32_service_manager.rb, line 37
  def delete(name)
    Win32::Service.delete(n(name))
  end
list (name = nil)

Alias for status

services ()

List all services that have names which start with the name_key.

[show source]
# File lib/win32_service_manager.rb, line 61
  def services
    Win32::Service.services.select do |svc|
      # TODO in future, 1.8.7, can use start_with?
      svc.display_name[0,@name_key.size] == @name_key
    end
  end
start (name)

Schedule the service for start

[show source]
# File lib/win32_service_manager.rb, line 42
  def start(name)
    Win32::Service.start(n(name))
  end
status (name = nil)

Returns an array of hashes derived from the Win32::Service::SvcInfo struct The optional argument will cut the array down to a single element for a service with the given name.

[show source]
# File lib/win32_service_manager.rb, line 54
  def status(name = nil)
    list = name ? services.select { |s| s.display_name == name } : services
    list.map { |svc_info| svc_info.to_hash }
  end
stop (name)

Schedule the service for stop

[show source]
# File lib/win32_service_manager.rb, line 47
  def stop(name)
    Win32::Service.stop(n(name))
  end