Class: Redis::Bin

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

Defined Under Namespace

Classes: RubyRedisServer

Class Method Summary collapse

Class Method Details

.serverObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/redis/bin.rb', line 45

def self.server

  if ARGV==['-v'] or ARGV==['--version']
    print "Redis server version %s (Ruby)\n" % Redis::VERSION
    exit 0
  end

  if ARGV==['--help'] or ARGV.size > 1
    STDERR.print "Usage: ruby-redis [/path/to/redis.conf]\n"
    STDERR.print "       ruby-redis - (read config from stdin)\n"
    exit 1
  end

  show_no_config_warning = (ARGV.size == 0)

  config = Config.new(ARGV.empty? ? [] : ARGF)

  Dir.chdir config[:dir]
  
  if config[:logfile] == 'stdout'
    logger = Logger.new STDOUT
  else
    logger = Logger.new config[:logfile] 
  end

  if config[:loglevel] == 'debug'
    logger.level = Logger::DEBUG
  elsif config[:loglevel] == 'notice'
    logger.level = Logger::NOTICE
  elsif config[:loglevel] == 'warning'
    logger.level = Logger::WARNING
  elsif config[:loglevel] != 'verbose'
    raise "Invalid log level. Must be one of debug, notice, warning, verbose."
  else
    logger.level = Logger::INFO
  end

  if show_no_config_warning
    logger.warn "Warning: no config file specified, using the default config. In order to specify a config file use 'ruby-redis /path/to/redis.conf'"
  end
  
  if config[:daemonize]
    exit!(0) if fork
    Process::setsid
    exit!(0) if fork
    STDIN.reopen("/dev/null")
    STDOUT.reopen("/dev/null", "w")
    STDERR.reopen("/dev/null", "w")
    begin
      File.open(config[:pidfile], 'w') do |io|
        io.write "%d\n" % Process.pid
      end
    rescue Exception => e
      logger.error e.message
    end
  end
  
  EventMachine.run {

    databases = Array.new(config[:databases]) {Database.new}
    started_message = "Server started, Ruby Redis version %s" % Redis::VERSION
  
    if config[:unixsocket]
      EventMachine::start_server config[:unixsocket], RubyRedisServer, logger, databases, config
      logger.notice started_message
      logger.notice "The server is now ready to accept connections at %s" % config[:unixsocket]
    else
      EventMachine::start_server config[:bind], config[:port], RubyRedisServer, logger, databases, config
      logger.notice started_message
      logger.notice "The server is now ready to accept connections on port %d" % config[:port]
    end
    
    # The test suite blocks until it gets the pid from the log.
    logger.flush

  }
  
end