Class: Jobby::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/runner.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{}

Instance Method Summary collapse

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 = {})
  @options = DEFAULT_OPTIONS.merge options
  if @options[:input].nil?
    message "--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_procObject

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.expand_path(@options[:prerun]) }
  end
end

#runObject

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

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.message)
  rescue Errno::ENOENT, Errno::ECONNREFUSED
    # Connect failed, fork and start the server process
    message "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(&get_proc_from_options)
      rescue Exception => exception
        return error(exception.message)
      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