File tree Expand file tree Collapse file tree 2 files changed +53
-1
lines changed Expand file tree Collapse file tree 2 files changed +53
-1
lines changed Original file line number Diff line number Diff line change @@ -151,7 +151,7 @@ def work(interval = 5.0, &block)
151
151
else
152
152
unregister_signal_handlers if !@cant_fork
153
153
procline "Processing #{ job . queue } since #{ Time . now . to_i } "
154
- redis . client . reconnect # Don't share connection with parent
154
+ reconnect
155
155
perform ( job , &block )
156
156
exit! unless @cant_fork
157
157
end
@@ -224,6 +224,24 @@ def reserve(interval = 5.0)
224
224
Job . new ( queue . name , job ) if queue && job
225
225
end
226
226
227
+ # Reconnect to Redis to avoid sharing a connection with the parent,
228
+ # retry up to 3 times with increasing delay before giving up.
229
+ def reconnect
230
+ tries = 0
231
+ begin
232
+ redis . client . reconnect
233
+ rescue Redis ::BaseConnectionError
234
+ if ( tries += 1 ) <= 3
235
+ log "Error reconnecting to Redis; retrying"
236
+ sleep ( tries )
237
+ retry
238
+ else
239
+ log "Error reconnecting to Redis; quitting"
240
+ raise
241
+ end
242
+ end
243
+ end
244
+
227
245
# Returns a list of queues to use when searching for a job.
228
246
# A splat ("*") means you want every queue (in alpha order) - this
229
247
# can be useful for dynamically adding new queues. Low priority queues
Original file line number Diff line number Diff line change @@ -504,6 +504,40 @@ def self.perform
504
504
refute_equal original_connection , Resque . redis . client . connection . instance_variable_get ( "@sock" )
505
505
end
506
506
507
+ it "tries to reconnect three times before giving up" do
508
+ begin
509
+ class Redis ::Client
510
+ alias_method :original_reconnect , :reconnect
511
+
512
+ def reconnect
513
+ raise Redis ::BaseConnectionError
514
+ end
515
+ end
516
+
517
+ class Resque ::Worker
518
+ alias_method :original_sleep , :sleep
519
+
520
+ def sleep ( duration = nil )
521
+ # noop
522
+ end
523
+ end
524
+
525
+ @worker . very_verbose = true
526
+ stdout , stderr = capture_io { @worker . work ( 0 ) }
527
+
528
+ assert_equal 3 , stdout . scan ( /retrying/ ) . count
529
+ assert_equal 1 , stdout . scan ( /quitting/ ) . count
530
+ ensure
531
+ class Redis ::Client
532
+ alias_method :reconnect , :original_reconnect
533
+ end
534
+
535
+ class Resque ::Worker
536
+ alias_method :sleep , :original_sleep
537
+ end
538
+ end
539
+ end
540
+
507
541
it "will call before_pause before it is paused" do
508
542
before_pause_called = false
509
543
captured_worker = nil
You can’t perform that action at this time.
0 commit comments