xing-gearman-ruby 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -11,7 +11,7 @@ begin
11
11
  s.email = "ladislav.martincik@xing.com"
12
12
  s.homepage = "http://github.com/xing/gearman-ruby"
13
13
  s.description = "Library for the Gearman distributed job system"
14
- s.authors = ["Daniel Erat", "Ladislav Martincik"]
14
+ s.authors = ["Daniel Erat", "Ladislav Martincik", "Pablo Delgado", "Mauro Pompilio", "Antonio Garrote", "Kim Altintop"]
15
15
  end
16
16
  rescue LoadError
17
17
  puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :patch: 0
3
3
  :major: 1
4
- :minor: 1
4
+ :minor: 2
@@ -5,14 +5,15 @@ Gearman::Util.debug = true
5
5
  servers = ['localhost:4730']
6
6
 
7
7
  client = Gearman::Client.new(servers)
8
- #try this out
9
- client.option_request("exceptions")
10
-
11
8
  taskset = Gearman::TaskSet.new(client)
12
9
 
13
- task = Gearman::Task.new('fail_with_exception', 20)
10
+ task = Gearman::Task.new('fail_with_exception', "void")
11
+ task.retry_count = 2
14
12
  task.on_complete {|d| puts d }
15
- task.on_exception {|message| puts message; false}
13
+ task.on_exception {|ex| puts "This should never be called" }
14
+ task.on_warning {|warning| puts "WARNING: #{warning}" }
15
+ task.on_retry { puts "PRE-RETRY HOOK: retry no. #{task.retries_done}" }
16
+ task.on_fail { puts "TASK FAILED, GIVING UP" }
16
17
 
17
18
  taskset.add_task(task)
18
19
  taskset.wait(100)
@@ -9,6 +9,6 @@ w = Gearman::Worker.new(servers)
9
9
  # Add a handler for a "sleep" function that takes a single argument, the
10
10
  # number of seconds to sleep before reporting success.
11
11
  w.add_ability('fail_with_exception') do |data,job|
12
- raise Exception.new("fooexception")
12
+ raise Exception.new("Exception in worker (args: #{data.inspect})")
13
13
  end
14
14
  loop { w.work }
data/gearman-ruby.gemspec CHANGED
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{gearman-ruby}
5
- s.version = "1.1.0"
5
+ s.version = "1.2.0"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
- s.authors = ["Daniel Erat", "Ladislav Martincik"]
9
- s.date = %q{2009-07-16}
8
+ s.authors = ["Daniel Erat", "Ladislav Martincik", "Pablo Delgado", "Mauro Pompilio", "Antonio Garrote", "Kim Altintop"]
9
+ s.date = %q{2009-07-30}
10
10
  s.description = %q{Library for the Gearman distributed job system}
11
11
  s.email = %q{ladislav.martincik@xing.com}
