Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add aws configuration in 'Shoryuken.configure_server' #252

Merged
merged 3 commits into from
Oct 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion lib/shoryuken.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require 'shoryuken/core_ext'
require 'shoryuken/util'
require 'shoryuken/logging'
require 'shoryuken/aws_config'
require 'shoryuken/environment_loader'
require 'shoryuken/queue'
require 'shoryuken/message'
Expand Down Expand Up @@ -72,6 +73,12 @@ def active_job_queue_name_prefixing=(prefixing)
@@active_job_queue_name_prefixing = prefixing
end

##
# Configuration for Shoryuken server, use like:
#
# Shoryuken.configure_server do |config|
# config.aws = { :sqs_endpoint => '...', :access_key_id: '...', :secret_access_key: '...', region: '...' }
# end
def configure_server
yield self if server?
end
Expand All @@ -82,8 +89,14 @@ def server_middleware
@server_chain
end

##
# Configuration for Shoryuken client, use like:
#
# Shoryuken.configure_client do |config|
# config.aws = { :sqs_endpoint => '...', :access_key_id: '...', :secret_access_key: '...', region: '...' }
# end
def configure_client
yield self
yield self unless server?
end

def client_middleware
Expand Down Expand Up @@ -118,6 +131,10 @@ def on_stop(&block)
@stop_callback = block
end

def aws=(hash)
@aws = Shoryuken::AwsConfig.setup(hash)
end
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@h3poteto is @aws being used?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, @aws is not used!


# Register a block to run at a point in the Shoryuken lifecycle.
# :startup, :quiet or :shutdown are valid events.
#
Expand Down
47 changes: 47 additions & 0 deletions lib/shoryuken/aws_config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# frozen_string_literal: true
module Shoryuken
class AwsConfig
class << self
attr_writer :options

def options
@options ||= {}
end

def setup(hash)
# aws-sdk tries to load the credentials from the ENV variables: AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY
# when not explicit supplied
return if hash.empty?

self.options = hash

shoryuken_keys = %w(
account_id
sns_endpoint
sqs_endpoint
receive_message
).map(&:to_sym)

aws_options = hash.reject do |k, _|
shoryuken_keys.include?(k)
end

# assume credentials based authentication
credentials = Aws::Credentials.new(
aws_options.delete(:access_key_id),
aws_options.delete(:secret_access_key)
)

# but only if the configuration options have valid values
aws_options = aws_options.merge(credentials: credentials) if credentials.set?

if (callback = Shoryuken.aws_initialization_callback)
Shoryuken.logger.info { 'Calling Shoryuken.on_aws_initialization block' }
callback.call(aws_options)
end

Aws.config = aws_options
end
end
end
end
2 changes: 1 addition & 1 deletion lib/shoryuken/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def topics(name)

def aws_client_options(service_endpoint_key)
environment_endpoint = ENV["AWS_#{service_endpoint_key.to_s.upcase}"]
explicit_endpoint = Shoryuken.options[:aws][service_endpoint_key] || environment_endpoint
explicit_endpoint = Shoryuken::AwsConfig.options[service_endpoint_key] || environment_endpoint
options = {}
options[:endpoint] = explicit_endpoint unless explicit_endpoint.to_s.empty?
options
Expand Down
29 changes: 1 addition & 28 deletions lib/shoryuken/environment_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,34 +44,7 @@ def config_file_options
end

def initialize_aws
# aws-sdk tries to load the credentials from the ENV variables: AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY
# when not explicit supplied
return if Shoryuken.options[:aws].empty?

shoryuken_keys = %w(
account_id
sns_endpoint
sqs_endpoint
receive_message).map(&:to_sym)

aws_options = Shoryuken.options[:aws].reject do |k, v|
shoryuken_keys.include?(k)
end

# assume credentials based authentication
credentials = Aws::Credentials.new(
aws_options.delete(:access_key_id),
aws_options.delete(:secret_access_key))

# but only if the configuration options have valid values
aws_options = aws_options.merge(credentials: credentials) if credentials.set?

if (callback = Shoryuken.aws_initialization_callback)
Shoryuken.logger.info { 'Calling Shoryuken.on_aws_initialization block' }
callback.call(aws_options)
end

Aws.config = aws_options
Shoryuken::AwsConfig.setup(Shoryuken.options[:aws])
end

def initialize_logger
Expand Down
2 changes: 1 addition & 1 deletion lib/shoryuken/fetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def receive_messages(queue, limit)
# AWS limits the batch size by 10
limit = limit > FETCH_LIMIT ? FETCH_LIMIT : limit

options = (Shoryuken.options[:aws][:receive_message] || {}).dup
options = (Shoryuken::AwsConfig.options[:receive_message] || {}).dup
options[:max_number_of_messages] = limit
options[:message_attribute_names] = %w(All)
options[:attribute_names] = %w(All)
Expand Down
1 change: 1 addition & 0 deletions spec/shoryuken/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
ENV['AWS_SNS_ENDPOINT'] = sns_endpoint
ENV['AWS_REGION'] = 'us-east-1'
Shoryuken.options[:aws] = {}
Shoryuken::AwsConfig.options = {}
end

it 'will use config file settings if set' do
Expand Down
1 change: 1 addition & 0 deletions spec/shoryuken/queue_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def call(options)
end

before do
allow(Shoryuken).to receive(:server?).and_return(false)
Shoryuken.configure_client do |config|
config.client_middleware do |chain|
chain.add MyClientMiddleware
Expand Down