Class: Serene::Daemons::Rogger::Rogger

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

Constant Summary collapse

CONFIG_FILE =
File.dirname(__FILE__) + "/../../config/rogger.config.yml"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opt = {}) ⇒ Rogger

opt overwrites CONFIG_FILE if not empty



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/serene_daemons_rogger/rogger.rb', line 17

def initialize (opt = {})
  @opt = opt
  @config = (@opt.empty? ? YAML.load(File.open(CONFIG_FILE)) : @opt)
  
  path = get_log_path({:service => "rogger", :environment => "prod", :category => "daemons"})
  FileUtils::mkdir_p(File.dirname(path))
  @log = Logger.new(path)
  @log.level = Logger::DEBUG
  
  @buffer = {}
  @mutex = Mutex.new
  @stop = false
  @flush_thread = nil
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



14
15
16
# File 'lib/serene_daemons_rogger/rogger.rb', line 14

def config
  @config
end

Instance Method Details

#runObject

this runs the whole show



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/serene_daemons_rogger/rogger.rb', line 33

def run
  @log.info("START RUN - " + Time.now.to_s)
  trap("INT") do
    begin
      stop if (not @stop)
    rescue => details
      abort("Error on exit: " + details.message + " - " + details.backtrace.join(" ++ "))
    end
  end
  
  begin
    @flush_thread = buffer_flusher
    @server = TCPServer.new(@config['port'].to_i)
    
    # keep accepting connections and spawning threads to handle them
    loop do
      break if (@stop)
      new_session(@server.accept)
    end
    
  rescue => details
    @log.fatal("END RUN: (" + details.message + ") " + details.backtrace.join(" ++ ") + " - " + Time.now.to_s)
    raise(details)
  end
  @log.info("END RUN - " + Time.now.to_s)
end

#valid_request?(request = {}) ⇒ Boolean

return true if all the values in the hash are alphanumeric, or false otherwise.

this will hopefully prevent a request that includes characters like “/..” that would make it possible to overwrite files outside of the @config

Returns:

  • (Boolean)


64
65
66
67
68
# File 'lib/serene_daemons_rogger/rogger.rb', line 64

def valid_request? (request = {})
  all_valid = true
  request.each_value { |v| all_valid = false if (not v.match(/^[a-zA-Z0-9]*$/)) }
  return(all_valid)
end