class WindowsServiceController

Constants

SERVICE_DISPLAYNAME
SERVICE_NAME

Public Class Methods

install(args = {}) click to toggle source
# File lib/envoi/mam/agent/daemon/windows_service_controller.rb, line 45
def install(args = {})
  # Quote the full path to deal with possible spaces in the path name.
  ruby = File.join(CONFIG['bindir'], CONFIG['ruby_install_name']).tr('/', '\\')
  path = __FILE__.tr('/', '\\')
  cmd = %Q("#{ruby}" "#{path}")

  install_args = {
      :service_name     => SERVICE_NAME,
      :display_name     => SERVICE_DISPLAYNAME,
      :binary_path_name => cmd
  }.merge!(args)

  Service.new(install_args)
  puts "Service `#{SERVICE_NAME}` installed"
end
pause() click to toggle source
# File lib/envoi/mam/agent/daemon/windows_service_controller.rb, line 104
def pause
  if Service.status(SERVICE_NAME).current_state != 'paused'
    Service.pause(SERVICE_NAME)
    while Service.status(SERVICE_NAME).current_state != 'paused'
      puts "One moment... #{Service.status(SERVICE_NAME).current_state}"
      sleep 1
    end
    puts "Service `#{SERVICE_NAME}` paused"
  else
    puts 'Already paused'
  end
end
resume() click to toggle source
# File lib/envoi/mam/agent/daemon/windows_service_controller.rb, line 117
def resume
  if Service.status(SERVICE_NAME).current_state != 'running'
    Service.resume(SERVICE_NAME)
    while Service.status(SERVICE_NAME).current_state != 'running'
      puts "One moment... #{Service.status(SERVICE_NAME).current_state}"
      sleep 1
    end
    puts "Service `#{SERVICE_NAME}` resumed"
  else
    puts 'Already running'
  end
end
run(command = nil, args = { }) click to toggle source
# File lib/envoi/mam/agent/daemon/windows_service_controller.rb, line 24
def run(command = nil, args = { })
  # You must provide at least one argument.
  raise ArgumentError, 'No argument provided.' unless command
  unless Service.exists?(SERVICE_NAME) || command == 'install'
    puts 'Service is not installed.'
    return
  end

  case command
  when 'start'; self.start
  when 'stop'; self.stop
  when 'pause'; self.pause
  when 'resume'; self.resume
  when 'status'; self.status
  when 'install'; self.install(args)
  when 'uninstall', 'delete'; self.uninstall
  else
    raise ArgumentError, 'unknown option: ' + command
  end
end
start() click to toggle source
# File lib/envoi/mam/agent/daemon/windows_service_controller.rb, line 73
def start
  if Service.status(SERVICE_NAME).current_state != 'running'
    # Service.start(SERVICE_NAME, nil, args)
    Service.start(SERVICE_NAME)
    while Service.status(SERVICE_NAME).current_state != 'running'
      puts "One moment... #{Service.status(SERVICE_NAME).current_state}"
      sleep 1
    end
    puts "Service `#{SERVICE_NAME}` started"
  else
    puts 'Already running'
  end
end
status() click to toggle source
# File lib/envoi/mam/agent/daemon/windows_service_controller.rb, line 87
def status
  puts "Service `#{SERVICE_NAME}` #{Service.status(SERVICE_NAME).current_state}"
end
stop() click to toggle source
# File lib/envoi/mam/agent/daemon/windows_service_controller.rb, line 91
def stop
  if Service.status(SERVICE_NAME).current_state != 'stopped'
    Service.stop(SERVICE_NAME)
    while Service.status(SERVICE_NAME).current_state != 'stopped'
      puts "One moment... #{Service.status(SERVICE_NAME).current_state}"
      sleep 1
    end
    puts "Service `#{SERVICE_NAME}` stopped"
  else
    puts 'Already stopped'
  end
end
uninstall() click to toggle source
# File lib/envoi/mam/agent/daemon/windows_service_controller.rb, line 61
def uninstall
  if Service.status(SERVICE_NAME).current_state != 'stopped'
    Service.stop(SERVICE_NAME)
  end
  while Service.status(SERVICE_NAME).current_state != 'stopped'
    puts "One moment... #{Service.status(SERVICE_NAME).current_state}"
    sleep 1
  end
  Service.delete(SERVICE_NAME)
  puts "Service #{SERVICE_NAME} deleted"
end