Class: Gearman::Server

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

Overview

Server

Description

A client for managing Gearman job servers.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hostport) ⇒ Server

Create a new client.

Parameters:

  • job_servers;eitherasingleserveroranarray ("host:port")

    ob_servers “host:port”; either a single server or an array

  • prefix

    function name prefix (namespace)



18
19
20
# File 'lib/gearman/server.rb', line 18

def initialize(hostport)
  @hostport = hostport  # "host:port"
end

Instance Attribute Details

#hostportObject (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.

Returns:

  • a response string



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.

Parameters:

  • hostport

    job server “host:port”

Returns:

  • a Socket

Raises:

  • (RuntimeError)


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

#statusObject

Returns results of a ‘status’ command.

Returns:

  • a hash of abilities with queued, active and workers keys.



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

#workersObject

Returns results of a ‘workers’ command.

Returns:

  • an array of worker hashes, containing host, status and functions keys.



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