aws-sdk-rails 2.1.0 → 3.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (29) hide show
  1. checksums.yaml +4 -4
  2. data/VERSION +1 -0
  3. data/bin/aws_sqs_active_job +5 -0
  4. data/lib/action_dispatch/session/dynamodb_store.rb +32 -0
  5. data/lib/active_job/queue_adapters/amazon_sqs_adapter.rb +49 -0
  6. data/lib/active_job/queue_adapters/amazon_sqs_async_adapter.rb +32 -0
  7. data/lib/aws-sdk-rails.rb +15 -31
  8. data/lib/aws/rails/mailer.rb +7 -8
  9. data/lib/aws/rails/notifications.rb +33 -0
  10. data/lib/aws/rails/railtie.rb +68 -0
  11. data/lib/aws/rails/sqs_active_job/configuration.rb +152 -0
  12. data/lib/aws/rails/sqs_active_job/executor.rb +58 -0
  13. data/lib/aws/rails/sqs_active_job/job_runner.rb +22 -0
  14. data/lib/aws/rails/sqs_active_job/lambda_handler.rb +66 -0
  15. data/lib/aws/rails/sqs_active_job/poller.rb +126 -0
  16. data/lib/generators/aws_record/base.rb +217 -0
  17. data/lib/generators/aws_record/generated_attribute.rb +129 -0
  18. data/lib/generators/aws_record/model/USAGE +24 -0
  19. data/lib/generators/aws_record/model/model_generator.rb +21 -0
  20. data/lib/generators/aws_record/model/templates/model.rb +48 -0
  21. data/lib/generators/aws_record/model/templates/table_config.rb +18 -0
  22. data/lib/generators/aws_record/secondary_index.rb +60 -0
  23. data/lib/generators/dynamo_db/session_store_migration/USAGE +13 -0
  24. data/lib/generators/dynamo_db/session_store_migration/session_store_migration_generator.rb +46 -0
  25. data/lib/generators/dynamo_db/session_store_migration/templates/dynamo_db_session_store.yml +70 -0
  26. data/lib/generators/dynamo_db/session_store_migration/templates/session_store_migration.rb +9 -0
  27. data/lib/tasks/aws_record/migrate.rake +12 -0
  28. data/lib/tasks/dynamo_db/session_store.rake +8 -0
  29. metadata +110 -14
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 105a6ec68c6b8ca61c5d4601a3102223b2a7bd29cd643635b8f46b50c0b918a4
4
- data.tar.gz: ef976f4a27a8542e5a264f2baad36a914ad2918068461b083a166a5ab8590f39
3
+ metadata.gz: b252fc5ed5514c1ab48750fc65acde4008f1f6a8b685bae42903708f7b987a14
4
+ data.tar.gz: da794888829a46dfeec19350997e753b9858bf07a0668ef603594d9db46bdf9a
5
5
  SHA512:
