Class: Gearman::Client
- Inherits:
-
Object
- Object
- Gearman::Client
- Includes:
- Logging
- Defined in:
- lib/gearman/client.rb
Instance Attribute Summary collapse
-
#task_create_timeout_sec ⇒ Object
Returns the value of attribute task_create_timeout_sec.
Instance Method Summary collapse
-
#do_task(task) ⇒ Object
Perform a single task.
- #handle_response(task, type, data) ⇒ Object
-
#initialize(job_servers = nil) ⇒ Client
constructor
Create a new client.
-
#set_options(opts) ⇒ Object
Set the options.
- #submit_job(task, reset_state = false, timeout = nil) ⇒ Object
Methods included from Logging
Constructor Details
#initialize(job_servers = nil) ⇒ Client
Create a new client.
13 14 15 16 17 18 |
# File 'lib/gearman/client.rb', line 13 def initialize(job_servers=nil) @coalesce_connections = {} # Unique ID => Connection @connection_pool = ConnectionPool.new(job_servers) @current_task = nil @task_create_timeout_sec = 10 end |
Instance Attribute Details
#task_create_timeout_sec ⇒ Object
Returns the value of attribute task_create_timeout_sec.
7 8 9 |
# File 'lib/gearman/client.rb', line 7 def task_create_timeout_sec @task_create_timeout_sec end |
Instance Method Details
#do_task(task) ⇒ Object
Perform a single task.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/gearman/client.rb', line 38 def do_task(task) result = nil failed = false task.on_complete {|v| result = v } task.on_fail { failed = true } task_set = TaskSet.new(self) if task_set.add_task(task) task_set.wait_forever else raise JobQueueError, "Unable to enqueue job." end failed ? nil : result end |
#handle_response(task, type, data) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/gearman/client.rb', line 112 def handle_response(task, type, data) case type when :work_complete handle, = data.split("\0", 2) logger.debug("Received WORK_COMPLETE for #{handle}") task.handle_completion() when :work_exception handle, exception = data.split("\0", 2) logger.debug("Received WORK_EXCEPTION for #{handle}") task.handle_exception(exception) when :work_fail logger.debug("Received WORK_FAIL for #{handle}") requeue = task.handle_failure add_task(task) if requeue when :work_status handle, numerator, denominator = data.split("\0", 3) logger.debug("Received WORK_STATUS for #{handle}: #{numerator} / #{denominator}") task.handle_status(numerator, denominator) when :work_warning handle, = data.split("\0", 2) logger.warn "Got WORK_WARNING for #{handle}: '#{}'" task.handle_warning() when :work_data handle, work_data = data.split("\0", 2) logger.debug "Got WORK_DATA for #{handle} with #{work_data ? work_data.size : '0'} byte(s) of data" task.handle_data(work_data) else # Not good. = "Got #{type.to_s} from #{connection} but it was not an expected type." logger.error raise ProtocolError, end end |
#set_options(opts) ⇒ Object
Set the options
24 25 26 27 28 29 30 31 |
# File 'lib/gearman/client.rb', line 24 def (opts) @connection_pool.with_all_connections do |conn| logger.debug "Send options request with #{opts}" request = Packet.pack_request("option_req", opts) response = conn.send_request(request) raise ProtocolError, response[1] if response[0]==:error end end |
#submit_job(task, reset_state = false, timeout = nil) ⇒ Object
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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/gearman/client.rb', line 56 def submit_job(task, reset_state = false, timeout = nil) task.reset_state if reset_state req = task.get_submit_packet() req_timeout = timeout || task_create_timeout_sec # Target the same job manager when submitting jobs # with the same unique id so that we can coalesce coalesce_key = task.get_uniq_hash end_time = if timeout Time.now.to_f + timeout else nil end begin connection = @connection_pool.get_connection(coalesce_key) logger.debug "Using #{connection} to submit job" type, data = connection.send_request(req, timeout) logger.debug "Got #{type.to_s} from #{connection}" if type == :job_created task.handle_created(data) if(!task.background) begin remaining = if end_time (t = end_time - Time.now.to_f) > 0 ? t : 0 else nil end type, data = connection.read_response(remaining) handle_response(task, type, data) end while [:work_status, :work_data, :work_warning].include? type end else # This shouldn't happen = "Received #{type.to_s} when we were expecting JOB_CREATED" logger.error raise ProtocolError, end rescue NetworkError = "Network error on read from #{connection.hostport} while adding job, marking server bad" logger.error raise NetworkError, rescue NoJobServersError logger.error "No servers available." raise NoJobServersError end true end |