forked from resque/resque
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
introduces a new class, Client, which will handle Redis stuff from now on. As such, it needs the reconnect logic.
- Loading branch information
1 parent
b805082
commit 65616ca
Showing
6 changed files
with
157 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
## | ||
# Resque::Client is a wrapper around all things Redis. | ||
# | ||
# This provides a level of indirection so that the rest of our code | ||
# doesn't need to know anything about Redis, and allows us to someday | ||
# maybe even move away from Redis to another backend if we need to. | ||
# | ||
# Also helps because we can mock this out in our tests. Only mock | ||
# stuff you own. | ||
# | ||
# Also, we can theoretically have multiple Redis/Resques going on | ||
# one project. | ||
module Resque | ||
class Client | ||
attr_reader :backend | ||
|
||
def initialize(backend) | ||
@backend = backend | ||
|
||
@reconnected = false | ||
end | ||
|
||
# Reconnect to Redis to avoid sharing a connection with the parent, | ||
# retry up to 3 times with increasing delay before giving up. | ||
def reconnect | ||
return if @reconnected | ||
|
||
tries = 0 | ||
begin | ||
backend.client.reconnect | ||
@reconnected = true | ||
rescue Redis::BaseConnectionError | ||
if (tries += 1) <= 3 | ||
Resque.logger.info "Error reconnecting to Redis; retrying" | ||
sleep(tries) | ||
retry | ||
else | ||
Resque.logger.info "Error reconnecting to Redis; quitting" | ||
raise | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.