Class: Blackhole::Runner

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Runner

Returns a new instance of Runner.



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

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.



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

def log_file
  @log_file
end

#loggerObject

Returns the value of attribute logger.



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

def logger
  @logger
end

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

#pid_fileObject

Returns the value of attribute pid_file.



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

def pid_file
  @pid_file
end

#verbosityObject

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



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

def verbosity
  @verbosity
end

Instance Method Details

#daemonize!Object



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

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 = "Blackhole [initializing]"
end

#kill!Object



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

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



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

def run!
  kill! if self.options[:kill]
  
  puts "Starting Blackhole..."
  
  daemonize!
  save_pid!
  setup_logging!      

  # Start the reactor!
  hole = Blackhole::Hole.new(self.options)
  hole.tug!
end

#save_pid!Object



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

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



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

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
    Blackhole.logger = self.logger
    
    self.logger.info "Blackhole Initialized..."
  end
end