gearman-ruby 3.0.1 → 3.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +1 -1
- data/VERSION.yml +1 -1
- data/examples/bgclient.rb +19 -0
- data/examples/client_background.rb +3 -3
- data/examples/client_reverse.rb +2 -2
- data/examples/worker_reverse_string.rb +9 -16
- data/lib/gearman/worker.rb +23 -4
- metadata +12 -5
data/Rakefile
CHANGED
@@ -9,7 +9,7 @@ begin
|
|
9
9
|
s.name = "gearman-ruby"
|
10
10
|
s.summary = "Library for the Gearman distributed job system"
|
11
11
|
s.email = "john@unixninjas.org"
|
12
|
-
s.homepage = "http://github.com/
|
12
|
+
s.homepage = "http://github.com/johnewart/gearman-ruby"
|
13
13
|
s.description = "Library for the Gearman distributed job system"
|
14
14
|
s.authors = ["John Ewart", "Colin Curtin", "Daniel Erat", "Ladislav Martincik", "Pablo Delgado", "Mauro Pompilio", "Antonio Garrote", "Kim Altintop"]
|
15
15
|
end
|
data/VERSION.yml
CHANGED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
#require 'gearman'
|
3
|
+
require '../lib/gearman'
|
4
|
+
require 'json'
|
5
|
+
|
6
|
+
servers = ['localhost:4731']
|
7
|
+
|
8
|
+
client = Gearman::Client.new(servers)
|
9
|
+
taskset = Gearman::TaskSet.new(client)
|
10
|
+
function = "reverse_string"
|
11
|
+
struct_data = {
|
12
|
+
:site_id => 45,
|
13
|
+
:domain => 'www.google.com'
|
14
|
+
}
|
15
|
+
#data = JSON.generate(struct_data)
|
16
|
+
data = "testing"
|
17
|
+
task = Gearman::BackgroundTask.new(function, data)
|
18
|
+
taskset.add_task(task)
|
19
|
+
|
@@ -2,13 +2,13 @@ require 'rubygems'
|
|
2
2
|
#require 'gearman'
|
3
3
|
require '../lib/gearman'
|
4
4
|
|
5
|
-
servers = ['localhost:
|
5
|
+
servers = ['localhost:4731']
|
6
6
|
|
7
7
|
client = Gearman::Client.new(servers)
|
8
8
|
taskset = Gearman::TaskSet.new(client)
|
9
9
|
|
10
10
|
task = Gearman::Task.new('sleep', 20, { :background => true })
|
11
|
-
task.on_complete {|d| puts d }
|
11
|
+
#task.on_complete {|d| puts d }
|
12
12
|
|
13
13
|
taskset.add_task(task)
|
14
|
-
taskset.wait(100)
|
14
|
+
#taskset.wait(100)
|
data/examples/client_reverse.rb
CHANGED
@@ -7,7 +7,7 @@ require '../lib/gearman'
|
|
7
7
|
t = nil
|
8
8
|
threadcounter = 0
|
9
9
|
|
10
|
-
client = Gearman::Client.new('localhost')
|
10
|
+
client = Gearman::Client.new('localhost:4731')
|
11
11
|
|
12
12
|
|
13
13
|
myid = threadcounter
|
@@ -22,6 +22,6 @@ taskset = Gearman::TaskSet.new(client)
|
|
22
22
|
puts "#{jid} #{data}"
|
23
23
|
|
24
24
|
time = Time.now() + rand(120) + 10
|
25
|
-
task.schedule(time)
|
25
|
+
#task.schedule(time)
|
26
26
|
taskset.add_task(task)
|
27
27
|
end
|
@@ -3,25 +3,18 @@ require '../lib/gearman'
|
|
3
3
|
|
4
4
|
# String reverse worker
|
5
5
|
|
6
|
-
servers = ['localhost:
|
6
|
+
servers = ['localhost:4731']
|
7
7
|
|
8
|
-
t = nil
|
9
8
|
jobnum = 0
|
10
9
|
|
11
|
-
(
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
jobnum += 1
|
19
|
-
result
|
20
|
-
end
|
21
|
-
loop { w.work }
|
22
|
-
}
|
10
|
+
w = Gearman::Worker.new(servers)
|
11
|
+
w.add_ability('reverse_string') do |data,job|
|
12
|
+
result = data.reverse
|
13
|
+
puts "Job: #{job.inspect} Data: #{data.inspect} Reverse: #{result} "
|
14
|
+
puts "Completed job ##{jobnum}"
|
15
|
+
jobnum += 1
|
16
|
+
result
|
23
17
|
end
|
18
|
+
loop { w.work }
|
24
19
|
|
25
|
-
puts "Waiting for threads..."
|
26
|
-
t.join
|
27
20
|
|
data/lib/gearman/worker.rb
CHANGED
@@ -54,6 +54,7 @@ class Worker
|
|
54
54
|
def run(data, job)
|
55
55
|
@block.call(data, job)
|
56
56
|
end
|
57
|
+
|
57
58
|
end
|
58
59
|
|
59
60
|
# = Job
|
@@ -107,6 +108,7 @@ class Worker
|
|
107
108
|
@client_id = Array.new(30) { chars[rand(chars.size)] }.join
|
108
109
|
@sockets = {} # "host:port" -> Socket
|
109
110
|
@abilities = {} # "funcname" -> Ability
|
111
|
+
@after_abilities = {} # "funcname" -> Ability
|
110
112
|
@bad_servers = [] # "host:port"
|
111
113
|
@servers_mutex = Mutex.new
|
112
114
|
%w{client_id reconnect_sec
|
@@ -246,6 +248,10 @@ class Worker
|
|
246
248
|
@sockets.values.each {|s| announce_ability(s, func, timeout) }
|
247
249
|
end
|
248
250
|
|
251
|
+
def after_ability(func, &f)
|
252
|
+
@after_abilities[func] = Ability.new(f)
|
253
|
+
end
|
254
|
+
|
249
255
|
##
|
250
256
|
# Let job servers know that we're no longer able to do something.
|
251
257
|
#
|
@@ -287,12 +293,10 @@ class Worker
|
|
287
293
|
exception = e
|
288
294
|
end
|
289
295
|
|
290
|
-
|
291
296
|
cmd = if ret && exception.nil?
|
292
|
-
|
293
|
-
Util.logger.debug "GearmanRuby: Sending work_complete for #{handle} with #{ret.size} byte(s) " +
|
297
|
+
Util.logger.debug "GearmanRuby: Sending work_complete for #{handle} with #{ret.to_s.size} byte(s) " +
|
294
298
|
"to #{hostport}"
|
295
|
-
[ Util.pack_request(:work_complete, "#{handle}\0#{ret}") ]
|
299
|
+
[ Util.pack_request(:work_complete, "#{handle}\0#{ret.to_s}") ]
|
296
300
|
elsif exception.nil?
|
297
301
|
Util.logger.debug "GearmanRuby: Sending work_fail for #{handle} to #{hostport}"
|
298
302
|
[ Util.pack_request(:work_fail, handle) ]
|
@@ -302,6 +306,21 @@ class Worker
|
|
302
306
|
end
|
303
307
|
|
304
308
|
cmd.each {|p| Util.send_request(sock, p) }
|
309
|
+
|
310
|
+
# There are cases where we might want to run something after the worker
|
311
|
+
# successfully completes the ability in question and sends its results
|
312
|
+
if ret && exception.nil?
|
313
|
+
after_ability = @after_abilities[func]
|
314
|
+
if after_ability
|
315
|
+
Util.logger.debug "Running after ability for #{func}..."
|
316
|
+
begin
|
317
|
+
after_ability.run(ret, data)
|
318
|
+
rescue Exception => e
|
319
|
+
nil
|
320
|
+
end
|
321
|
+
end
|
322
|
+
end
|
323
|
+
|
305
324
|
true
|
306
325
|
end
|
307
326
|
|
metadata
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gearman-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
+
hash: 3
|
4
5
|
prerelease: false
|
5
6
|
segments:
|
6
7
|
- 3
|
7
8
|
- 0
|
8
|
-
-
|
9
|
-
version: 3.0.
|
9
|
+
- 2
|
10
|
+
version: 3.0.2
|
10
11
|
platform: ruby
|
11
12
|
authors:
|
12
13
|
- John Ewart
|
@@ -21,7 +22,7 @@ autorequire:
|
|
21
22
|
bindir: bin
|
22
23
|
cert_chain: []
|
23
24
|
|
24
|
-
date: 2010-
|
25
|
+
date: 2010-08-20 00:00:00 -07:00
|
25
26
|
default_executable:
|
26
27
|
dependencies: []
|
27
28
|
|
@@ -76,8 +77,9 @@ files:
|
|
76
77
|
- test/mock_worker_test.rb
|
77
78
|
- test/util_test.rb
|
78
79
|
- test/worker_test.rb
|
80
|
+
- examples/bgclient.rb
|
79
81
|
has_rdoc: true
|
80
|
-
homepage: http://github.com/
|
82
|
+
homepage: http://github.com/johnewart/gearman-ruby
|
81
83
|
licenses: []
|
82
84
|
|
83
85
|
post_install_message:
|
@@ -86,23 +88,27 @@ rdoc_options:
|
|
86
88
|
require_paths:
|
87
89
|
- lib
|
88
90
|
required_ruby_version: !ruby/object:Gem::Requirement
|
91
|
+
none: false
|
89
92
|
requirements:
|
90
93
|
- - ">="
|
91
94
|
- !ruby/object:Gem::Version
|
95
|
+
hash: 3
|
92
96
|
segments:
|
93
97
|
- 0
|
94
98
|
version: "0"
|
95
99
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
|
+
none: false
|
96
101
|
requirements:
|
97
102
|
- - ">="
|
98
103
|
- !ruby/object:Gem::Version
|
104
|
+
hash: 3
|
99
105
|
segments:
|
100
106
|
- 0
|
101
107
|
version: "0"
|
102
108
|
requirements: []
|
103
109
|
|
104
110
|
rubyforge_project:
|
105
|
-
rubygems_version: 1.3.
|
111
|
+
rubygems_version: 1.3.7
|
106
112
|
signing_key:
|
107
113
|
specification_version: 3
|
108
114
|
summary: Library for the Gearman distributed job system
|
@@ -112,6 +118,7 @@ test_files:
|
|
112
118
|
- test/mock_worker_test.rb
|
113
119
|
- test/util_test.rb
|
114
120
|
- test/worker_test.rb
|
121
|
+
- examples/bgclient.rb
|
115
122
|
- examples/calculus_client.rb
|
116
123
|
- examples/calculus_worker.rb
|
117
124
|
- examples/client.rb
|