6
- metadata.gz: 39f7819a13cfa4ec5a95d7ef85e11830224b445006f2b2f168700ac2840f4f02646cfb694316a4c973ead7c56325bd1fb9f7ac67ec8f3d61b41e38a9f7513361
7
- data.tar.gz: 6b60ca1d73b53de41c89a2c6d4acd15500240dc34514b5291de56a1ebfcb7962df9242d3576b7512724d041f5771d4fc1c792959e3449613c0e1f6a075379fa2
6
+ metadata.gz: 5c5a5f74b8faab027f2d8b3b64e32a4feb2e85c29207d051a66e93424b6b71d8dbdf0dd80ba1baac1b9be66489759c8efba1860fdba7d41990fbd8279d173b08
7
+ data.tar.gz: 227f06499dbd9a1705eae1ffb00777502cbfa3f512c8e07b1a037d5b04a35afcc651487518ea582e646cf6060682112a85d8baf59e6a9d543eb7beb16c8e3fa7
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 3.4.0
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative '../lib/aws/rails/sqs_active_job/poller'
4
+
5
+ Aws::Rails::SqsActiveJob::Poller.new.run
@@ -0,0 +1,32 @@
1
+ require 'aws-sessionstore-dynamodb'
2
+
3
+ module ActionDispatch
4
+ module Session
5
+ # Uses the Dynamo DB Session Store implementation to create a class that
6
+ # extends ActionDispatch::Session. Rails will create a :dynamodb_store
7
+ # configuration for session_store from this class name.
8
+ #
9
+ # This class will use the Rails secret_key_base unless otherwise provided.
10
+ #
11
+ # Configuration can also be provided in YAML files from Rails config, either
12
+ # in "config/session_store.yml" or "config/session_store/#{Rails.env}.yml".
13
+ # Configuration files that are environment-specific will take precedence.
14
+ #
15
+ # @see https://docs.aws.amazon.com/sdk-for-ruby/aws-sessionstore-dynamodb/api/Aws/SessionStore/DynamoDB/Configuration.html
16
+ class DynamodbStore < Aws::SessionStore::DynamoDB::RackMiddleware
17
+ def initialize(app, options = {})
18
+ options[:config_file] ||= config_file if config_file.exist?
19
+ options[:secret_key] ||= Rails.application.secret_key_base
20
+ super
21
+ end
22
+
23
+ private
24
+
25
+ def config_file
26
+ file = Rails.root.join("config/dynamo_db_session_store/#{Rails.env}.yml")
27
+ file = Rails.root.join('config/dynamo_db_session_store.yml') unless file.exist?
28
+ file
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'aws-sdk-sqs'
4
+
5
+ module ActiveJob
6
+ module QueueAdapters
7
+
8
+ class AmazonSqsAdapter
9
+
10
+ def enqueue(job)
11
+ _enqueue(job)
12
+ end
13
+
14
+ def enqueue_at(job, timestamp)
15
+ delay = (timestamp - Time.now.to_f).floor
16
+ raise ArgumentError, 'Unable to queue a job with a delay great than 15 minutes' if delay > 15.minutes
17
+ _enqueue(job, delay_seconds: delay)
18
+ end
19
+
20
+ private
21
+
22
+ def _enqueue(job, send_message_opts = {})
23
+ body = job.serialize
24
+ queue_url = Aws::Rails::SqsActiveJob.config.queue_url_for(job.queue_name)
25
+ send_message_opts[:queue_url] = queue_url
26
+ send_message_opts[:message_body] = Aws::Json.dump(body)
27
+ send_message_opts[:message_attributes] = message_attributes(job)
28
+ Aws::Rails::SqsActiveJob.config.client.send_message(send_message_opts)
29
+ end
30
+
31
+ def message_attributes(job)
32
+ {
33
+ 'aws_sqs_active_job_class' => {
34
+ string_value: job.class.to_s,
35
+ data_type: 'String'
36
+ },
37
+ 'aws_sqs_active_job_version' => {
38
+ string_value: Aws::Rails::VERSION,
39
+ data_type: 'String'
40
+ }
41
+ }
42
+ end
43
+ end
44
+
45
+ # create an alias to allow `:amazon` to be used as the adapter name
46
+ # `:amazon` is the convention used for ActionMailer and ActiveStorage
47
+ AmazonAdapter = AmazonSqsAdapter
48
+ end
49
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'aws-sdk-sqs'
4
+ require 'concurrent'
5
+
6
+ module ActiveJob
7
+ module QueueAdapters
8
+
9
+ # == Async adapter for Amazon SQS ActiveJob
10
+ #
11
+ # This adapter queues jobs asynchronously (ie non-blocking). Error handler can be configured
12
+ # with +Aws::Rails::SqsActiveJob.config.async_queue_error_handler+.
13
+ #
14
+ # To use this adapter, set up as:
15
+ #
16
+ # config.active_job.queue_adapter = :amazon_sqs_async
17
+ class AmazonSqsAsyncAdapter < AmazonSqsAdapter
18
+
19
+ private
20
+
21
+ def _enqueue(job, send_message_opts = {})
22
+ Concurrent::Promise
23
+ .execute { super(job, send_message_opts) }
24
+ .on_error do |e|
25
+ Rails.logger.error "Failed to queue job #{job}. Reason: #{e}"
26
+ error_handler = Aws::Rails::SqsActiveJob.config.async_queue_error_handler
27
+ error_handler.call(e, job, send_message_opts) if error_handler
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -1,37 +1,21 @@
1
- require_relative 'aws/rails/mailer'
2
-
3
- module Aws
4
- module Rails
1
+ # frozen_string_literal: true
5
2
 
