Class: Jobby::Runner
- Inherits:
-
Object
- Object
- Jobby::Runner
- Defined in:
- lib/runner.rb
Constant Summary collapse
- DEFAULT_OPTIONS =
{}
Instance Method Summary collapse
-
#create_prerun_proc ⇒ Object
Makes a Proc that loads the given filepath, passing in the logger object.
-
#initialize(options = {}) ⇒ Runner
constructor
A new instance of Runner.
-
#run ⇒ Object
Tries to connect a client to the server.
Constructor Details
#initialize(options = {}) ⇒ Runner
Returns a new instance of Runner.
25 26 27 28 29 30 31 32 |
# File 'lib/runner.rb', line 25 def initialize( = {}) @options = DEFAULT_OPTIONS.merge if @options[:input].nil? "--input not supplied, reading from STDIN (use ctrl-d to end input)" @options[:input] = $stdin.read end create_prerun_proc end |
Instance Method Details
#create_prerun_proc ⇒ Object
Makes a Proc that loads the given filepath, passing in the logger object.
35 36 37 38 39 |
# File 'lib/runner.rb', line 35 def create_prerun_proc if @options[:prerun] @options[:prerun_proc] = Proc.new { |logger| load File.(@options[:prerun]) } end end |
#run ⇒ Object
Tries to connect a client to the server. If there isn’t a server detected on the socket this process is forked and a server is started. Then, another client tries to connect to the server.
We may consider using fork and exec instead of just fork since COW [1] semantics are broken using Ruby 1.8.x. There is a patch, for those who can use it [2].
- 1
- 2
-
izumi.plan99.net/blog/index.php/2008/01/14/making-ruby’s-garbage-collector-copy-on-write-friendly-part-7/
-
TODO: this code is pretty ugly.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/runner.rb', line 54 def run change_process_ownership begin run_client rescue Errno::EACCES => exception return error(exception.) rescue Errno::ENOENT, Errno::ECONNREFUSED # Connect failed, fork and start the server process "There doesn't seem to be a server listening on #{@options[:socket]} - starting one..." if @options[:verbose] fork do begin Jobby::Server.new(@options[:socket], @options[:max_child_processes], @options[:log], @options[:prerun_proc]).run(&) rescue Exception => exception return error(exception.) end end # give the server 30 seconds to start @connected = false 60.times do begin run_client @connected = true break rescue Errno::ECONNREFUSED, Errno::EACCES, Errno::ENOENT sleep 0.5 end end unless @connected error "Couldn't connect to the server process after 60 tries" end end end |