Class: Sparrow::Runner

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Miscel

#base_dir, #log_path, #logger, #options, #options=, #pid_dir

Constructor Details

#initializeRunner

Returns a new instance of Runner.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/sparrow/runner.rb', line 14

def initialize
  self.options = {
    :host => "0.0.0.0",
    :port => 11212,
    :debug => false,
    :type => 'disk',
    :start_time => Time.now
  }
  
  self.options.merge!({
    :base_dir => base_dir,
    :pid_dir  => pid_dir,
    :log_path => log_path
  })
  
  parse_options
  
  FileUtils.mkdir_p(options[:base_dir])
  FileUtils.mkdir_p(File.dirname(options[:log_path]))
  
  if options.include?(:kill)
    kill_pid(options[:port])
  end
  
  if options.include?(:kill_all)
    kill_pid('*')
  end
  
  if !options[:daemonize]
    start
  else
    daemonize
  end
end

Class Method Details

.runObject



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

def run
  self.new
end

Instance Method Details

#parse_optionsObject



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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/sparrow/runner.rb', line 74

def parse_options
  OptionParser.new do |opts|
    opts.summary_width = 25
    opts.banner = "Sparrow (#{VERSION})\n\n",
                  "Usage: sparrow [-b path] [-h host] [-p port] [-P file]\n",
                  "               [-d] [-k port] [-l file] [-e]\n",
                  "       sparrow --help\n",
                  "       sparrow --version\n"
    
    opts.separator ""
    opts.separator ""; opts.separator "Configuration:"
    
    opts.on("-b", "--base PATH", String, "Path to queue data store.", "(default: #{options[:base_dir]})") do |v|
      options[:base_dir] = File.expand_path(v)
    end
    
    opts.separator ""; opts.separator "Network:"
    
    opts.on("-h", "--host HOST", String, "Specify host", "(default: #{options[:host]})") do |v|
      options[:host] = v
    end
    
    opts.on("-p", "--port PORT", Integer, "Specify port", "(default: #{options[:port]})") do |v|
      options[:port] = v
    end
    
    opts.separator ""; opts.separator "Daemonization:"
    
    opts.on("-P", "--pid FILE", String, "save PID in DIR when using -d option.", "(default: #{options[:pid_dir]})") do |v|
      options[:pid_dir] = File.expand_path(v)
    end
    
    opts.on("-d", "--daemon", "Daemonize mode") do |v|
      options[:daemonize] = v
    end

    opts.on("-k", "--kill", "Kill daemons on port #{options[:port]}.") do |v|
      options[:kill] = v
    end
    
    opts.on("-j", "--kill-all", String, "Kill specified running daemons - leave blank to kill all.") do |v|
      options[:kill_all] = v
    end
    
    opts.separator ""; opts.separator "Logging:"
    
    opts.on("-l", "--log [FILE]", String, "Path to print debugging information.") do |v|
      options[:log_path] = File.expand_path(v)
    end
    
    opts.on("-e", "--debug", "Run in debug mode", "(default: #{options[:debug]})") do |v|
      options[:debug] = v
    end
    
    opts.separator ""; opts.separator "Miscellaneous:"
    
    opts.on_tail("-?", "--help", "Display this usage information.") do
      puts "#{opts}\n"
      exit
    end
    
    opts.on_tail("-v", "--version", "Display version") do |v|
      puts "Sparrow #{VERSION}"
      exit
    end
  end.parse!
  options
end

#startObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/sparrow/runner.rb', line 49

def start
  puts "Starting Sparrow server on port: #{options[:port]}..."
  
  trap("INT") {
    stop
    exit
  }
  trap("TERM"){
    stop
    exit
  }
  
  EventMachine::run {
    EventMachine::start_server(options[:host], options[:port].to_i, Sparrow::Server)
  }
end

#stopObject



66
67
68
69
70
71
72
# File 'lib/sparrow/runner.rb', line 66

def stop
  puts
  puts "Writing queues to disk..."
  Sparrow::Queue.shutdown!
  puts "Stopping Eventmachine Server"
  EventMachine::stop
end