Class: WinServiceManager

Inherits:
Object
  • Object
show all
Defined in:
lib/win-service-manager.rb

Instance Method Summary collapse

Constructor Details

#initialize(srv_any_path = "#{ENV['ProgramFiles']}\\Windows Resource Kits\\Tools\\srvany.exe") ⇒ WinServiceManager

Name your service, provice a path to SRVANY.exe - found in the MS Windows Resource Kit



6
7
8
9
# File 'lib/win-service-manager.rb', line 6

def initialize(srv_any_path = "#{ENV['ProgramFiles']}\\Windows Resource Kits\\Tools\\srvany.exe")
  @name_key = ''
  @srv_any_path = srv_any_path
end

Instance Method Details

#create(name, command, args = '', app_directory = '', 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. The arguments are then adjusted with win32-registry. One recommended pattern is to store persisence details about the service as yaml in the optional description field.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/win-service-manager.rb', line 16

def create(name, command, args = '', app_directory = '', description = nil, options = {})
defaults = {
  :service_type       => Win32::Service::WIN32_OWN_PROCESS,
  :start_type         => Win32::Service::DEMAND_START,
  :error_control      => Win32::Service::ERROR_NORMAL
  }.merge(options)
  name = @name_key + name.to_s
  options = defaults.merge(
  :display_name       => name,
  :service_name       => name,
  :description        => description || name,
  :binary_path_name   => @srv_any_path
  )
  Win32::Service.create(options)
  registry(name) do |reg|
    reg.create('Parameters') do |params|
      params.write_i("Start", 3)
      params.write_s("Application", command)
      params.write_s("AppParameters", args)
      params.write_s("AppDirectory", app_directory)
    end
  end
end

#delete(name) ⇒ Object

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



41
42
43
# File 'lib/win-service-manager.rb', line 41

def delete(name)
  Win32::Service.delete(@name_key + name.to_s)
end

#listObject

Returns an array of tuples of name and description



54
55
56
57
58
59
60
61
62
63
# File 'lib/win-service-manager.rb', line 54

def list
  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
  services.map do |svc_info|
    name = svc_info.display_name
    [name.slice(@name_key.size, name.size), svc_info.description]
  end
end

#start(name) ⇒ Object



45
46
47
# File 'lib/win-service-manager.rb', line 45

def start(name)
  Win32::Service.start(@name_key + name.to_s)
end

#stop(name) ⇒ Object



49
50
51
# File 'lib/win-service-manager.rb', line 49

def stop(name)
  Win32::Service.stop(@name_key + name.to_s)
end