On Jan 25, 2006, at 12:17 AM, Dustin wrote:
$global = []
Thanks in advance for the help.
-Dustin
Dustin-
This looks like a job for distributed ruby or DRb. You can use a drb
server that runs on a high port number and published a hash. Then in
your rails app you can access the hash just like a normal object and
store and lookup whatever you want in it. This way you can have one
process that handles your state data that is outside the rails app
and is persistent between requests.
Here is an example:
drb_server.rb:
require ‘drb/drb’
DRb.start_service(‘druby://127.0.0.1:13500’, Hash.new)
puts DRb.uri
DRb.thread.join
Now to use this lets just look at an irb example for the client. You
can put this code somewhere in your rails app to initialize the Drb
objects and get the hash. Here’s a simple example:
ezra:~ ez$ irb
irb(main):001:0> require ‘drb/drb’
=> true
irb(main):002:0> DRb.start_service
=> #<DRb::DRbServer:0x206e64 @front=nil, @config=
{:tcp_acl=>nil, :idconv=>#<DRb::DRbIdConv:
0x20e344>, :argc_limit=>256, :verbose=>false, :load_limit=>26214400},
@grp=#ThreadGroup:0x206d24, @thread=#<Thread:0x206978 sleep>,
@protocol=#<DRb::DRbTCPSocket:0xd17a8 @acl=nil, @config=
{:tcp_acl=>nil, :idconv=>#<DRb::DRbIdConv:
0x20e344>, :argc_limit=>256, :verbose=>false, :load_limit=>26214400},
@msg=#<DRb::DRbMessage:0xd0e84 @argc_limit=256,
@load_limit=26214400>, @socket=#TCPServer:0xd4570, @uri=“druby://
ezra.local:61721”>, @idconv=#DRb::DRbIdConv:0x20e344, @uri=“druby://
ezra.local:61721”>
irb(main):003:0> test = DRbObject.new(nil, ‘druby://127.0.0.1:3500’)
=> #<DRb::DRbObject:0x509f64 @ref=nil, @uri=“druby://127.0.0.1:3500”>
irb(main):004:0> test
=> #<DRb::DRbObject:0x509f64 @ref=nil, @uri=“druby://127.0.0.1:3500”>
irb(main):005:0> test = {:foo =>‘bart bart’, :bar => ‘niknik’}
=> {:foo=>“bart bart”, :bar=>“niknik”}
irb(main):006:0> test[:foo]
=> “bart bart”
irb(main):009:0> test.merge!({:mydata => {:title => ‘testing
drb’, :description => ‘a distributed hash object to use in my rails
apps!’}})
=> {:foo=>“bart bart”, :bar=>“niknik”, :mydata=>{:title=>“testing
drb”, :description=>“a distributed hash object to use in my rails
apps!”}}
irb(main):010:0> test
=> {:foo=>“bart bart”, :bar=>“niknik”, :mydata=>{:title=>“testing
drb”, :description=>“a distributed hash object to use in my rails
apps!”}}
irb(main):011:0> test[:mydata][:title]
=> “testing drb”
Hope that helps you. Stay tuned as I will soon be releasing a plugin
the publishes remote stand alone ActiveRecord models. These can be
used for long running processes that take too long to be in the
request response cycle.
Cheers-
-Ezra Z.
Yakima Herald-Republic
WebMaster
509-577-7732
[email protected]