gearman-ruby 2.0.0 → 3.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. data/Rakefile +5 -6
  2. data/VERSION.yml +2 -2
  3. data/examples/calculus_client.rb +8 -10
  4. data/examples/calculus_worker.rb +4 -1
  5. data/examples/client.php +23 -0
  6. data/examples/client.rb +6 -10
  7. data/examples/client_background.rb +7 -7
  8. data/examples/client_data.rb +4 -4
  9. data/examples/client_epoch.rb +23 -0
  10. data/examples/client_exception.rb +4 -2
  11. data/examples/client_prefix.rb +4 -2
  12. data/examples/client_reverse.rb +27 -0
  13. data/examples/scale_image.rb +4 -3
  14. data/examples/scale_image_worker.rb +1 -1
  15. data/examples/worker.rb +2 -4
  16. data/examples/worker_data.rb +2 -2
  17. data/examples/worker_exception.rb +1 -1
  18. data/examples/worker_prefix.rb +1 -1
  19. data/examples/worker_reverse_string.rb +27 -0
  20. data/examples/worker_reverse_to_file.rb +18 -0
  21. data/examples/{evented_worker.rb → worker_signals.rb} +18 -8
  22. data/lib/gearman.rb +68 -21
  23. data/lib/gearman/client.rb +137 -65
  24. data/lib/gearman/server.rb +4 -4
  25. data/lib/gearman/task.rb +140 -20
  26. data/lib/gearman/taskset.rb +280 -5
  27. data/lib/gearman/testlib.rb +95 -0
  28. data/lib/gearman/util.rb +184 -28
  29. data/lib/gearman/worker.rb +356 -20
  30. data/test/client_test.rb +145 -0
  31. data/test/mock_client_test.rb +629 -0
  32. data/test/mock_worker_test.rb +321 -0
  33. data/test/util_test.rb +8 -3
  34. data/test/worker_test.rb +50 -34
  35. metadata +41 -41
  36. data/examples/client_echo.rb +0 -16
  37. data/examples/evented_client.rb +0 -23
  38. data/examples/worker_echo.rb +0 -20
  39. data/examples/worker_echo_pprof.rb +0 -5
  40. data/gearman-ruby.gemspec +0 -111
  41. data/lib/gearman/evented/client.rb +0 -99
  42. data/lib/gearman/evented/reactor.rb +0 -86
  43. data/lib/gearman/evented/worker.rb +0 -118
  44. data/lib/gearman/job.rb +0 -38
  45. data/lib/gearman/protocol.rb +0 -110
  46. data/test/basic_integration_test.rb +0 -121
  47. data/test/crash_test.rb +0 -69
  48. data/test/job_test.rb +0 -30
  49. data/test/protocol_test.rb +0 -132
  50. data/test/test_helper.rb +0 -31
