Class: Gearman::Server
- Inherits:
-
Object
- Object
- Gearman::Server
- Defined in:
- lib/gearman/server.rb
Overview
Server
Description
A client for managing Gearman job servers.
Instance Attribute Summary collapse
-
#hostport ⇒ Object
readonly
Returns the value of attribute hostport.
Instance Method Summary collapse
-
#initialize(hostport) ⇒ Server
constructor
Create a new client.
-
#send_command(name) ⇒ Object
Sends a command to the server.
-
#socket(num_retries = 3) ⇒ Object
Get a socket for a job server.
-
#status ⇒ Object
Returns results of a ‘status’ command.
-
#workers ⇒ Object
Returns results of a ‘workers’ command.
Constructor Details
#initialize(hostport) ⇒ Server
Create a new client.
18 19 20 |
# File 'lib/gearman/server.rb', line 18 def initialize(hostport) @hostport = hostport # "host:port" end |
Instance Attribute Details
#hostport ⇒ Object (readonly)
Returns the value of attribute hostport.
21 22 23 |
# File 'lib/gearman/server.rb', line 21 def hostport @hostport end |
Instance Method Details
#send_command(name) ⇒ Object
Sends a command to the server.
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/gearman/server.rb', line 45 def send_command(name) response = '' socket.puts(name) while true do if buf = socket.recv_nonblock(65536) rescue nil response << buf return response if response =~ /\n.\n$/ end end end |
#socket(num_retries = 3) ⇒ Object
Get a socket for a job server.
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/gearman/server.rb', line 28 def socket(num_retries=3) return @socket if @socket num_retries.times do begin sock = TCPSocket.new(*hostport.split(':')) rescue Exception else return @socket = sock end end raise RuntimeError, "Unable to connect to job server #{hostport}" end |
#status ⇒ Object
Returns results of a ‘status’ command.
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/gearman/server.rb', line 60 def status status = {} if response = send_command('status') response.split("\n").each do |line| if line.match /^([A-Za-z_]+)\t([A-Za-z_]+)\t(\d+)\t(\d+)\t(\d+)$/ (status[$1] ||= {})[$2] = { :queue => $3, :active => $4, :workers => $5 } end end end status end |
#workers ⇒ Object
Returns results of a ‘workers’ command.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/gearman/server.rb', line 76 def workers workers = [] if response = send_command('workers') response.split("\n").each do |line| if line.match /^(\d+)\s([a-z0-9\:\.]+)\s([^\s]*)\s:\s([a-z_\s\t]+)$/ func_parts = $4.split(' ') functions = [] while !func_parts.empty? functions << func_parts.shift << '.' << func_parts.shift end workers << { :host => $2, :status => $3, :functions => functions } end end end workers end |