Class: TokyoCacheCow::Runner

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Runner

Returns a new instance of Runner.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/tokyo_cache_cow/runner.rb', line 9

def initialize(argv)
  @argv = argv
  # Default options values
  @options = {
    :chdir                 => Dir.pwd,
    :address               => '0.0.0.0',
    :port                  => Server::DefaultPort,
    :class                 => 'TokyoCacheCow::Cache::TokyoCabinetMemcache',
    :require               => [],
    :file                  => '/tmp/tcc-cache',
    :pid                   => '/tmp/tcc.pid',
    :special_delete_prefix => nil,
    :daemonize             => false,
    :marshalling           => true
  }
  
  parse!
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/tokyo_cache_cow/runner.rb', line 7

def options
  @options
end

Instance Method Details

#parse!Object



76
77
78
# File 'lib/tokyo_cache_cow/runner.rb', line 76

def parse!
  parser.parse!(@argv)
end

#parserObject



28
29
30
31
32
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/tokyo_cache_cow/runner.rb', line 28

def parser
  OptionParser.new do |opts|
    opts.banner = "Usage: tokyo_cache_cow [options]"

    opts.separator ""
    opts.separator "Options:"

    opts.on("-p[OPTIONAL]", "--port", "Port (default: #{options[:port]})") do |v|
      options[:port] = v
    end

    opts.on("-a[OPTIONAL]", "--address", "Address (default: #{options[:address]})") do |v|
      options[:address] = v
    end

    opts.on("-c[OPTIONAL]", "--class", "Cache provider class (default: #{options[:class]})") do |v|
      options[:class] = v
    end

    opts.on("-r[OPTIONAL]", "--require", "require") do |v|
      options[:require] << v
    end

    opts.on("-f[OPTIONAL]", "--file", "File (default: #{options[:file]})") do |v|
      options[:file] = v
    end

    opts.on("-d[OPTIONAL]", "--daemonize", "Daemonize (default: #{options[:daemonize]})") do |v|
      options[:daemonize] = true
    end

    opts.on("-P[OPTIONAL]", "--pid", "Pid file (default: #{options[:pid]})") do |v|
      options[:pid] = v
    end

    opts.on("-m[OPTIONAL]", "--matcher", "Special flag for doing matched deletes and gets (not enabled by default)") do |v|
      options[:special_match_char] = v
    end

    opts.on("-M[=OPTIONAL]", "--marshalling", "Disable marshalling of values") do |v|
      options[:marshalling] = false
    end

    opts.on_tail("-h", "--help", "Show this help message.") { puts opts; exit }

  end
end

#start!Object



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
# File 'lib/tokyo_cache_cow/runner.rb', line 80

def start!
  @options[:require].each {|r| require r}
  
  clazz = @options[:class].to_s.split('::').inject(Kernel) do |parent, mod|
    parent.const_get(mod)
  end
  
  address = @options[:address]
  port = @options[:port]
  special_match_char = @options[:special_match_char]
  puts "Starting the tokyo cache cow #{address} #{port}"
  pid = EM.fork_reactor do
    cache = clazz.new(:file => @options[:file])
    cache.marshalling_enabled = options[:marshalling]
    trap("INT") { EM.stop; puts "\nmoooooooo ya later"; exit(0)}
    EM.run do
      EM.start_server(address, port, TokyoCacheCow::Server) do |c|
        c.cache = cache
        c.special_match_char = special_match_char if special_match_char
      end
    end
  end
  
  if @options[:daemonize]
    File.open(options[:pid], 'w') {|f| f << pid}
    Process.detach(pid)
    Process.exit!(0)
  else
    trap("INT") { }
    Process.wait(pid)
  end
  
  pid
end