Class: Nitro::DrbServer

Inherits:
Object
  • Object
show all
Defined in:
lib/nitro/server/drb.rb

Overview

A Drb server, typically used to host the application caches (sessions, application scoped variables, og cache, fragment cache, etc).

Example

require ‘nitro/server/drb

class MyDrbServer < Nitro::DrbServer

def setup_drb_objects
  ...
end

end

MyDrbServer.start – FIXME: should probably move to another directory (scripts?) TODO: Add some debuging support (overload synchash). TODO: Implement MemcacheServer. TODO: Better start/stop functionality. ++

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.startObject

A helper method to start the DRb server.



111
112
113
# File 'lib/nitro/server/drb.rb', line 111

def self.start
  self.new.start
end

Instance Method Details

#parse_optionsObject

Parse the command line arguments.



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
# File 'lib/nitro/server/drb.rb', line 40

def parse_options
  parser = OptionParser.new do |opts|
    opts.banner = "Usage: #$0 [options]"
    opts.separator ''
    opts.separator 'Specific options:'

    opts.on('-D', '--debug', 'Run in debug mode.') do |p|
      @debug = true
    end

    opts.on('-d', '--daemon', 'Run as daemon.') do |p|
      @daemon = true
    end

    opts.on_tail('-h', '--help', 'Show this message.') do
      puts opts
      exit
    end
  end

  begin
    parser.parse!(ARGV)
  rescue OptionParser::InvalidOption
    puts 'Invalid option, pass the --help parameter to get help!' 
    exit
  end
end

#setup_drbObject



78
79
80
81
# File 'lib/nitro/server/drb.rb', line 78

def setup_drb
  setup_drb_objects()
  DRb.thread.join    
end

#setup_drb_objectsObject

Override in your application to setup your custom objects. The default implementation only creates a session store.



71
72
73
74
75
76
# File 'lib/nitro/server/drb.rb', line 71

def setup_drb_objects
  require 'nitro/session/drb'
  @session_cache = SyncHash.new 
  DRb.start_service("druby://#{Session.cache_address}:#{Session.cache_port}", @session_cache)    
  puts "Drb session cache at druby://#{Session.cache_address}:#{Session.cache_port}."
end

#startObject

Start the DRb server. – TODO: refactor with runner code. ++



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/nitro/server/drb.rb', line 88

def start
  parse_options

  if @daemon
    require 'daemons/daemonize'
    pwd = Dir.pwd

    Daemonize.daemonize()

    # Restore the original pwd (daemonize sets the 
    # pwd to '/').

    Dir.chdir(pwd)

    # Save a process sentinel file.    
    FileUtils.touch ".d#{Process.pid}.pid"
  end
  setup_drb()
end

#start!Object

Start the DRb server. – TODO: refactor with runner code. ++



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/nitro/server/drb.rb', line 107

def start
  parse_options

  if @daemon
    require 'daemons/daemonize'
    pwd = Dir.pwd

    Daemonize.daemonize()

    # Restore the original pwd (daemonize sets the 
    # pwd to '/').

    Dir.chdir(pwd)

    # Save a process sentinel file.    
    FileUtils.touch ".d#{Process.pid}.pid"
  end
  setup_drb()
end