gearman-ruby 4.0.4 → 4.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f8799efeb8ec1766ffb262f401595cf9f1029fdc
4
- data.tar.gz: e672b3358f6bda8dc55467e1a5af72973d422e2d
3
+ metadata.gz: 6ffdbe675e2808d933b8fb7ebdd8ffe5a42b0814
4
+ data.tar.gz: a1633bb7cb2c83ecc631e8f32bb15719d99a06c2
5
5
  SHA512:
6
- metadata.gz: efea3f3ae2c88664d13b56debf6ec69e533c43136c8b2555f6c93de7ade16406e51ccee0fd27d3a40cecb07f347281f92139b8b30a2cc6a12c1f9ac49c1d36a8
7
- data.tar.gz: 5e391604849cec1c2ae53b300d9c0812b2a9f55b9a76faa0f9843b48cc4368a82f44adc0d545ad9adbdbae54552ae6115089365361a725c1180f985193a0b66a
6
+ metadata.gz: d7ef5d38b28304b96945bc9a7d9c023acec174ff6dbc3585c19dbb123d2dd2e6aeeb70edfc7e0ed5e81d2bfe7f65b424d8ddac75b5e8993864808cc1f13f3dbd
7
+ data.tar.gz: bed878e4570169263cf3106ad7db091204d7533db1bdff81be6c882f3a8a96b9833ea679b2807423a5218201d4a80efe9e50adc5b43effdf981d04076826a078
@@ -0,0 +1,22 @@
1
+ $LOAD_PATH.unshift("../lib")
2
+ require 'rubygems'
3
+ require 'gearman'
4
+
5
+ # Control logger
6
+ l = Logger.new($stdout)
7
+ l.level = Logger::DEBUG
8
+ Gearman.logger=l
9
+
10
+ servers = ['localhost:4730']
11
+
12
+ client = Gearman::Client.new(servers)
13
+
14
+ # Submitting work with no explicit unique ID will
15
+ # cause one to be generated by the client based on
16
+ # the job queue and the data, these 10 jobs should
17
+ # get de-duped into one job in the server (provided
18
+ # that nobody is pulling from the queue in the middle)
19
+ (0..10).each do |i|
20
+ task = Gearman::BackgroundTask.new('dedupe', 'data')
21
+ client.do_task(task)
22
+ end
@@ -0,0 +1,19 @@
1
+ $LOAD_PATH.unshift("../lib")
2
+ require 'rubygems'
3
+ require 'gearman'
4
+
5
+ # Control logger
6
+ l = Logger.new($stdout)
7
+ l.level = Logger::DEBUG
8
+ Gearman.logger=l
9
+
10
+ servers = ['localhost:4730']
11
+
12
+ worker = Gearman::Worker.new(servers)
13
+
14
+ worker.add_ability('dedupe') do |data, job|
15
+ puts "Should only see one of these come through!"
16
+ true
17
+ end
18
+
19
+ loop { worker.work }
@@ -98,7 +98,7 @@ module Gearman
98
98
  raise ProtocolError, message
99
99
  end
100
100
  rescue NetworkError
101
- message = "Network error on read from #{hostport} while adding job, marking server bad"
101
+ message = "Network error on read from #{connection.hostport} while adding job, marking server bad"
102
102
  logger.error message
103
103
  raise NetworkError, message
104
104
  rescue NoJobServersError
@@ -5,6 +5,8 @@ module Gearman
5
5
  include Logging
6
6
 
7
7
  DEFAULT_PORT = 4730
8
+ TIME_BETWEEN_CHECKS = 60 #seconds
9
+ SLEEP_TIME = 30 #seconds
8
10
 
9
11
  def initialize(servers = [])
10
12
  @bad_servers = []
@@ -14,9 +16,9 @@ module Gearman
14
16
  @reconnect_seconds = 10
15
17
  @server_counter = 0 # Round-robin distribution of requests
16
18
  @servers_mutex = Mutex.new
19
+ @last_check_time = Time.now
17
20
 
18
21
  add_servers(servers)
19
- start_reconnect_thread
20
22
  end
21
23
 
22
24
  def add_connection(connection)
@@ -49,7 +51,6 @@ module Gearman
49
51
 
50
52
  def get_connection(coalesce_key = nil)
51
53
  @servers_mutex.synchronize do
52
-
53
54
  logger.debug "Available job servers: #{@job_servers.inspect}"
54
55
  raise NoJobServersError if @job_servers.empty?
55
56
  @server_counter += 1
