Skip to content

Commit

Permalink
playing with drb
Browse files Browse the repository at this point in the history
  • Loading branch information
Sven Fuchs committed Feb 19, 2010
1 parent eba93a5 commit d291fb9
Show file tree
Hide file tree
Showing 9 changed files with 177 additions and 25 deletions.
24 changes: 0 additions & 24 deletions _backup/lib/steam/forker.rb

This file was deleted.

23 changes: 23 additions & 0 deletions lib/core_ext/ruby/process/daemon.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module Process
def self.daemon(nochdir = nil, noclose = nil)
exit if fork # Parent exits, child continues.
Process.setsid # Become session leader.
exit if fork # Zap session leader. See [1].

unless nochdir
Dir.chdir "/" # Release old working directory.
end

File.umask 0000 # Ensure sensible umask. Adjust as needed.

unless noclose
STDIN.reopen "/dev/null" # Free file descriptors and
STDOUT.reopen "/dev/null", "a" # point them somewhere sensible.
STDERR.reopen '/dev/null', 'a'
end

trap("TERM") { exit }

return 0
end unless respond_to?(:daemon)
end
2 changes: 1 addition & 1 deletion lib/steam.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ module Steam
autoload :Browser, 'steam/browser'
autoload :Connection, 'steam/connection'
autoload :Java, 'steam/java'
autoload :Reloader, 'steam/reloader'
autoload :Request, 'steam/request'
autoload :Response, 'steam/response'
autoload :Session, 'steam/session'
Expand All @@ -18,6 +17,7 @@ def config
:url_scheme => 'http',
:charset => 'utf-8',
:java_load_params => '-Xmx1024M',
:drb_uri => 'druby://127.0.0.1:9000',
:html_unit => {
:java_path => File.expand_path("../htmlunit-2.6/", __FILE__),
:browser_version => :FIREFOX_3,
Expand Down
2 changes: 2 additions & 0 deletions lib/steam/browser/html_unit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ module Browser
class HtmlUnit
autoload :Actions, 'steam/browser/html_unit/actions'
autoload :Client, 'steam/browser/html_unit/client'
autoload :Drb, 'steam/browser/html_unit/drb'
autoload :Forker, 'steam/browser/html_unit/forker'
autoload :Connection, 'steam/browser/html_unit/connection'
autoload :Matchers, 'steam/browser/html_unit/matchers'
autoload :Page, 'steam/browser/html_unit/page'
Expand Down
33 changes: 33 additions & 0 deletions lib/steam/browser/html_unit/drb.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require 'drb'

module Steam
module Browser
class HtmlUnit
class Drb
autoload :Client, 'steam/browser/html_unit/drb/client'
autoload :Service, 'steam/browser/html_unit/drb/service'

def initialize
DRb.start_service
@browser = DRbObject.new(nil, Steam.config[:drb_uri])
end

def daemonize(connection = nil, options = {})
Forker.new { start(connection, options) }
sleep(1) # FIXME
end

def start(connection = nil, options = {})
uri = Steam.config[:drb_uri]
DRb.start_service(uri, HtmlUnit.new(connection, options))
puts "Browser ready and listening at #{uri}"
DRb.thread.join
end

def method_missing(method, *args, &block)
@browser.send(method, *args, &block)
end
end
end
end
end
20 changes: 20 additions & 0 deletions lib/steam/browser/html_unit/drb/client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require 'drb'

module Steam
module Browser
class HtmlUnit
module Drb
class Client
def initialize
DRb.start_service
@browser = DRbObject.new(nil, Service.uri)
end

def method_missing(method, *args, &block)
@browser.send(method, *args, &block) # if @browser.respond_to?(method)
end
end
end
end
end
end
28 changes: 28 additions & 0 deletions lib/steam/browser/html_unit/drb/service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require 'drb'
require 'core_ext/ruby/process/daemon'

module Steam
module Browser
class HtmlUnit
module Drb
module Service
class << self
def uri
Steam.config[:drb_uri]
end

def daemonize(connection = nil, options = {})
Forker.new { start(connection, options) }
end

def start(connection = nil, options = {})
DRb.start_service(uri, HtmlUnit.new(connection, options))
puts "Browser ready and listening at #{uri}"
DRb.thread.join
end
end
end
end
end
end
end
51 changes: 51 additions & 0 deletions lib/steam/browser/html_unit/forker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
module Steam
class Forker
attr_reader :options

def initialize(options = {}, &block)
options = {
:pid => '/tmp/steam.pid',
:stdin => $stdin,
:stdout => $stdout,
:stderr => $stderr
}.merge(options)

@pid = Kernel.fork do
STDIN.reopen options[:stdin]
STDOUT.reopen options[:stdout]
STDERR.reopen options[:stderr]
block.call
end
# write_pid
at_exit { kill }
end

def interrupt
kill('INT')
end

def kill(signal = 'TERM')
if running?
Process.kill(Signal.list[signal], @pid)
delete_pid
true
end
end

def running?
@pid ? !!Process.getpgid(@pid) : false
rescue Errno::ESRCH
false
end

protected

def write_pid
::File.open('/tmp/steam.pid', 'w'){ |f| f.write("#{@pid}") }
end

def delete_pid
::File.delete('/tmp/steam.pid') rescue Errno::ENOENT
end
end
end
19 changes: 19 additions & 0 deletions test/playground/drb.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
$: << File.expand_path("../../../lib", __FILE__)

require 'steam'

include Steam

@app = Connection::Mock.new
root = File.expand_path("../../fixtures/public", __FILE__)
static = Connection::Static.new(:root => root, :urls => %w(/ /javascripts /stylesheets))
connection = Rack::Cascade.new([static, @app])

browser = Browser::HtmlUnit::Drb.new

browser.daemonize(connection)

browser.request('/index.html')
p browser.response.status

# @browser = Browser::HtmlUnit.new(connection)

0 comments on commit d291fb9

Please sign in to comment.