6
- # @api private
7
- class Railtie < ::Rails::Railtie
8
- initializer "aws-sdk-rails.initialize", before: :load_config_initializers do |app|
9
- # Initialization Actions
10
- Aws::Rails.add_action_mailer_delivery_method
11
- Aws::Rails.log_to_rails_logger
12
- end
13
- end
3
+ require_relative 'aws/rails/mailer'
4
+ require_relative 'aws/rails/railtie'
5
+ require_relative 'aws/rails/notifications'
6
+ require_relative 'aws/rails/sqs_active_job/configuration'
7
+ require_relative 'aws/rails/sqs_active_job/executor'
8
+ require_relative 'aws/rails/sqs_active_job/job_runner'
9
+ require_relative 'aws/rails/sqs_active_job/lambda_handler'
14
10
 
15
- # This is called automatically from the SDK's Railtie, but if you want to
16
- # manually specify options for building the Aws::SES::Client object, you
17
- # can manually call this method.
18
- #
19
- # @param [Symbol] name The name of the ActionMailer delivery method to
20
- # register.
21
- # @param [Hash] options The options you wish to pass on to the
22
- # Aws::SES::Client initialization method.
23
- def self.add_action_mailer_delivery_method(name = :aws_sdk, options = {})
24
- ActiveSupport.on_load(:action_mailer) do
25
- self.add_delivery_method(name, Aws::Rails::Mailer, options)
26
- end
27
- end
11
+ require_relative 'action_dispatch/session/dynamodb_store'
12
+ require_relative 'active_job/queue_adapters/amazon_sqs_adapter'
13
+ require_relative 'active_job/queue_adapters/amazon_sqs_async_adapter'
28
14
 
29
- # Configures the AWS SDK for Ruby's logger to use the Rails logger.
30
- def self.log_to_rails_logger
31
- Aws.config[:logger] = ::Rails.logger
32
- nil
33
- end
15
+ require_relative 'generators/aws_record/base'
34
16
 
17
+ module Aws
18
+ module Rails
19
+ VERSION = File.read(File.expand_path('../VERSION', __dir__)).strip
35
20
  end
36
21
  end
37
-
@@ -1,23 +1,23 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'aws-sdk-ses'
2
4
 
3
5
  module Aws
4
6
  module Rails
5
-
6
7
  # Provides a delivery method for ActionMailer that uses Amazon Simple Email
7
8
  # Service.
8
- #
9
+ #
9
10
  # Once you have an SES delivery method you can configure Rails to
10
11
  # use this for ActionMailer in your environment configuration
11
12
  # (e.g. RAILS_ROOT/config/environments/production.rb)
12
13
  #
13
- # config.action_mailer.delivery_method = :aws_sdk
14
+ # config.action_mailer.delivery_method = :ses
14
15
  #
15
- # Uses the AWS SDK for Ruby V2's credential provider chain when creating an
16
- # SES client instance.
16
+ # Uses the AWS SDK for Ruby's credential provider chain when creating an SES
17
+ # client instance.
17
18
  class Mailer
18
-
19
19
  # @param [Hash] options Passes along initialization options to
20
- # [Aws::SES::Client.new](http://docs.aws.amazon.com/sdkforruby/api/Aws/SES/Client.html#initialize-instance_method).
20
+ # [Aws::SES::Client.new](https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/SES/Client.html#initialize-instance_method).
21
21
  def initialize(options = {})
22
22
  @client = SES::Client.new(options)
23
23
  end
@@ -42,7 +42,6 @@ module Aws
42
42
  def settings
43
43
  {}
44
44
  end
45
-
46
45
  end
47
46
  end
48
47
  end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'aws-sdk-core'
