Skip to content

Commit

Permalink
Fix active-elastic-job#51 Add lazy loading of AWS credential configur…
Browse files Browse the repository at this point in the history
…ation

The default configuration of AWS credentials for this gem
uses AWS instance profiles for retrieving credentials. It
seems that `Aws::InstanceProfileCredentials.new` is blocking
if not executed on an actual EC2 instance, which consequently
impacts the startup time of the application. Therefore,
we postpone the initialization of the credential configuration
to when it's actually needed in the queue adapter.
  • Loading branch information
tawan committed Feb 5, 2017
1 parent 7abe07e commit 8a870e6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,19 @@ The snippet below shows the various configurable settings and their defaults.
```Ruby
Rails.application.configure do
config.active_elastic_job.process_jobs = ENV['PROCESS_ACTIVE_ELASTIC_JOBS'] == 'true'
config.active_elastic_job.aws_credentials = Aws::InstanceProfileCredentials.new
config.active_elastic_job.aws_credentials = lambda { Aws::InstanceProfileCredentials.new } # allows lambdas for lazy loading
config.active_elastic_job.secret_key_base = Rails.application.secrets[:secret_key_base]
cofnig.active_elastic_job.periodic_tasks_route = '/periodic_tasks'.freeze
end
```
If you want to provide the AWS credentials not by the EC2 instance prodfile, but via environment variables, you can do so:
```Ruby
Rails.application.configure do
config.active_elastic_job.aws_credentials = Aws::Credentials.new(ENV['AWS_ACCESS_KEY_ID'], ENV['AWS_SECRET_ACCESS_KEY'])
end
```
## FAQ
A summary of frequently asked questions:
### What are the advantages in comparison to popular alternatives like Resque, Sidekiq or DelayedJob?
Expand Down
2 changes: 1 addition & 1 deletion lib/active_elastic_job/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module ActiveElasticJob
class Railtie < Rails::Railtie
config.active_elastic_job = ActiveSupport::OrderedOptions.new
config.active_elastic_job.process_jobs = ENV['PROCESS_ACTIVE_ELASTIC_JOBS'] == 'true'
config.active_elastic_job.aws_credentials = Aws::InstanceProfileCredentials.new
config.active_elastic_job.aws_credentials = lambda { Aws::InstanceProfileCredentials.new }
config.active_elastic_job.periodic_tasks_route = '/periodic_tasks'.freeze

initializer "active_elastic_job.insert_middleware" do |app|
Expand Down
6 changes: 5 additions & 1 deletion lib/active_job/queue_adapters/active_elastic_job_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,11 @@ def aws_sqs_client
end

def aws_sqs_client_credentials
config.aws_credentials
@aws_credentials ||= if config.aws_credentials.kind_of?(Proc)
config.aws_credentials.call
else
config.aws_credentials
end
end

def aws_region
Expand Down

0 comments on commit 8a870e6

Please sign in to comment.