Skip to content
This repository has been archived by the owner on Mar 14, 2019. It is now read-only.

Commit

Permalink
Merge pull request #17 from stitchfix/feature/allow-alternate-url-format
Browse files Browse the repository at this point in the history
Allows a second format for URLs
  • Loading branch information
davetron5000 committed May 7, 2015
2 parents dbb4364 + 0ce67c6 commit 677264d
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 12 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Expand Up @@ -7,5 +7,4 @@ script:
services:
- redis-server
rvm:
- 2.1.0
- 2.2.0
- 2.1.6
5 changes: 5 additions & 0 deletions app/models/missing_resque_configuration_error.rb
@@ -0,0 +1,5 @@
class MissingResqueConfigurationError < StandardError
def initialize(environment_variables,resque_name)
super("Couldn't find environment variable for Resque #{resque_name}. Tried: #{environment_variables.join(',')}")
end
end
24 changes: 24 additions & 0 deletions app/models/resque_url.rb
@@ -0,0 +1,24 @@
class ResqueUrl
def initialize(resque_name)
@resque_name = resque_name
end

def url
@url ||= environment_variables.map { |environment_variable|
ENV[environment_variable]
}.compact.first
if @url.nil?
raise MissingResqueConfigurationError.new(environment_variables,@resque_name)
end
@url
end

private

def environment_variables
[
"RESQUE_BRAIN_INSTANCES_#{@resque_name}",
"#{@resque_name.gsub(/-/,'_').upcase}_RESQUE_REDIS_URL",
]
end
end
8 changes: 2 additions & 6 deletions app/models/resques.rb
Expand Up @@ -8,17 +8,12 @@
#
# And then use `RESQUES` to access the configured resques.
class Resques
class MissingResqueConfigurationError < StandardError
end

# Parses the environment, yielding each configured instance to the block
def self.from_environment
self.new(String(ENV["RESQUE_BRAIN_INSTANCES"]).split(/\s*,\s*/).map { |instance_name|
redis = Redis::Namespace.new(:resque,redis: Redis.new(url: ENV.fetch("RESQUE_BRAIN_INSTANCES_#{instance_name}")))
redis = Redis::Namespace.new(:resque,redis: Redis.new(url: ResqueUrl.new(instance_name).url))
ResqueInstance.new(name: instance_name, resque_data_store: Resque::DataStore.new(redis))
})
rescue KeyError => ex
raise MissingResqueConfigurationError,ex.message
end

def initialize(instances)
Expand All @@ -34,4 +29,5 @@ def all
def find(name)
@instances[name]
end

end
13 changes: 9 additions & 4 deletions test/models/resques_test.rb
@@ -1,14 +1,16 @@
require 'quick_test_helper'
require 'minitest/autorun'
require 'support/fake_resque_data_store'
rails_require 'models/resque_url'
rails_require 'models/missing_resque_configuration_error'
rails_require 'models/resques'
rails_require 'models/resque_instance'

class ResquesTest < MiniTest::Test
def teardown
ENV["RESQUE_BRAIN_INSTANCES"] = nil
ENV["RESQUE_BRAIN_INSTANCES_env1"] = nil
ENV["RESQUE_BRAIN_INSTANCES_env2"] = nil
ENV["ENV2_RESQUE_REDIS_URL"] = nil
end

def test_all
Expand All @@ -27,7 +29,7 @@ def test_find_not_exists
def test_from_environment
ENV["RESQUE_BRAIN_INSTANCES"] = "env1,env2"
ENV["RESQUE_BRAIN_INSTANCES_env1"] = "redis://whatever:supersecret@localhost:1234"
ENV["RESQUE_BRAIN_INSTANCES_env2"] = "redis://whatever:megasecret@10.0.0.1:4567"
ENV["ENV2_RESQUE_REDIS_URL"] = "redis://whatever:megasecret@10.0.0.1:4567"

resques = Resques.from_environment

Expand All @@ -53,11 +55,14 @@ def test_from_environment
def test_from_environment_missing_config
ENV["RESQUE_BRAIN_INSTANCES"] = "env1,env2"
ENV["RESQUE_BRAIN_INSTANCES_env1"] = "redis://localhost:1234"
ENV["RESQUE_BRAIN_INSTANCES_env2"] = nil
ENV["ENV2_RESQUE_REDIS_URL"] = nil

assert_raises(Resques::MissingResqueConfigurationError) do
exception = assert_raises(MissingResqueConfigurationError) do
Resques.from_environment
end
assert_match /env2/, exception.message
assert_match /RESQUE_BRAIN_INSTANCES_env2/, exception.message
assert_match /ENV2_RESQUE_REDIS_URL/, exception.message
end

private
Expand Down

0 comments on commit 677264d

Please sign in to comment.