Class: Mouth::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/mouth/runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Runner

Returns a new instance of Runner.



14
15
16
17
18
19
# File 'lib/mouth/runner.rb', line 14

def initialize(opts={})
  self.log_file = opts[:log_file]
  self.pid_file = opts[:pid_file]
  self.verbosity = opts[:verbosity]
  self.options = opts
end

Instance Attribute Details

#log_fileObject

Returns the value of attribute log_file.



8
9
10
# File 'lib/mouth/runner.rb', line 8

def log_file
  @log_file
end

#loggerObject

Returns the value of attribute logger.



10
11
12
# File 'lib/mouth/runner.rb', line 10

def logger
  @logger
end

#optionsObject

Returns the value of attribute options.



12
13
14
# File 'lib/mouth/runner.rb', line 12

def options
  @options
end

#pid_fileObject

Returns the value of attribute pid_file.



9
10
11
# File 'lib/mouth/runner.rb', line 9

def pid_file
  @pid_file
end

#verbosityObject

0: Only errors/warnings 1: informational 2: debug/all incomding UDP packets



11
12
13
# File 'lib/mouth/runner.rb', line 11

def verbosity
  @verbosity
end

Instance Method Details

#daemonize!Object



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/mouth/runner.rb', line 47

def daemonize!
  # Fork and continue in forked process
  # Also calls setsid
  # Also redirects all output to /dev/null
  Process.daemon(true)
  
  # Reset umask
  File.umask(0000)
  
  # Set the procline
  $0 = "mouth [initializing]"
end

#kill!Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/mouth/runner.rb', line 35

def kill!
  if @pid_file
    pid = File.read(@pid_file)
    #logger.warn "Sending #{kill_command} to #{pid.to_i}"
    Process.kill(:INT, pid.to_i)
  else
    #logger.warn "No pid_file specified"
  end
ensure
  exit(0)
end

#run!Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/mouth/runner.rb', line 21

def run!
  kill! if self.options[:kill]
  
  puts "Starting Mouth..."
  
  daemonize!
  save_pid!
  setup_logging!
  
  # Start the reactor!
  sucker = Mouth::Sucker.new(self.options)
  sucker.suck!
end

#save_pid!Object



60
61
62
63
64
65
66
# File 'lib/mouth/runner.rb', line 60

def save_pid!
  if @pid_file
    pid = Process.pid
    FileUtils.mkdir_p(File.dirname(@pid_file))
    File.open(@pid_file, 'w') { |f| f.write(pid) }
  end
end

#setup_logging!Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/mouth/runner.rb', line 68

def setup_logging!
  if @log_file
    STDERR.reopen(@log_file, 'a')
    
    # Open a logger
    self.logger = Logger.new(@log_file)
    self.logger.level = case self.verbosity
    when 0
      Logger::WARN
    when 1
      Logger::INFO
    else
      Logger::DEBUG
    end
    Mouth.logger = self.logger
    
    self.logger.info "Mouth Initialized..."
  end
end