Class: WindowsServiceController

Inherits:
Object
  • Object
show all
Includes:
RbConfig, Win32
Defined in:
lib/envoi/mam/agent/daemon/windows_service_controller.rb

Constant Summary collapse

SERVICE_NAME =
'cantemo_portal_watch_folder_agent'
SERVICE_DISPLAYNAME =
'Cantemo Portal Watch Folder Agent'

Class Method Summary collapse

Class Method Details

.install(args = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# 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

.pauseObject



104
105
106
107
108
109
110
111
112
113
114
115
# 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

.resumeObject



117
118
119
120
121
122
123
124
125
126
127
128
# 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 = { }) ⇒ Object

Raises:

  • (ArgumentError)


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 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

.startObject



73
74
75
76
77
78
79
80
81
82
83
84
85
# 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

.statusObject



87
88
89
# 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

.stopObject



91
92
93
94
95
96
97
98
99
100
101
102
# 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

.uninstallObject



61
62
63
64
65
66
67
68
69
70
71
# 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