Skip to content

Commit

Permalink
Merge pull request #317 from phstc/remove-aws-config
Browse files Browse the repository at this point in the history
WIP remove AwsConfig

Fix #293
  • Loading branch information
phstc committed Feb 17, 2017
2 parents 4f2ba80 + 574f927 commit 477c5f5
Show file tree
Hide file tree
Showing 16 changed files with 85 additions and 403 deletions.
73 changes: 3 additions & 70 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ end

[Check the Middleware documentation](https://github.com/phstc/shoryuken/wiki/Middleware).

### Configuration (worker side)
### Shoryuken Configuration

Sample configuration file `shoryuken.yml`.

Expand All @@ -118,82 +118,15 @@ queues:
- [low_priority, 1]
```

And setup ```aws``` options to use ```configure_client``` in `config/initializers/shoryuken.rb`:
#### AWS Configuration

```ruby
Shoryuken.configure_client do |config|
config.aws = {
secret_access_key: ..., # or ENV["AWS_SECRET_ACCESS_KEY"]
access_key_id: ..., # or ENV["AWS_ACCESS_KEY_ID"]
region: "us-east-1", # or ENV["AWS_REGION"]
receive_message: { # See http://docs.aws.amazon.com/sdkforruby/api/Aws/SQS/Client.html#receive_message-instance_method
# wait_time_seconds: N, # The number of seconds to wait for new messages when polling. Defaults to the #wait_time_seconds defined on the queue
attribute_names: [
"ApproximateReceiveCount",
"SentTimestamp"
]
}
}
end
```

If you use Shoryuken with plain ruby worker class (not Rails), please call `configure_client` at the beginning of the worker file:

```ruby
Shoryuken.configure_client do |config|
config.aws = {
secret_access_key: ENV["AWS_SECRET_ACCESS_KEY"],
access_key_id: ENV["AWS_ACCESS_KEY_ID"],
region: ENV["AWS_REGION"]
}
end
class MyWorker
end
```

The `aws` section is used to configure both the Aws objects used by Shoryuken internally, and also to set up some Shoryuken-specific config. The Shoryuken-specific keys are listed below, and you can expect any other key defined in that block to be passed on untouched to `Aws::SQS::Client#initialize`:

- `account_id` is used when generating SNS ARNs
- `sns_endpoint` can be used to explicitly override the SNS endpoint
- `sqs_endpoint` can be used to explicitly override the SQS endpoint
- `receive_message` can be used to define the options passed to the http://docs.aws.amazon.com/sdkforruby/api/Aws/SQS/Client.html#receive_message-instance_method
There are a few ways to configure the AWS client:

The `sns_endpoint` and `sqs_endpoint` Shoryuken-specific options will also fallback to the environment variables `AWS_SNS_ENDPOINT` and `AWS_SQS_ENDPOINT` respectively, if they are set.

### Configuration (producer side)

'Producer' processes need permissions to put messages into SQS. There are a few ways:

* Use the `configure_server` in Rails initializer
* Ensure the `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` env vars are set.
* Create a `~/.aws/credentials` file.
* Set `Aws.config[:credentials]` from Ruby code (e.g. in a Rails initializer)
* Use the Instance Profiles feature. The IAM role of the targeted machine must have an adequate SQS Policy.

For example, use the `configure_server` in `config/initializers/shoryuken.rb`:

```ruby
Shoryuken.configure_client do |config|
config.aws = {
secret_access_key: ENV["AWS_SECRET_ACCESS_KEY"],
access_key_id: ENV["AWS_ACCESS_KEY_ID"],
region: ENV["AWS_REGION"]
}
end
Shoryuken.configure_server do |config|
config.aws = {
secret_access_key: ENV["AWS_SECRET_ACCESS_KEY"],
access_key_id: ENV["AWS_ACCESS_KEY_ID"],
region: ENV["AWS_REGION"]
}
end
```


Note that storing your credentials into Amazon instances represents a security risk. Instance Profiles tends to be the best choice.

You can read about these in more detail [here](http://docs.aws.amazon.com/sdkforruby/api/Aws/SQS/Client.html).

### Rails Integration
Expand Down
120 changes: 67 additions & 53 deletions lib/shoryuken.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
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 All @@ -21,8 +20,6 @@
Shoryuken::Middleware::Server.autoload :AutoExtendVisibility, 'shoryuken/middleware/server/auto_extend_visibility'
require 'shoryuken/middleware/server/exponential_backoff_retry'
require 'shoryuken/middleware/server/timing'
require 'shoryuken/sns_arn'
require 'shoryuken/topic'
require 'shoryuken/polling'
require 'shoryuken/manager'
require 'shoryuken/launcher'
Expand All @@ -39,78 +36,102 @@ module Shoryuken
lifecycle_events: {
startup: [],
quiet: [],
shutdown: [],
shutdown: []
},
polling_strategy: Polling::WeightedRoundRobin,
}
polling_strategy: Polling::WeightedRoundRobin
}.freeze

