Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

DRb support

  • Loading branch information...
commit 9a97aea7f938dec9f71a031914e6afbb189202c7 1 parent 8199394
@svenfuchs authored
View
1  lib/steam.rb
@@ -4,6 +4,7 @@ module Steam
autoload :Browser, 'steam/browser'
autoload :Connection, 'steam/connection'
autoload :Java, 'steam/java'
+ autoload :Process, 'steam/process'
autoload :Request, 'steam/request'
autoload :Response, 'steam/response'
autoload :Session, 'steam/session'
View
34 lib/steam/browser/html_unit/drb.rb
@@ -4,30 +4,40 @@ module Steam
module Browser
class HtmlUnit
class Drb
- autoload :Client, 'steam/browser/html_unit/drb/client'
- autoload :Service, 'steam/browser/html_unit/drb/service'
+ attr_reader :process, :connection, :options
def initialize(connection, options = {})
- DRb.start_service
- @drb = DRbObject.new(nil, Steam.config[:drb_uri])
- @connection = connection
- @options = options
+ @connection, @options = connection, options
+ @process = Steam::Process.new
+
+ process.kill if options[:restart] && process.running?
+ options[:daemon] ? daemonize : start unless process.running?
+ end
+
+ def object
+ @object ||= DRbObject.new(nil, Steam.config[:drb_uri])
end
-
+
def daemonize
- Forker.new(@options) { start }
+ options[:keep_alive] = true unless options.key?(:keep_alive)
+ process.fork(options) { start }
sleep(1) # FIXME
end
-
+
def start
uri = Steam.config[:drb_uri]
- DRb.start_service(uri, HtmlUnit.new(@connection, @options)) rescue Errno::EADDRINUSE
- puts "HtmlUnit ready and listening at #{uri}"
+ DRb.start_service(uri, HtmlUnit.new(connection, options))
+ puts "HtmlUnit ready and listening at #{uri} [#{::Process.pid}]"
DRb.thread.join
end
+ def restart
+ process.kill if process.running?
+ daemonize
+ end
+
def method_missing(method, *args, &block)
- @drb.send(method, *args, &block)
+ object.send(method, *args, &block)
end
end
end
View
58 lib/steam/browser/html_unit/forker.rb
@@ -1,58 +0,0 @@
-module Steam
- class Forker
- attr_reader :options
-
- def initialize(options = {}, &block)
- options = {
- :pid => '/tmp/steam.pid',
- :stdin => $stdin,
- :stdout => $stdout,
- :stderr => $stderr
- }.merge(options)
-
- recall_pid && kill if options[:restart]
-
- @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')
- puts "killing #{@pid}"
- 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 recall_pid
- @pid = ::File.read('/tmp/steam.pid').to_i rescue nil
- end
-
- def delete_pid
- ::File.delete('/tmp/steam.pid') rescue Errno::ENOENT
- end
- end
-end
View
53 lib/steam/process.rb
@@ -0,0 +1,53 @@
+module Steam
+ class Process
+ attr_reader :pid, :path
+
+ def initialize(path = '/tmp')
+ @path = path
+ recall_pid
+ end
+
+ def pid?
+ !!pid
+ end
+
+ def running?
+ pid? && !!::Process.getpgid(pid)
+ rescue Errno::ESRCH
+ false
+ end
+
+ def fork(options)
+ @pid = Kernel.fork { yield }
+ write_pid
+ # Process.detach(pid) ?
+ at_exit { kill } unless options[:keep_alive]
+ end
+
+ def kill(signal = 'TERM')
+ ::Process.kill(Signal.list[signal], pid)
+ delete_pid
+ true
+ end
+
+ protected
+
+ def recall_pid
+ @pid = ::File.read(filename).to_i rescue nil
+ delete_pid if pid? && !running?
+ end
+
+ def write_pid
+ File.open(filename, 'w') { |f| f.write(pid) }
+ end
+
+ def delete_pid
+ File.delete(filename) rescue Errno::ENOENT
+ @pid = nil
+ end
+
+ def filename
+ "#{path}/steam.pid"
+ end
+ end
+end
View
7 test/playground/drb.rb
@@ -9,11 +9,8 @@
static = Connection::Static.new(:root => root, :urls => %w(/ /javascripts /stylesheets))
connection = Rack::Cascade.new([static, @app])
-browser = Browser::HtmlUnit::Drb.new(connection)
-
-browser.daemonize
-
+browser = Browser::HtmlUnit::Drb.new(connection, :daemon => true)
browser.request('/index.html')
-p browser.response.status
+puts browser.response.status
# @browser = Browser::HtmlUnit.new(connection)
View
55 test/process_test.rb
@@ -0,0 +1,55 @@
+require File.expand_path('../test_helper', __FILE__)
+
+class ProcessTest < Test::Unit::TestCase
+ include Steam
+
+ def setup
+ delete_pid
+ end
+
+ def teardown
+ delete_pid
+ end
+
+ def write_pid(pid)
+ File.open('/tmp/steam.pid', 'w') { |f| f.write(pid) }
+ end
+
+ def delete_pid
+ File.delete('/tmp/steam.pid') rescue Errno::ENOENT
+ end
+
+ test "new instance recalls an existing pid" do
+ write_pid(1234)
+ assert_equal 1234, Process.new.pid
+ end
+
+ test "pid? returns true if pid was recalled" do
+ write_pid(1234)
+ assert_equal true, Process.new.pid?
+ end
+
+ test "pid? returns false if pid could not be recalled" do
+ assert_equal false, Process.new.pid?
+ end
+
+ test "running? returns true if pid refers to an existing process" do
+ write_pid(::Process.pid)
+ assert_equal true, Process.new.running?
+ end
+
+ test "running? returns false if pid does not refer to an existing process" do
+ write_pid(1073741823)
+ assert_equal false, Process.new.running?
+ end
+
+ # # WTF the child process actually writes to /tmp/out.txt but File.read('/tmp/out.txt')
+ # # returns an empty string
+ # test "fork forks the process, reopens given streams and yields the given block" do
+ # File.delete('/tmp/out.txt') rescue Errno::ENOENT
+ # io = File.open('/tmp/out.txt', 'w+')
+ # io.sync = true
+ # Process.new.fork(:out => io) { puts 'foo' }
+ # assert_equal 'foo', File.open('/tmp/out.txt', 'r') { |f| f.read }
+ # end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.