@@ -62,6 +63,7 @@ module Gearman
62
63
  end
63
64
 
64
65
  def poll_connections(timeout = nil)
66
+ update_job_servers
65
67
  available_sockets = []
66
68
  @servers_mutex.synchronize do
67
69
  available_sockets.concat @job_servers.collect { |conn| conn.socket }
@@ -73,6 +75,7 @@ module Gearman
73
75
  end
74
76
 
75
77
  def with_all_connections(&block)
78
+ update_job_servers
76
79
  @servers_mutex.synchronize do
77
80
  @job_servers.each do |connection|
78
81
  begin
@@ -88,6 +91,10 @@ module Gearman
88
91
 
89
92
  private
90
93
 
94
+ def time_to_check_connections
95
+ (Time.now - @last_check_time) >= TIME_BETWEEN_CHECKS
96
+ end
97
+
91
98
  def deactivate_connection(connection)
92
99
  @job_servers.reject! { |c| c == connection }
93
100
  @bad_servers << connection
@@ -99,31 +106,35 @@ module Gearman
99
106
  @connection_handler.call(connection) if @connection_handler
100
107
  end
101
108
 
102
- def start_reconnect_thread
103
- Thread.new do
104
- loop do
105
- @servers_mutex.synchronize do
106
- # If there are any failed servers, try to reconnect to them.
107
- update_job_servers unless @bad_servers.empty?
108
- end
109
- sleep @reconnect_seconds
110
- end
111
- end.run
112
- end
113
-
109
+ ##
110
+ # Check for bad servers and update the available pools
111
+ #
114
112
  def update_job_servers
113
+ # Check if it's been > TIME_BETWEEN_CHECKS or we have no good servers
114
+ return unless time_to_check_connections || @job_servers.empty?
115
+
115
116
  logger.debug "Found #{@bad_servers.size} zombie connections, checking pulse."
116
- @bad_servers.each do |connection|
117
- begin
118
- message = "Testing server #{connection}..."
119
- if connection.is_healthy?
120
- logger.debug "#{message} Connection is healthy, putting back into service"
121
- activate_connection(connection)
122
- else
123
- logger.debug "#{message} Still down."
117
+ @servers_mutex.synchronize do
118
+ @bad_servers.each do |connection|
119
+ begin
120
+ message = "Testing server #{connection}..."
121
+ if connection.is_healthy?
122
+ logger.debug "#{message} Connection is healthy, putting back into service"
123
+ activate_connection(connection)
124
+ else
125
+ logger.debug "#{message} Still down."
126
+ end
124
127
  end
125
128
  end
126
129
  end
130
+
131
+ # Sleep for a few to allow a chance for the world to become sane
132
+ if @job_servers.empty?
133
+ logger.warn "No job servers available, sleeping for #{SLEEP_TIME} seconds"
134
+ sleep(SLEEP_TIME)
135
+ end
136
+
137
+ @last_check_time = Time.now
127
138
  end
128
139
 
129
140
 
@@ -1,3 +1,3 @@
1
1
  module Gearman
2
- VERSION = '4.0.4'
2
+ VERSION = '4.0.5'
3
3
  end
@@ -51,6 +51,19 @@ describe Gearman::Client do
51
51
 
52
52
  end
53
53
 
54
+ it 'propagates NetworkErrors while submitting jobs' do
55
+ mock_connection = double(Gearman::Connection)
56
+ mock_connection.stub(:send_request).and_raise(Gearman::NetworkError)
57
+ mock_connection.stub(:hostport).and_return("localhost:4730")
58
+
59
+ @mock_connection_pool.stub(:get_connection) { mock_connection }
60
+
61
+ task = Gearman::Task.new("queue", "data")
62
+
63
+ expect {
64
+ @client.submit_job(task)
65
+ }.to raise_error(Gearman::NetworkError)
66
+ end
54
67
 
55
68
 
56
69
  it "should raise a NetworkError when it didn't write as much as expected to a socket" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gearman-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.4
4
+ version: 4.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Ewart
@@ -27,8 +27,10 @@ files:
27
27
  - Rakefile
28
28
  - examples/client.rb
29
29
  - examples/client_background_jobs.rb
30
+ - examples/client_dedupe.rb
30
31
  - examples/client_reverse_string.rb
31
32
  - examples/worker.rb
33
+ - examples/worker_dedupe.rb
32
34
  - examples/worker_reverse_string.rb
33
35
  - gearman-ruby.gemspec
34
36
  - lib/gearman.rb