@@queues = []
@@worker_registry = DefaultWorkerRegistry.new
@@active_job_queue_name_prefixing = false
@@sqs_client = nil
@@sqs_client_receive_message_opts = {}
@@start_callback = nil
@@stop_callback = nil

class << self
def options
@options ||= DEFAULTS.dup
end

def queues
@@queues
end

def logger
Shoryuken::Logging.logger
end

def register_worker(*args)
worker_registry.register_worker(*args)
def worker_registry
@@worker_registry
end

def worker_registry=(worker_registry)
@@worker_registry = worker_registry
end

def worker_registry
@@worker_registry
def start_callback
@@start_callback
end

def start_callback=(start_callback)
@@start_callback = start_callback
end

def stop_callback
@@stop_callback
end

def stop_callback=(stop_callback)
@@stop_callback = stop_callback
end

def active_job_queue_name_prefixing
@@active_job_queue_name_prefixing
end

def active_job_queue_name_prefixing=(prefixing)
@@active_job_queue_name_prefixing = prefixing
def active_job_queue_name_prefixing=(active_job_queue_name_prefixing)
@@active_job_queue_name_prefixing = active_job_queue_name_prefixing
end

def sqs_client
@@sqs_client
end

def sqs_client=(sqs_client)
@@sqs_client
end

def sqs_client_receive_message_opts
@@sqs_client_receive_message_opts
end

def sqs_client_receive_message_opts=(sqs_client_receive_message_opts)
@@sqs_client_receive_message_opts
end

def options
@@options ||= DEFAULTS.dup
end

def logger
Shoryuken::Logging.logger
end

def register_worker(*args)
@@worker_registry.register_worker(*args)
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

def server_middleware
@server_chain ||= default_server_middleware
yield @server_chain if block_given?
@server_chain
@@server_chain ||= default_server_middleware
yield @@server_chain if block_given?
@@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 unless server?
end

def client_middleware
@client_chain ||= default_client_middleware
yield @client_chain if block_given?
@client_chain
@@client_chain ||= default_client_middleware
yield @@client_chain if block_given?
@@client_chain
end

def default_worker_options
Expand All @@ -120,27 +141,24 @@ def default_worker_options
'auto_delete' => false,
'auto_visibility_timeout' => false,
'retry_intervals' => nil,
'batch' => false }
'batch' => false
}
end

def default_worker_options=(options)
@@default_worker_options = options
end

def on_aws_initialization(&block)
@aws_initialization_callback = block
def default_worker_options=(default_worker_options)
@@default_worker_options = default_worker_options
end

def on_start(&block)
@start_callback = block
@@start_callback = block
end

def on_stop(&block)
@stop_callback = block
@@stop_callback = block
end

def aws=(hash)
Shoryuken::AwsConfig.setup(hash)
def sqs_client
@@sqs_client ||= Aws::SQS::Client.new
end

# Register a block to run at a point in the Shoryuken lifecycle.
Expand All @@ -157,10 +175,6 @@ def on(event, &block)
options[:lifecycle_events][event] << block
end

attr_reader :aws_initialization_callback,
:start_callback,
:stop_callback

private

def default_server_middleware
Expand Down
64 changes: 0 additions & 64 deletions lib/shoryuken/aws_config.rb

This file was deleted.

18 changes: 3 additions & 15 deletions lib/shoryuken/client.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,19 @@
module Shoryuken
class Client
@@queues = {}
@@topics = {}

class << self
def queues(name)
@@queues[name.to_s] ||= Shoryuken::Queue.new(sqs, name)
end

def sns
@sns ||= Shoryuken::AwsConfig.sns
end

def sns_arn
@sns_arn ||= SnsArn
end

def sqs
@sqs ||= Shoryuken::AwsConfig.sqs
@@sqs ||= Shoryuken.sqs_client
end

def topics(name)
@@topics[name.to_s] ||= Topic.new(name, sns)
def sqs=(sqs)
@@sqs = sqs
end

attr_accessor :account_id
attr_writer :sns, :sqs, :sqs_resource, :sns_arn
end
end
end
Loading

0 comments on commit 477c5f5

Please sign in to comment.