4
+ require 'active_support/notifications'
5
+
6
+ module Aws
7
+ module Rails
8
+
9
+ # Instruments client operation calls for ActiveSupport::Notifications
10
+ # Each client operation will produce an event with name:
11
+ # <operation>.<service>.aws
12
+ # @api private
13
+ class Notifications < Seahorse::Client::Plugin
14
+
15
+ def add_handlers(handlers, config)
16
+ # This plugin needs to be first
17
+ # which means it is called first in the stack, to start recording time,
18
+ # and returns last
19
+ handlers.add(Handler, step: :initialize, priority: 99)
20
+ end
21
+
22
+ class Handler < Seahorse::Client::Handler
23
+
24
+ def call(context)
25
+ event_name = "#{context.operation_name}.#{context.config.api.metadata['serviceId']}.aws"
26
+ ActiveSupport::Notifications.instrument(event_name, context: context) do
27
+ @handler.call(context)
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Aws
4
+ # Use the Rails namespace.
5
+ module Rails
6
+ # @api private
7
+ class Railtie < ::Rails::Railtie
8
+ initializer 'aws-sdk-rails.initialize',
9
+ before: :load_config_initializers do
10
+ # Initialization Actions
11
+ Aws::Rails.use_rails_encrypted_credentials
12
+ Aws::Rails.add_action_mailer_delivery_method
13
+ Aws::Rails.log_to_rails_logger
14
+ end
15
+
16
+ rake_tasks do
17
+ load 'tasks/dynamo_db/session_store.rake'
18
+ load 'tasks/aws_record/migrate.rake'
19
+ end
20
+ end
21
+
22
+ # This is called automatically from the SDK's Railtie, but can be manually
23
+ # called if you want to specify options for building the Aws::SES::Client.
24
+ #
25
+ # @param [Symbol] name The name of the ActionMailer delivery method to
26
+ # register.
27
+ # @param [Hash] options The options you wish to pass on to the
28
+ # Aws::SES::Client initialization method.
29
+ def self.add_action_mailer_delivery_method(name = :ses, options = {})
30
+ ActiveSupport.on_load(:action_mailer) do
31
+ add_delivery_method(name, Aws::Rails::Mailer, options)
32
+ end
33
+ end
34
+
35
+ # Configures the AWS SDK for Ruby's logger to use the Rails logger.
36
+ def self.log_to_rails_logger
37
+ Aws.config[:logger] = ::Rails.logger
38
+ nil
39
+ end
40
+
41
+ # Configures the AWS SDK with credentials from Rails encrypted credentials.
42
+ def self.use_rails_encrypted_credentials
43
+ # limit the config keys we merge to credentials only
44
+ aws_credential_keys = %i[access_key_id secret_access_key session_token]
45
+
46
+ Aws.config.merge!(
47
+ ::Rails.application
48
+ .try(:credentials)
49
+ .try(:aws)
50
+ .to_h.slice(*aws_credential_keys)
51
+ )
52
+ end
53
+
54
+ # Adds ActiveSupport Notifications instrumentation to AWS SDK
55
+ # client operations. Each operation will produce an event with a name:
56
+ # <operation>.<service>.aws. For example, S3's put_object has an event
57
+ # name of: put_object.S3.aws
58
+ def self.instrument_sdk_operations
59
+ Aws.constants.each do |c|
60
+ m = Aws.const_get(c)
61
+ if m.is_a?(Module) && m.const_defined?(:Client) &&
62
+ m.const_get(:Client).superclass == Seahorse::Client::Base
63
+ m.const_get(:Client).add_plugin(Aws::Rails::Notifications)
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,152 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Aws
4
+ module Rails
5
+ module SqsActiveJob
6
+
7
+ # @return [Configuration] the (singleton) Configuration
8
+ def self.config
9
+ @config ||= Configuration.new
10
+ end
11
+
12
+ # @yield Configuration
13
+ def self.configure
14
+ yield(config)
15
+ end
16
+
17
+ # Configuration for AWS SQS ActiveJob.
18
+ # Use +Aws::Rails::SqsActiveJob.config+ to access the singleton config instance.
19
+ class Configuration
20
+
21
+ # Default configuration options
22
+ # @api private
23
+ DEFAULTS = {
24
+ max_messages: 10,
25
+ visibility_timeout: 120,
26
+ shutdown_timeout: 15,
27
+ queues: {},
28
+ logger: ::Rails.logger
29
+ }
30
+
31
+ # @api private
32
+ attr_accessor :queues, :max_messages, :visibility_timeout,
33
+ :shutdown_timeout, :client, :logger, :async_queue_error_handler
34
+
35
+ # Don't use this method directly: Confugration is a singleton class, use
36
+ # +Aws::Rails::SqsActiveJob.config+ to access the singleton config.
37
+ #
38
+ # @param [Hash] options
39
+ # @option options [Hash[Symbol, String]] :queues A mapping between the
40
+ # active job queue name and the SQS Queue URL. Note: multiple active
41
+ # job queues can map to the same SQS Queue URL.
42
+ #
43
+ # @option options [Integer] :max_messages
44
+ # The max number of messages to poll for in a batch.
45
+ #
46
+ # @option options [Integer] :visibility_timeout
47
+ # The visibility timeout is the number of seconds
48
+ # that a message will not be processable by any other consumers.
49
+ # You should set this value to be longer than your expected job runtime
50
+ # to prevent other processes from picking up an running job.
51
+ # See the (SQS Visibility Timeout Documentation)[https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-visibility-timeout.html]
52
+ #
53
+ # @option options [Integer] :shutdown_timeout
54
+ # the amount of time to wait
55
+ # for a clean shutdown. Jobs that are unable to complete in this time
56
+ # will not be deleted from the SQS queue and will be retryable after
57
+ # the visibility timeout.
58
+ #
59
+ # @option options [ActiveSupport::Logger] :logger Logger to use
60
+ # for the poller.
61
+ #
62
+ # @option options [String] :config_file
63
+ # Override file to load configuration from. If not specified will
64
+ # attempt to load from config/aws_sqs_active_job.yml.
65
+ #
66
+ # @option options [Callable] :async_queue_error_handler An error handler
67
+ # to be called when the async active job adapter experiances an error
68
+ # queueing a job. Only applies when
69
+ # +active_job.queue_adapter = :amazon_sqs_async+. Called with:
70
+ # [error, job, job_options]
71
+ #
72
+ # @option options [SQS::Client] :client SQS Client to use. A default
73
+ # client will be created if none is provided.
74
+ def initialize(options = {})
75
+ options[:config_file] ||= config_file if config_file.exist?
76
+ options = DEFAULTS
77
+ .merge(file_options(options))
78
+ .merge(options)
79
+ set_attributes(options)
80
+ end
81
+
82
+ def client
83
+ @client ||= Aws::SQS::Client.new(user_agent_suffix: user_agent)
84
+ end
85
+
86
+ # Return the queue_url for a given job_queue name
87
+ def queue_url_for(job_queue)
88
+ job_queue = job_queue.to_sym
89
+ raise ArgumentError, "No queue defined for #{job_queue}" unless queues.key? job_queue
90
+
91
+ queues[job_queue.to_sym]
92
+ end
93
+
94
+ # @api private
95
+ def to_s
96
+ to_h.to_s
97
+ end
98
+
99
+ # @api private
100
+ def to_h
101
+ h = {}
102
+ self.instance_variables.each do |v|
103
+ v_sym = v.to_s.gsub('@', '').to_sym
104
+ val = self.instance_variable_get(v)
105
+ h[v_sym] = val
106
+ end
107
+ h
108
+ end
109
+
110
+ private
111
+
112
+ # Set accessible attributes after merged options.
113
+ def set_attributes(options)
114
+ options.keys.each do |opt_name|
115
+ instance_variable_set("@#{opt_name}", options[opt_name])
116
+ end
117
+ end
118
+
119
+ def file_options(options = {})
120
+ file_path = config_file_path(options)
121
+ if file_path
122
+ load_from_file(file_path)
123
+ else
124
+ {}
125
+ end
126
+ end
127
+
128
+ def config_file
129
+ file = ::Rails.root.join("config/aws_sqs_active_job/#{::Rails.env}.yml")
130
+ file = ::Rails.root.join('config/aws_sqs_active_job.yml') unless file.exist?
131
+ file
132
+ end
133
+
134
+ # Load options from YAML file
135
+ def load_from_file(file_path)
136
+ require "erb"
137
+ opts = YAML.load(ERB.new(File.read(file_path)).result) || {}
138
+ opts.deep_symbolize_keys
139
+ end
140
+
141
+ # @return [String] Configuration path found in environment or YAML file.
142
+ def config_file_path(options)
143
+ options[:config_file] || ENV["AWS_SQS_ACTIVE_JOB_CONFIG_FILE"]
144
+ end
145
+
146
+ def user_agent
147
+ "ft/aws-sdk-rails-activejob/#{Aws::Rails::VERSION}"
148
+ end
149
+ end
150
+ end
151
+ end
152
+ end