12
12
  s.extra_rdoc_files = [
data/lib/gearman/task.rb CHANGED
@@ -17,8 +17,8 @@ class Task
17
17
  def initialize(func, arg='', opts={})
18
18
  @func = func.to_s
19
19
  @arg = arg or '' # TODO: use something more ref-like?
20
- %w{uniq on_complete on_fail on_retry on_exception on_status retry_count
21
- priority background}.map {|s| s.to_sym }.each do |k|
20
+ %w{on_complete on_fail on_retry on_exception on_status on_warning
21
+ uniq retry_count priority background}.map {|s| s.to_sym }.each do |k|
22
22
  instance_variable_set "@#{k}", opts[k]
23
23
  opts.delete k
24
24
  end
@@ -31,7 +31,7 @@ class Task
31
31
  @hash = nil
32
32
  end
33
33
  attr_accessor :uniq, :retry_count, :priority, :background
34
- attr_reader :successful, :func, :arg
34
+ attr_reader :successful, :func, :arg, :retries_done
35
35
 
36
36
  ##
37
37
  # Internal method to reset this task's state so it can be run again.
@@ -67,6 +67,9 @@ class Task
67
67
  # Set a block of code to be executed when a remote exception is sent by a worker.
68
68
  # The block will receive the message of the exception passed from the worker.
69
69
  # The user can return true for retrying or false to mark it as finished
70
+ #
71
+ # NOTE: this is actually deprecated, cf. https://bugs.launchpad.net/gearmand/+bug/405732
72
+ #
70
73
  def on_exception(&f)
71
74
  @on_exception = f
72
75
  end
@@ -79,6 +82,15 @@ class Task
79
82
  @on_status = f
80
83
  end
81
84
 
85
+ ##
86
+ # Set a block of code to be executed when we receive a warning from a worker.
87
+ # It is recommended for workers to send work_warning, followed by work_fail if
88
+ # an exception occurs on their side. Don't expect this behavior from workers NOT
89
+ # using this very library ATM, though. (cf. https://bugs.launchpad.net/gearmand/+bug/405732)
90
+ def on_warning(&f)
91
+ @on_warning = f
92
+ end
93
+
82
94
  ##
83
95
  # Handle completion of the task.
84
96
  #
@@ -104,17 +116,11 @@ class Task
104
116
  end
105
117
 
106
118
  ##
107
- # Record an exception and check whether we should be retried.
119
+ # Record an exception.
108
120
  #
109
- # @return true if we should be resubmitted; false otherwise
110
121
  def handle_exception(exception)
111
- if @on_exception
112
- should_retry = @on_exception.call(exception)
113
- @retries_done += 1 if should_retry
114
- should_retry
115
- else
116
- false
117
- end
122
+ @on_exception.call(exception) if @on_exception
123
+ self
118
124
  end
119
125
 
120
126
  ##
@@ -123,6 +129,14 @@ class Task
123
129
  @on_status.call(numerator, denominator) if @on_status
124
130
  self
125
131
  end
132
+
133
+ ##
134
+ # Handle a warning.
135
+ #
136
+ def handle_warning(message)
137
+ @on_warning.call(message) if @on_warning
138
+ self
139
+ end
126
140
 
127
141
  ##
128
142
  # Return a hash that we can use to execute identical tasks on the same
@@ -135,21 +135,15 @@ class TaskSet
135
135
  # @param hostport "host:port" of job server
136
136
  # @param data data returned in packet from server
137
137
  def handle_work_exception(hostport, data)
138
- handle, exception = data.split("\0", 3)
138
+ handle, exception = data.split("\0", 2)
139
139
  Util.log "Got work_exception with handle #{handle} from #{hostport}: '#{exception}'"
140
140
  js_handle = Util.handle_to_str(hostport, handle)
141
- tasks = @tasks_in_progress.delete(js_handle)
141
+ tasks = @tasks_in_progress[js_handle]
142
142
  if not tasks
143
- raise ProtocolError, "Got unexpected work_status with handle " +
143
+ raise ProtocolError, "Got unexpected work_exception with handle " +
144
144
  "#{handle} from #{hostport} (no task by that name)"
145
145
  end
146
- tasks.each do |t|
147
- if t.handle_exception(exception)
148
- add_task_internal(t, false)
149
- else
150
- @finished_tasks << t
151
- end
152
- end
146
+ tasks.each {|t| t.handle_exception(exception) }
153
147
  end
154
148
  private :handle_work_exception
155
149
 
@@ -195,6 +189,24 @@ class TaskSet
195
189
  end
196
190
  private :handle_work_status
197
191
 
192
+ ##
193
+ # Handle a 'work_warning' response from a job server.
194
+ #
195
+ # @param hostport "host:port" of job server
196
+ # @param data data returned in packet from server
197
+ def handle_work_warning(hostport, data)
198
+ handle, message = data.split("\0", 2)
199
+ Util.log "Got work_warning with handle #{handle} from #{hostport}: '#{message}'"
200
+ js_handle = Util.handle_to_str(hostport, handle)
201
+ tasks = @tasks_in_progress[js_handle]
202
+ if not tasks
203
+ raise ProtocolError, "Got unexpected work_warning with handle " +
204
+ "#{handle} from #{hostport} (no task by that name)"
205
+ end
206
+ tasks.each {|t| t.handle_warning(message) }
207
+ end
208
+ private :handle_work_warning
209
+
198
210
  ##
199
211
  # Read and process a packet from a socket.
200
212
  #
@@ -217,6 +229,8 @@ class TaskSet
217
229
  handle_work_status(hostport, data)
218
230
  when :work_exception
219
231
  handle_work_exception(hostport, data)
232
+ when :work_warning
233
+ handle_work_warning(hostport, data)
220
234
  else
221
235
  Util.log "Got #{type.to_s} from #{hostport}"
222
236
  end
data/lib/gearman/util.rb CHANGED
@@ -38,6 +38,7 @@ class Util
38
38
  12 => :work_status, # W->J/C: HANDLE[0]NUMERATOR[0]DENOMINATOR
39
39
  13 => :work_complete, # W->J/C: HANDLE[0]RES
40
40
  14 => :work_fail, # W->J/C: HANDLE
41
+ 29 => :work_warning, # W->J/C: HANDLE[0]MSG
41
42
 
42
43
  25 => :work_exception, # W->J: HANDLE[0]ARG
43
44
  26 => :option_req, # C->J: TEXT
@@ -271,16 +271,17 @@ class Worker
271
271
  ret = ret.to_s
272
272
  Util.log "Sending work_complete for #{handle} with #{ret.size} byte(s) " +
273
273
  "to #{hostport}"
274
- Util.pack_request(:work_complete, "#{handle}\0#{ret}")
274
+ [ Util.pack_request(:work_complete, "#{handle}\0#{ret}") ]
275
275
  elsif exception.nil?
276
276
  Util.log "Sending work_fail for #{handle} to #{hostport}"
277
- Util.pack_request(:work_fail, handle)
277
+ [ Util.pack_request(:work_fail, handle) ]
278
278
  elsif exception
279
- Util.log "Sending work_exception for #{handle} to #{hostport}"
280
- Util.pack_request(:work_exception, "#{handle}\0#{exception.message}")
279
+ Util.log "Sending work_warning, work_fail for #{handle} to #{hostport}"
280
+ [ Util.pack_request(:work_warning, "#{handle}\0#{exception.message}"),
281
+ Util.pack_request(:work_fail, handle) ]
281
282
  end
282
283
 
283
- Util.send_request(sock, cmd)
284
+ cmd.each {|p| Util.send_request(sock, p) }
284
285
  true
285
286
  end
286
287
 
metadata CHANGED
@@ -1,16 +1,20 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xing-gearman-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Erat
8
8
  - Ladislav Martincik
9
+ - Pablo Delgado
10
+ - Mauro Pompilio
11
+ - Antonio Garrote
12
+ - Kim Altintop
9
13
  autorequire:
10
14
  bindir: bin
11
15
  cert_chain: []
12
16
 
13
- date: 2009-07-16 00:00:00 -07:00
17
+ date: 2009-07-30 00:00:00 -07:00
14
18
  default_executable:
15
19
  dependencies: []
16
20
 
@@ -73,6 +77,7 @@ files:
73
77
  - test/worker_test.rb
74
78
  has_rdoc: false
75
79
  homepage: http://github.com/xing/gearman-ruby
80
+ licenses:
76
81
  post_install_message:
77
82
  rdoc_options:
78
83
  - --charset=UTF-8
@@ -93,7 +98,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
93
98
  requirements: []
94
99
 
95
100
  rubyforge_project:
96
- rubygems_version: 1.2.0
101
+ rubygems_version: 1.3.5
97
102
  signing_key:
98
103
  specification_version: 3
99
104
  summary: Library for the Gearman distributed job system