Skip to content

Instantly share code, notes, and snippets.

@edwardsharp
Last active March 8, 2017 06:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edwardsharp/7ca042e43bdf15cead95b4d8058ad7c6 to your computer and use it in GitHub Desktop.
Save edwardsharp/7ca042e43bdf15cead95b4d8058ad7c6 to your computer and use it in GitHub Desktop.
rails drb factory
require 'config/environment'
require 'drb'
DRBURI = "druby://0.0.0.0:6666" #"drbunix://#{File.join(Dir.pwd,'socket')}"
# NOTE: A dRuby reference to an object is not sufficient
# prevention from garbage collection!
class RailsFactory
def initialize
User.extend DRbUndumped
end
def model(model_name)
# Object.const_get(model_name)
model_name.constantize.extend DRbUndumped
end
def user
User
end
def all_user_count
User.all.count
end
def log m
p m
end
end
$SAFE = 1 # disable eval() and friends
p "DRBURI: #{DRBURI}\n"
DRb.start_service(DRBURI, RailsFactory.new)
DRb.thread.join
# # # # # # # # # # # # #
# client something like #
# # # # # # # # # # # # #
# require 'drb'
# SERVER_URI="druby://127.0.0.1:6666"
# factory = DRbObject.new_with_uri(SERVER_URI)
# factory.log "w00t!"
# u = factory.user
# u.all.count
# u.all.count #note: first works, second does:
# #NoMethodError: undefined method `count' for #<DRb::DRbUnknown:0x007fb4ec00fe58>
# u.count #note: seems to always work.
# factory.all_user_count
# r = factory.model 'Role'
# r.count
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment