-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Sven Fuchs
committed
Feb 19, 2010
1 parent
eba93a5
commit d291fb9
Showing
9 changed files
with
177 additions
and
25 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |