sidekiq_sqs_processor 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/README.md +315 -0
- data/lib/generators/sidekiq_sqs_processor/install_generator.rb +71 -0
- data/lib/generators/sidekiq_sqs_processor/templates/README.txt +41 -0
- data/lib/generators/sidekiq_sqs_processor/templates/initializer.rb.tt +66 -0
- data/lib/generators/sidekiq_sqs_processor/templates/worker.rb.tt +59 -0
- data/lib/generators/sidekiq_sqs_processor/templates/worker_spec.rb.tt +54 -0
- data/lib/generators/sidekiq_sqs_processor/templates/worker_test.rb.tt +50 -0
- data/lib/generators/sidekiq_sqs_processor/worker_generator.rb +54 -0
- data/lib/sidekiq_sqs_processor/base_worker.rb +82 -0
- data/lib/sidekiq_sqs_processor/configuration.rb +182 -0
- data/lib/sidekiq_sqs_processor/continuous_poller.rb +128 -0
- data/lib/sidekiq_sqs_processor/railtie.rb +102 -0
- data/lib/sidekiq_sqs_processor/version.rb +5 -0
- data/lib/sidekiq_sqs_processor.rb +161 -0
- metadata +158 -0
@@ -0,0 +1,161 @@
|
|
1
|
+
require 'sidekiq'
|
2
|
+
require 'aws-sdk-sqs'
|
3
|
+
require 'json'
|
4
|
+
require 'benchmark'
|
5
|
+
require 'logger'
|
6
|
+
require 'securerandom'
|
7
|
+
|
8
|
+
require_relative 'sidekiq_sqs_processor/version'
|
9
|
+
require_relative 'sidekiq_sqs_processor/configuration'
|
10
|
+
require_relative 'sidekiq_sqs_processor/base_worker'
|
11
|
+
require_relative 'sidekiq_sqs_processor/continuous_poller'
|
12
|
+
require_relative 'sidekiq_sqs_processor/continuous_poller'
|
13
|
+
require_relative 'sidekiq_sqs_processor/railtie' if defined?(Rails)
|
14
|
+
|
15
|
+
# Main module for the SidekiqSqsProcessor gem
|
16
|
+
# Provides configuration and management interfaces for SQS message processing with Sidekiq
|
17
|
+
module SidekiqSqsProcessor
|
18
|
+
class << self
|
19
|
+
attr_writer :configuration
|
20
|
+
|
21
|
+
# Get the current configuration
|
22
|
+
# @return [SidekiqSqsProcessor::Configuration]
|
23
|
+
def configuration
|
24
|
+
@configuration ||= Configuration.new
|
25
|
+
end
|
26
|
+
|
27
|
+
# Configure the gem
|
28
|
+
# @yield [config] Gives the configuration object to the block
|
29
|
+
# @example
|
30
|
+
# SidekiqSqsProcessor.configure do |config|
|
31
|
+
# config.aws_region = 'us-east-1'
|
32
|
+
# config.queue_urls = ['https://sqs.us-east-1.amazonaws.com/123456789012/my-queue']
|
33
|
+
# end
|
34
|
+
def configure
|
35
|
+
yield(configuration)
|
36
|
+
end
|
37
|
+
|
38
|
+
# Start the continuous poller
|
39
|
+
# @return [Boolean] Whether the poller was started
|
40
|
+
def start_continuous_poller
|
41
|
+
ContinuousPoller.instance.start
|
42
|
+
end
|
43
|
+
|
44
|
+
# Stop the continuous poller
|
45
|
+
# @return [Boolean] Whether the poller was stopped
|
46
|
+
def stop_continuous_poller
|
47
|
+
ContinuousPoller.instance.stop
|
48
|
+
end
|
49
|
+
|
50
|
+
# Check if the continuous poller is running
|
51
|
+
# @return [Boolean] Whether the poller is running
|
52
|
+
def continuous_poller_running?
|
53
|
+
ContinuousPoller.instance.running?
|
54
|
+
end
|
55
|
+
|
56
|
+
# Get statistics about the continuous poller
|
57
|
+
# @return [Hash] Statistics about the poller threads
|
58
|
+
def continuous_poller_stats
|
59
|
+
ContinuousPoller.instance.stats
|
60
|
+
end
|
61
|
+
|
62
|
+
# Get the AWS SQS client
|
63
|
+
# @return [Aws::SQS::Client]
|
64
|
+
def sqs_client
|
65
|
+
@sqs_client ||= create_sqs_client
|
66
|
+
end
|
67
|
+
|
68
|
+
# Explicitly set the SQS client (primarily for testing)
|
69
|
+
# @param client [Aws::SQS::Client] The SQS client to use
|
70
|
+
def sqs_client=(client)
|
71
|
+
@sqs_client = client
|
72
|
+
end
|
73
|
+
|
74
|
+
# Create a new SQS client based on configuration
|
75
|
+
# @return [Aws::SQS::Client] The AWS SQS client
|
76
|
+
def create_sqs_client
|
77
|
+
options = { region: configuration.aws_region }
|
78
|
+
|
79
|
+
# Use custom credentials if provided
|
80
|
+
if configuration.aws_credentials
|
81
|
+
options[:credentials] = configuration.aws_credentials
|
82
|
+
elsif configuration.aws_access_key_id && configuration.aws_secret_access_key
|
83
|
+
options[:access_key_id] = configuration.aws_access_key_id
|
84
|
+
options[:secret_access_key] = configuration.aws_secret_access_key
|
85
|
+
end
|
86
|
+
|
87
|
+
Aws::SQS::Client.new(options)
|
88
|
+
end
|
89
|
+
|
90
|
+
# Get the logger
|
91
|
+
# @return [Logger] The logger
|
92
|
+
def logger
|
93
|
+
configuration.logger || Sidekiq.logger
|
94
|
+
end
|
95
|
+
|
96
|
+
# Reset the configuration and clients
|
97
|
+
# Used primarily for testing
|
98
|
+
def reset!
|
99
|
+
@configuration = nil
|
100
|
+
@sqs_client = nil
|
101
|
+
end
|
102
|
+
|
103
|
+
# Validate the current configuration
|
104
|
+
# @return [Boolean] Whether the configuration is valid
|
105
|
+
# @raise [ArgumentError] If the configuration is invalid
|
106
|
+
def validate_configuration!
|
107
|
+
configuration.validate!
|
108
|
+
end
|
109
|
+
|
110
|
+
# Find all worker classes that inherit from SidekiqSqsProcessor::BaseWorker
|
111
|
+
# @return [Array<Class>] Array of worker classes
|
112
|
+
def worker_classes
|
113
|
+
ObjectSpace.each_object(Class).select { |c| c < BaseWorker rescue false }
|
114
|
+
end
|
115
|
+
|
116
|
+
# Get a worker class by name
|
117
|
+
# @param name [String] The worker class name
|
118
|
+
# @return [Class, nil] The worker class or nil if not found
|
119
|
+
def find_worker_class(name)
|
120
|
+
Object.const_get(name) rescue nil
|
121
|
+
end
|
122
|
+
|
123
|
+
# Enqueue a message directly to a specific worker
|
124
|
+
# @param worker_class [Class, String] The worker class or name
|
125
|
+
# @param message_body [Hash, String] The message body
|
126
|
+
# @param options [Hash] Additional options for the message
|
127
|
+
# @return [String] The Sidekiq job ID
|
128
|
+
def enqueue_message(worker_class, message_body, options = {})
|
129
|
+
# If worker_class is a string, convert to actual class
|
130
|
+
worker_class = Object.const_get(worker_class) if worker_class.is_a?(String)
|
131
|
+
|
132
|
+
# Ensure the worker is a SidekiqSqsProcessor::BaseWorker
|
133
|
+
unless worker_class < BaseWorker
|
134
|
+
raise ArgumentError, "Worker class must inherit from SidekiqSqsProcessor::BaseWorker"
|
135
|
+
end
|
136
|
+
|
137
|
+
# Create a simulated SQS message
|
138
|
+
message_data = {
|
139
|
+
'message_id' => SecureRandom.uuid,
|
140
|
+
'body' => message_body.is_a?(String) ? message_body : JSON.generate(message_body),
|
141
|
+
'attributes' => {},
|
142
|
+
'message_attributes' => options[:message_attributes] || {},
|
143
|
+
'enqueued_at' => Time.now.to_f
|
144
|
+
}
|
145
|
+
|
146
|
+
# Special handling for receipt_handle and queue_url if used for testing
|
147
|
+
message_data['receipt_handle'] = options[:receipt_handle] if options[:receipt_handle]
|
148
|
+
message_data['queue_url'] = options[:queue_url] if options[:queue_url]
|
149
|
+
|
150
|
+
# Enqueue to Sidekiq
|
151
|
+
worker_class.perform_async(message_data)
|
152
|
+
end
|
153
|
+
|
154
|
+
# Handle an error using the configured error handler
|
155
|
+
# @param error [Exception] The error to handle
|
156
|
+
# @param context [Hash] Additional context for the error
|
157
|
+
def handle_error(error, context = {})
|
158
|
+
configuration.handle_error(error, context)
|
159
|
+
end
|
160
|
+
end
|
161
|
+
end
|
metadata
ADDED
@@ -0,0 +1,158 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sidekiq_sqs_processor
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Unnikrishnan KP
|
8
|
+
bindir: bin
|
9
|
+
cert_chain: []
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
11
|
+
dependencies:
|
12
|
+
- !ruby/object:Gem::Dependency
|
13
|
+
name: sidekiq
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
15
|
+
requirements:
|
16
|
+
- - "~>"
|
17
|
+
- !ruby/object:Gem::Version
|
18
|
+
version: '7.0'
|
19
|
+
type: :runtime
|
20
|
+
prerelease: false
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
22
|
+
requirements:
|
23
|
+
- - "~>"
|
24
|
+
- !ruby/object:Gem::Version
|
25
|
+
version: '7.0'
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: aws-sdk-sqs
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - "~>"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '1.0'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '1.0'
|
40
|
+
- !ruby/object:Gem::Dependency
|
41
|
+
name: rails
|
42
|
+
requirement: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - "~>"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '8.0'
|
47
|
+
type: :runtime
|
48
|
+
prerelease: false
|
49
|
+
version_requirements: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - "~>"
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '8.0'
|
54
|
+
- !ruby/object:Gem::Dependency
|
55
|
+
name: rspec
|
56
|
+
requirement: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - "~>"
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '3.0'
|
61
|
+
type: :development
|
62
|
+
prerelease: false
|
63
|
+
version_requirements: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - "~>"
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '3.0'
|
68
|
+
- !ruby/object:Gem::Dependency
|
69
|
+
name: rake
|
70
|
+
requirement: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - "~>"
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '13.0'
|
75
|
+
type: :development
|
76
|
+
prerelease: false
|
77
|
+
version_requirements: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - "~>"
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '13.0'
|
82
|
+
- !ruby/object:Gem::Dependency
|
83
|
+
name: rubocop
|
84
|
+
requirement: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - "~>"
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '1.0'
|
89
|
+
type: :development
|
90
|
+
prerelease: false
|
91
|
+
version_requirements: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - "~>"
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '1.0'
|
96
|
+
- !ruby/object:Gem::Dependency
|
97
|
+
name: simplecov
|
98
|
+
requirement: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - "~>"
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: 0.21.0
|
103
|
+
type: :development
|
104
|
+
prerelease: false
|
105
|
+
version_requirements: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - "~>"
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: 0.21.0
|
110
|
+
description: A Ruby gem that seamlessly integrates Amazon SQS with Sidekiq for efficient
|
111
|
+
and scalable message processing
|
112
|
+
email:
|
113
|
+
- unnikrishnan.kp@bigbinary.com
|
114
|
+
executables: []
|
115
|
+
extensions: []
|
116
|
+
extra_rdoc_files: []
|
117
|
+
files:
|
118
|
+
- README.md
|
119
|
+
- lib/generators/sidekiq_sqs_processor/install_generator.rb
|
120
|
+
- lib/generators/sidekiq_sqs_processor/templates/README.txt
|
121
|
+
- lib/generators/sidekiq_sqs_processor/templates/initializer.rb.tt
|
122
|
+
- lib/generators/sidekiq_sqs_processor/templates/worker.rb.tt
|
123
|
+
- lib/generators/sidekiq_sqs_processor/templates/worker_spec.rb.tt
|
124
|
+
- lib/generators/sidekiq_sqs_processor/templates/worker_test.rb.tt
|
125
|
+
- lib/generators/sidekiq_sqs_processor/worker_generator.rb
|
126
|
+
- lib/sidekiq_sqs_processor.rb
|
127
|
+
- lib/sidekiq_sqs_processor/base_worker.rb
|
128
|
+
- lib/sidekiq_sqs_processor/configuration.rb
|
129
|
+
- lib/sidekiq_sqs_processor/continuous_poller.rb
|
130
|
+
- lib/sidekiq_sqs_processor/railtie.rb
|
131
|
+
- lib/sidekiq_sqs_processor/version.rb
|
132
|
+
homepage: https://github.com/unni/pub-sub-with-sqs/sidekiq-sqs-processor#readme
|
133
|
+
licenses:
|
134
|
+
- MIT
|
135
|
+
metadata:
|
136
|
+
homepage_uri: https://github.com/unni/pub-sub-with-sqs/sidekiq-sqs-processor#readme
|
137
|
+
source_code_uri: https://github.com/unni/pub-sub-with-sqs/sidekiq-sqs-processor
|
138
|
+
changelog_uri: https://github.com/unni/pub-sub-with-sqs/sidekiq-sqs-processor/blob/master/CHANGELOG.md
|
139
|
+
documentation_uri: https://github.com/unni/pub-sub-with-sqs/sidekiq-sqs-processor/wiki
|
140
|
+
bug_tracker_uri: https://github.com/unni/pub-sub-with-sqs/sidekiq-sqs-processor/issues
|
141
|
+
rdoc_options: []
|
142
|
+
require_paths:
|
143
|
+
- lib
|
144
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
145
|
+
requirements:
|
146
|
+
- - ">="
|
147
|
+
- !ruby/object:Gem::Version
|
148
|
+
version: 2.6.0
|
149
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
150
|
+
requirements:
|
151
|
+
- - ">="
|
152
|
+
- !ruby/object:Gem::Version
|
153
|
+
version: '0'
|
154
|
+
requirements: []
|
155
|
+
rubygems_version: 3.6.8
|
156
|
+
specification_version: 4
|
157
|
+
summary: Sidekiq-based SQS message processing framework
|
158
|
+
test_files: []
|