Class: Writefully::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/writefully/cli.rb

Instance Method Summary collapse

Instance Method Details

#start(file) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/writefully/cli.rb', line 10

def start(file)
  config = Writefully.config_from(file)
  
  if options.daemonize?
    ::Process.daemon(true, true)
    setup_logger(config[:logfile])
    write(::Process.pid, config[:pidfile])
    spawn(listen(config))
  else
    ::Signal.trap("INT") { $stdout.puts "Writefully exiting..."; exit }
    listen(config)
  end
end

#stop(file) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/writefully/cli.rb', line 25

def stop(file)
  config = Writefully.config_from(file)

  pid = open(config[:pidfile]).read.strip.to_i
  ::Process.kill("HUP", pid)
  true
rescue Errno::ENOENT
  $stdout.puts "#{pidfile} does not exist: Errno::ENOENT"
  true
rescue Errno::ESRCH
  $stdout.puts "The process #{pid} did not exist: Errno::ESRCH"
  true
rescue Errno::EPERM
  $stderr.puts "Lack of privileges to manage the process #{pid}: Errno::EPERM"
  false
rescue ::Exception => e
  $stderr.puts "While signaling the PID, unexpected #{e.class}: #{e}"
  false
end