Class: Slacky::Service

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

Instance Method Summary collapse

Constructor Details

#initialize(config, daemon) ⇒ Service

Returns a new instance of Service.



4
5
6
7
# File 'lib/slacky/service.rb', line 4

def initialize(config, daemon)
  @config = config
  @daemon = daemon
end

Instance Method Details

#restartObject



50
51
52
53
# File 'lib/slacky/service.rb', line 50

def restart
  stop
  start
end

#runObject



9
10
11
# File 'lib/slacky/service.rb', line 9

def run
  @daemon.start false
end

#start(persist = false) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/slacky/service.rb', line 13

def start(persist = false)
  pid = get_pid
  if pid
    puts "#{@config.name} is already running with PID #{pid}"
    return
  end

  print "Starting #{@config.name}... "
  new_pid = Process.fork { @daemon.start }
  Process.detach new_pid
  puts "started"
end

#statusObject



55
56
57
58
59
60
61
62
63
64
# File 'lib/slacky/service.rb', line 55

def status
  pid = get_pid
  if pid
    puts "#{@config.name} is running with PID #{pid}"
    true
  else
    puts "#{@config.name} is not running"
    false
  end
end

#stop(persist = false) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/slacky/service.rb', line 26

def stop(persist = false)
  pid = get_pid
  unless pid
    puts "#{@config.name} is not running"
    return
  end

  print "Stopping #{@config.name}..."

  begin
    Process.kill 'HUP', pid
  rescue
    @daemon.cleanup
  end

  ticks = 0
  while pid = get_pid && ticks < 40
    sleep 0.5
    ticks += 1
    print '.' if ticks % 4 == 0
  end
  puts " #{pid.nil? ? 'stopped' : 'failed'}"
end