@@ -1,16 +0,0 @@
1
- require 'rubygems'
2
- require '../lib/gearman'
3
-
4
- servers = ['localhost:4730']
5
- client = Gearman::Client.new(servers)
6
- taskset = Gearman::TaskSet.new(client)
7
-
8
- 100000.times do |i|
9
- task = Gearman::Task.new('echo', "hello #{i}")
10
- task.on_complete {|d| puts d }
11
- taskset.add_task(task)
12
- end
13
- taskset.wait(100)
14
-
15
- taskset.add_task(Gearman::Task.new('quit'))
16
- taskset.wait(100)
@@ -1,23 +0,0 @@
1
- require 'rubygems'
2
- require "ruby-debug"
3
- require '../lib/gearman'
4
-
5
- Gearman::Util.debug = true
6
-
7
- servers = ['localhost:4730', 'localhost:4731']
8
- client = Gearman::Client.new(servers)
9
-
10
- taskset = Gearman::Taskset.new
11
-
12
- task = Gearman::Task.new('sleep', 2)
13
- task.on_status {|numerator, denominator| puts "TASK 1: Completed #{numerator} of #{denominator}"}
14
- task.on_complete {|d| puts "TASK 1: #{d}" }
15
- taskset << task
16
-
17
- task = Gearman::Task.new('sleep', 15, :poll_status_interval => 2, :uuid => nil)
18
- task.on_status {|numerator, denominator| puts "TASK 2: Completed #{numerator} of #{denominator}"}
19
- task.on_data {|data| puts "TASK 2 DATA: #{data}" }
20
- task.on_complete {|d| puts "TASK 2: #{d}" }
21
- taskset << task
22
-
23
- client.run(taskset)
@@ -1,20 +0,0 @@
1
- require 'rubygems'
2
- require '../lib/gearman'
3
- # require 'perftools'
4
-
5
- quit = false
6
- # PerfTools::CpuProfiler.start("/tmp/worker_echo_profile") do
7
- servers = ['localhost:4730']
8
- worker = Gearman::Worker.new(servers)
9
-
10
- worker.add_ability('echo') do |data, job|
11
- data
12
- end
13
- worker.add_ability('quit') do |data, job|
14
- quit = true
15
- end
16
- loop {
17
- break if quit
18
- worker.work
19
- }
20
- # end
@@ -1,5 +0,0 @@
1
- require 'rubygems'
2
- require 'perftools'
3
- PerfTools::CpuProfiler.start("/tmp/worker_echo_profile") do
4
- require File.dirname(__FILE__) + "/worker_echo.rb"
5
- end
data/gearman-ruby.gemspec DELETED
@@ -1,111 +0,0 @@
1
- # Generated by jeweler
2
- # DO NOT EDIT THIS FILE
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
- # -*- encoding: utf-8 -*-
5
-
6
- Gem::Specification.new do |s|
7
- s.name = %q{gearman-ruby}
8
- s.version = "2.0.0"
9
-
10
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["Kim Altintop"]
12
- s.date = %q{2009-12-11}
13
- s.description = %q{Library for the Gearman distributed job system}
14
- s.email = %q{kim.altintop@gmail.com}
15
- s.extra_rdoc_files = [
16
- "LICENSE",
17
- "README"
18
- ]
19
- s.files = [
20
- ".gitignore",
21
- "HOWTO",
22
- "LICENSE",
23
- "README",
24
- "Rakefile",
25
- "TODO",
26
- "VERSION.yml",
27
- "examples/calculus_client.rb",
28
- "examples/calculus_worker.rb",
29
- "examples/client.rb",
30
- "examples/client_background.rb",
31
- "examples/client_data.rb",
32
- "examples/client_exception.rb",
33
- "examples/client_prefix.rb",
34
- "examples/evented_client.rb",
35
- "examples/evented_worker.rb",
36
- "examples/gearman_environment.sh",
37
- "examples/scale_image.rb",
38
- "examples/scale_image_worker.rb",
39
- "examples/server.rb",
40
- "examples/worker.rb",
41
- "examples/worker_data.rb",
42
- "examples/worker_exception.rb",
43
- "examples/worker_prefix.rb",
44
- "gearman-ruby.gemspec",
45
- "lib/gearman.rb",
46
- "lib/gearman/client.rb",
47
- "lib/gearman/evented/client.rb",
48
- "lib/gearman/evented/reactor.rb",
49
- "lib/gearman/evented/worker.rb",
50
- "lib/gearman/job.rb",
51
- "lib/gearman/protocol.rb",
52
- "lib/gearman/server.rb",
53
- "lib/gearman/task.rb",
54
- "lib/gearman/taskset.rb",
55
- "lib/gearman/util.rb",
56
- "lib/gearman/worker.rb",
57
- "test/basic_integration_test.rb",
58
- "test/crash_test.rb",
59
- "test/job_test.rb",
60
- "test/protocol_test.rb",
61
- "test/test_helper.rb",
62
- "test/util_test.rb",
63
- "test/worker_test.rb"
64
- ]
65
- s.homepage = %q{http://github.com/kim/gearman-ruby}
66
- s.rdoc_options = ["--charset=UTF-8"]
67
- s.require_paths = ["lib"]
68
- s.rubygems_version = %q{1.3.5}
69
- s.summary = %q{Library for the Gearman distributed job system}
70
- s.test_files = [
71
- "test/basic_integration_test.rb",
72
- "test/crash_test.rb",
73
- "test/job_test.rb",
74
- "test/protocol_test.rb",
75
- "test/test_helper.rb",
76
- "test/util_test.rb",
77
- "test/worker_test.rb",
78
- "examples/calculus_client.rb",
79
- "examples/calculus_worker.rb",
80
- "examples/client.rb",
81
- "examples/client_background.rb",
82
- "examples/client_data.rb",
83
- "examples/client_echo.rb",
84
- "examples/client_exception.rb",
85
- "examples/client_prefix.rb",
86
- "examples/evented_client.rb",
87
- "examples/evented_worker.rb",
88
- "examples/scale_image.rb",
89
- "examples/scale_image_worker.rb",
90
- "examples/server.rb",
91
- "examples/worker.rb",
92
- "examples/worker_data.rb",
93
- "examples/worker_echo.rb",
94
- "examples/worker_echo_pprof.rb",
95
- "examples/worker_exception.rb",
96
- "examples/worker_prefix.rb"
97
- ]
98
-
99
- if s.respond_to? :specification_version then
100
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
101
- s.specification_version = 3
102
-
103
- if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
104
- s.add_runtime_dependency(%q<eventmachine>, [">= 0.12.8"])
105
- else
106
- s.add_dependency(%q<eventmachine>, [">= 0.12.8"])
107
- end
108
- else
109
- s.add_dependency(%q<eventmachine>, [">= 0.12.8"])
110
- end
111
- end
@@ -1,99 +0,0 @@
1
- module Gearman
2
- module Evented
3
-
4
- module ClientReactor
5
- include Gearman::Evented::Reactor
6
-
7
- def keep_connected
8
- @keep_connected ||= (@opts[:keep_connected] || false)
9
- end
10
-
11
- def keep_connected=(keep)
12
- @keep_connected = keep
13
- end
14
-
15
- def connection_completed
16
- @cbs_job_created ||= []
17
- @pending_jobs = []
18
- @assigned_jobs = {}
19
- @background_jobs = {}
20
- super
21
- end
22
-
23
- def receive_data(data)
24
- packets = Gearman::Protocol.decode_response(data)
25
- log "received #{packets.size} packet(s) at once"
26
- log "packets: #{packets.inspect}"
27
- packets.each do |type, handle, *data|
28
- dispatch_packet(type, handle, *data)
29
- end
30
- end
31
-
32
- def dispatch_packet_callback(&callback)
33
- @dispatch_packet_callback = callback
34
- end
35
-
36
- def dispatch_packet(type, handle, *data)
37
- log "Got #{type.to_s}, #{handle}, #{data.inspect} from #{server}"
38
- if type == :job_created
39
- job_created(handle)
40
- if cb = @cbs_job_created.shift
41
- cb.call(handle)
42
- end
43
- else
44
- dispatch(type, handle, data)
45
- end
46
- end
47
-
48
- def submit_job(task, &cb_job_created)
49
- cmd = "submit_job"
50
- cmd << "_#{task.priority}" if [ :high, :low ].include?(task.priority)
51
- cmd << "_bg" if task.background
52
-
53
- log "#{cmd} #{task.name}, #{task.payload} to #{server}"
54
- send cmd.to_sym, [ task.name, task.hash, task.payload ].join("\0")
55
- @pending_jobs << task
56
- @cbs_job_created << cb_job_created if cb_job_created
57
- end
58
-
59
- def job_created(handle)
60
- job = @pending_jobs.shift
61
- raise ProtocolError, "No job waiting for handle! (#{handle})" unless job
62
- EM.add_periodic_timer(job.poll_status_interval) { get_status(handle) } if job.poll_status_interval
63
- if job.background
64
- @background_jobs[handle] = job
65
- else
66
- @assigned_jobs[handle] = job
67
- end
68
- end
69
-
70
- def get_status(handle)
71
- send :get_status, handle
72
- end
73
-
74
- def dispatch(type, handle, args)
75
- return unless type
76
- task = @assigned_jobs[handle]
77
- task = @background_jobs[handle] unless task
78
- raise ProtocolError, "No task by that name: #{handle}" unless task
79
-
80
- if :work_fail == type && task.should_retry?
81
- task.dispatch(:on_retry, task.retries_done)
82
- @assigned_jobs.delete(handle)
83
- submit_job(task)
84
- return
85
- end
86
-
87
- if type == :status_res
88
- task.dispatch(:on_status, args)
89
- else
90
- task.dispatch(type.to_s.sub("work", "on"), *args)
91
- end
92
-
93
- @assigned_jobs.delete(handle) if [:work_complete, :work_fail].include?(type)
94
- disconnect if @assigned_jobs.empty? && !keep_connected
95
- end
96
- end
97
-
98
- end
99
- end
@@ -1,86 +0,0 @@
1
- module Gearman
2
- module Evented
3
-
4
- module Reactor
5
- include EM::Deferrable
6
-
7
- def self.included(mod)
8
- mod.instance_eval do
9
- def connect(host, port, opts = {})
10
- Gearman::Evented::Reactor.connect(host, port, self, opts)
11
- end
12
- end
13
- end
14
-
15
- def self.connect(host, port, reactor, opts = {})
16
- EM.connect(host, (port || 4730), reactor) do |c|
17
- c.instance_eval do
18
- @host = host
19
- @port = port || 4730
20
- @opts = opts
21
- end
22
- end
23
- end
24
-
25
- def connected?
26
- @connected
27
- end
28
-
29
- def server
30
- @hostport ||= [ @host, @port ].join(":")
31
- end
32
-
33
- def connection_completed
34
- log "connected to #{@host}:#{@port}"
35
- @connected = true
36
- @reconnecting = false
37
- @reconnect = true
38
- succeed
39
- end
40
-
41
- def unbind
42
- log "disconnected from #{@host}:#{@port}"
43
- @connected = false
44
- EM.next_tick { reconnect } unless !@reconnect
45
- @reconnect = true
46
- end
47
-
48
- def disconnect
49
- log "force disconnect from #{@host}:#{@port}"
50
- @reconnect = false
51
- close_connection_after_writing
52
- end
53
-
54
- def reconnect(force = false)
55
- if @reconnecting
56
- EM.add_timer(@opts[:reconnect_sec] || 30) { reconnect }
57
- return
58
- elsif !@reconnect && !force
59
- log "forced disconnect, aborting reconnect attempt"
60
- @reconnect = true
61
- return
62
- else
63
- @reconnecting = true
64
- @deferred_status = nil
65
- end
66
-
67
- log "reconnecting to #{@host}:#{@port}"
68
- EM.reconnect(@host, @port.to_i, self)
69
- end
70
-
71
- def send(command, data = nil)
72
- log "send #{command} #{data.inspect} (#{server})"
73
- send_data(Gearman::Protocol.encode_request(command, data))
74
- end
75
-
76
- def log(msg, force = false)
77
- Gearman::Util.log(msg, force)
78
- end
79
-
80
- def to_s
81
- "#{@host}:#{@port}"
82
- end
83
- end
84
-
85
- end
86
- end
@@ -1,118 +0,0 @@
1
- module Gearman
2
- module Evented
3
-
4
- module WorkerReactor
5
- include Gearman::Evented::Reactor
6
-
7
- def connection_completed
8
- send :set_client_id, client_id
9
- super
10
-
11
- @abilities ||= @opts.delete(:abilities) || []
12
- @abilities.each do |ability, args|
13
- announce_ability(ability, args[:timeout])
14
- end
15
-
16
- grab_job
17
- end
18
-
19
- def announce_ability(name, timeout)
20
- cmd = timeout ? :can_do_timeout : :can_do
21
- arg = timeout ? "#{name}\0#{timeout.to_s}" : name
22
- log "announce_ability #{name} #{timeout}"
23
- send cmd, arg
24
- end
25
-
26
- def announce_disability(name)
27
- send :cant_do, name
28
- end
29
-
30
- def grab_job
31
- log "Grab Job"
32
- send :grab_job_uniq
33
- end
34
-
35
- def work_fail(handle)
36
- send :work_fail, handle
37
- end
38
-
39
- def work_complete(handle, data)
40
- send :work_complete, "#{handle}\0#{data}"
41
- end
42
-
43
- def work_warning(handle, message)
44
- send :work_warning, "#{handle}\0#{message}"
45
- end
46
-
47
- def receive_data(data)
48
- Gearman::Protocol.decode_response(data).each do |type, handle, *data|
49
- dispatch_packet(type, handle, *data)
50
- end
51
- end
52
-
53
- def dispatch_packet(type, handle, *data)
54
- success = true
55
- timer = 0
56
- case type
57
- when :no_job
58
- send :pre_sleep
59
- timer = @opts[:reconnect_sec] || 30
60
- when :job_assign, :job_assign_uniq
61
- log "job assign #{handle}, #{data.inspect}"
62
- handle_job_assign(handle, data[0], data[1])
63
- when :noop
64
- log "NOOP"
65
- when :error
66
- log "[ERROR]: error from server #{server}: #{data}"
67
- else
68
- log "Got unknown #{type}, #{data} from #{server}"
69
- end
70
-
71
- EM.add_timer(timer) { grab_job }
72
- succeed [handle, data]
73
- end
74
-
75
- def handle_job_assign(handle, func, args = '')
76
- return unless handle
77
- unless func
78
- log "ERROR: Ignoring job_assign with no function"
79
- return
80
- end
81
-
82
- log "Got job_assign '#{func}' with handle #{handle} and #{args.size rescue 0} byte(s)"
83
-
84
- unless @abilities.has_key?(func)
85
- log "Ignoring job_assign for unsupported func #{func} with handle #{handle}"
86
- work_fail handle
87
- return
88
- end
89
-
90
- exception = nil
91
- begin
92
- ret = @abilities[func][:callback].call(args, Gearman::Job.new(self, handle))
93
- rescue Exception => e
94
- exception = e
95
- end
96
-
97
- if ret && exception.nil?
98
- ret = ret.to_s
99
- log "Sending work_complete for #{handle} with #{ret.size} byte(s)"
100
- work_complete handle, ret
101
- elsif exception.nil?
102
- log "Sending work_fail for #{handle} to #{server}"
103
- work_fail handle
104
- elsif exception
105
- log "exception #{exception.message}, sending work_warning, work_fail for #{handle}"
106
- work_warning handle, exception.message
107
- work_fail handle
108
- end
109
- end
110
-
111
- def client_id
112
- @client_id ||= `uuidgen`.strip
113
- end
114
-
115
- end
116
-
117
- end
118
- end