Class: Win32ServiceManager

Inherits:
Object
  • Object
show all
Defined in:
lib/win32_service_manager.rb

Constant Summary collapse

VERSION =
'0.1.3'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name_key = '') ⇒ Win32ServiceManager

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.



11
12
13
14
# File 'lib/win32_service_manager.rb', line 11

def initialize(name_key = '')
  self.class.load_dependencies
  @name_key = name_key
end

Class Method Details

.load_dependenciesObject



7
# File 'lib/win32_service_manager.rb', line 7

def self.load_dependencies; require 'win32/service'; end

.versionObject



6
# File 'lib/win32_service_manager.rb', line 6

def self.version; VERSION; end

Instance Method Details

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

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.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 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) ⇒ Object

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



37
38
39
# File 'lib/win32_service_manager.rb', line 37

def delete(name)
  Win32::Service.delete(n(name))
end

#servicesObject

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



61
62
63
64
65
66
# 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) ⇒ Object

Schedule the service for start



42
43
44
# File 'lib/win32_service_manager.rb', line 42

def start(name)
  Win32::Service.start(n(name))
end

#status(name = nil) ⇒ Object Also known as: list

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.



54
55
56
57
# 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) ⇒ Object

Schedule the service for stop



47
48
49
# File 'lib/win32_service_manager.rb', line 47

def stop(name)
  Win32::Service.stop(n(name))
end