sqs_processor 0.1.2 → 0.1.3
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 +4 -4
- data/README.md +18 -1
- data/lib/sqs_processor/processor.rb +34 -0
- data/lib/sqs_processor/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c8c84fdb30ca8f133ccf09157614bd1aff65487f384a587abe975c2b883ca338
|
4
|
+
data.tar.gz: f9177b02b2b713e1acbf5ce9fac29f9a13c48eee8454b07778df81dd7472ca09
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c0cae0d423be0b4605ab032007df401017836617d020f0b98062b45d8a5d7cb09b89d87111a7217199bb2b20392a29d9ea57e7904b3b72bc616c30ad66d42fcd
|
7
|
+
data.tar.gz: 25fc1ea117532b69f30a34f71b03ca844bbb9ae9c6173c500e0b81ba943db085a76e0ae2425425b90bae5c9ab6dda878618bc3e6116f7ad336d2dd8635f072e8
|
data/README.md
CHANGED
@@ -9,7 +9,7 @@ A Ruby gem for processing messages from Amazon SQS queues with configurable mess
|
|
9
9
|
- **Error Handling**: Robust error handling with message retention on failure
|
10
10
|
- **Customizable**: Extensible message processing logic
|
11
11
|
- **Logging**: Comprehensive logging with configurable levels
|
12
|
-
- **
|
12
|
+
- **Graceful Shutdown**: Handles SIGTERM, SIGINT, and SIGQUIT signals gracefully
|
13
13
|
- **Environment Variables**: Support for AWS credentials and configuration via environment variables
|
14
14
|
|
15
15
|
## Prerequisites
|
@@ -196,6 +196,22 @@ The script supports multiple ways to provide AWS credentials:
|
|
196
196
|
- **Network Errors**: Automatic retry with exponential backoff
|
197
197
|
- **Queue Errors**: Comprehensive error logging with stack traces
|
198
198
|
|
199
|
+
## Graceful Shutdown
|
200
|
+
|
201
|
+
The processor handles termination signals gracefully:
|
202
|
+
|
203
|
+
- **SIGTERM**: Standard termination signal (used by container orchestrators)
|
204
|
+
- **SIGINT**: Interrupt signal (Ctrl+C)
|
205
|
+
- **SIGQUIT**: Quit signal
|
206
|
+
|
207
|
+
When a shutdown signal is received:
|
208
|
+
1. The processor stops accepting new messages
|
209
|
+
2. Completes processing of any current message batch
|
210
|
+
3. Logs the shutdown process
|
211
|
+
4. Exits cleanly
|
212
|
+
|
213
|
+
This ensures that no messages are lost during deployments or process termination.
|
214
|
+
|
199
215
|
## Monitoring
|
200
216
|
|
201
217
|
The script provides detailed logging including:
|
@@ -212,6 +228,7 @@ The script provides detailed logging including:
|
|
212
228
|
3. **Handle errors gracefully**: Return `false` from processing methods to keep messages in queue
|
213
229
|
4. **Monitor queue depth**: Use the built-in queue attribute reporting
|
214
230
|
5. **Use appropriate batch sizes**: Balance between throughput and memory usage
|
231
|
+
6. **Deploy with graceful shutdown**: The processor handles SIGTERM gracefully for container deployments
|
215
232
|
|
216
233
|
## Troubleshooting
|
217
234
|
|
@@ -16,9 +16,11 @@ module SQSProcessor
|
|
16
16
|
@queue_url = queue_url
|
17
17
|
@max_messages = max_messages
|
18
18
|
@visibility_timeout = visibility_timeout
|
19
|
+
@shutdown_requested = false
|
19
20
|
|
20
21
|
setup_logger(logger)
|
21
22
|
setup_sqs_client(aws_access_key_id, aws_secret_access_key, aws_session_token, aws_region)
|
23
|
+
setup_signal_handlers
|
22
24
|
|
23
25
|
raise 'Queue URL is required.' unless @queue_url
|
24
26
|
end
|
@@ -39,6 +41,23 @@ module SQSProcessor
|
|
39
41
|
)
|
40
42
|
end
|
41
43
|
|
44
|
+
def setup_signal_handlers
|
45
|
+
Signal.trap('TERM') do
|
46
|
+
logger.info 'Received SIGTERM, initiating graceful shutdown...'
|
47
|
+
@shutdown_requested = true
|
48
|
+
end
|
49
|
+
|
50
|
+
Signal.trap('INT') do
|
51
|
+
logger.info 'Received SIGINT, initiating graceful shutdown...'
|
52
|
+
@shutdown_requested = true
|
53
|
+
end
|
54
|
+
|
55
|
+
Signal.trap('QUIT') do
|
56
|
+
logger.info 'Received SIGQUIT, initiating graceful shutdown...'
|
57
|
+
@shutdown_requested = true
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
42
61
|
def process_messages
|
43
62
|
logger.info 'Starting SQS message processing...'
|
44
63
|
logger.info "Queue URL: #{@queue_url}"
|
@@ -46,6 +65,8 @@ module SQSProcessor
|
|
46
65
|
logger.info "Visibility timeout: #{@visibility_timeout} seconds"
|
47
66
|
|
48
67
|
loop do
|
68
|
+
break if @shutdown_requested
|
69
|
+
|
49
70
|
receive_messages
|
50
71
|
sleep 1 # Small delay between polling cycles
|
51
72
|
rescue StandardError => e
|
@@ -53,6 +74,8 @@ module SQSProcessor
|
|
53
74
|
logger.error e.backtrace.join("\n")
|
54
75
|
sleep 5 # Longer delay on error
|
55
76
|
end
|
77
|
+
|
78
|
+
logger.info 'Graceful shutdown completed.'
|
56
79
|
end
|
57
80
|
|
58
81
|
def receive_messages
|
@@ -71,6 +94,8 @@ module SQSProcessor
|
|
71
94
|
logger.info "Received #{response.messages.length} message(s)"
|
72
95
|
|
73
96
|
response.messages.each do |message|
|
97
|
+
break if @shutdown_requested
|
98
|
+
|
74
99
|
process_single_message(message)
|
75
100
|
end
|
76
101
|
end
|
@@ -134,5 +159,14 @@ module SQSProcessor
|
|
134
159
|
|
135
160
|
attributes
|
136
161
|
end
|
162
|
+
|
163
|
+
def shutdown_requested?
|
164
|
+
@shutdown_requested
|
165
|
+
end
|
166
|
+
|
167
|
+
def request_shutdown
|
168
|
+
logger.info 'Shutdown requested manually...'
|
169
|
+
@shutdown_requested = true
|
170
|
+
end
|
137
171
|
end
|
138
172
|
end
|