sqs_processor 0.1.2 → 0.1.4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b236be094b122ba859d742bff3fb3973e70cc9328dcd14244517ac09c4adec45
4
- data.tar.gz: 32d709d43420508be56b438e6ae2958efd6ad5aeef23e1a4408fd296a87e0bb0
3
+ metadata.gz: 64697b1de1623aae4f565a5fb479f7821a93929cae7962814481e81008ba8006
4
+ data.tar.gz: 9e0f1bee31f3f0925db9cbb4e98c053742f6e4cc658b9008e71ce72230ce1d87
5
5
  SHA512:
6
- metadata.gz: bc53c5db71dea861a64b1e282e7466ff9378fc664c0fa3bbe0c161189211f31d3ef925b1da122e4d1a29b8c4cb3fcdcc29acefc71ddc9342002cc6166ad22ba4
7
- data.tar.gz: 208bb5ac50214e931dc0935a13d01cdac120460ad1c80a10a8fb060890a86cb07ad353d9730eff40f25d8724bb9d75db598a2564ae342de24837b4afbc6b4dac
6
+ metadata.gz: a02c92703472e7f58d44009e02b0a992211eb4f456c2ff274f58a7a7c4affae6b082d38143291d8838a4ecd81373a15787185d51e4882e39decd160acd2d75c8
7
+ data.tar.gz: 26b45ce1da7ec1fb3de5b99274d2d964c01b3deaf5a47efad7e27d97058b0c0d3522d09bbcb1e0aa3052126173e33a4a11dc03f8ecf068fa92f71460360f6d5c
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
- - **Command Line Options**: Flexible configuration via command line arguments
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,23 @@ 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 immediately interrupts any blocking operations (like SQS polling)
209
+ 2. Stops accepting new messages
210
+ 3. Completes processing of any current message batch
211
+ 4. Logs the shutdown process
212
+ 5. Exits cleanly
213
+
214
+ **Immediate Response**: Like Puma, the processor responds to shutdown signals immediately, interrupting any blocking operations without waiting for timeouts.
215
+
199
216
  ## Monitoring
200
217
 
201
218
  The script provides detailed logging including:
@@ -212,6 +229,7 @@ The script provides detailed logging including:
212
229
  3. **Handle errors gracefully**: Return `false` from processing methods to keep messages in queue
213
230
  4. **Monitor queue depth**: Use the built-in queue attribute reporting
214
231
  5. **Use appropriate batch sizes**: Balance between throughput and memory usage
232
+ 6. **Deploy with graceful shutdown**: The processor handles SIGTERM gracefully for container deployments
215
233
 
216
234
  ## Troubleshooting
217
235
 
@@ -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,29 @@ 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
+ # Immediately interrupt any blocking operations
49
+ Thread.main.raise Interrupt.new
50
+ end
51
+
52
+ Signal.trap('INT') do
53
+ logger.info 'Received SIGINT, initiating graceful shutdown...'
54
+ @shutdown_requested = true
55
+ # Immediately interrupt any blocking operations
56
+ Thread.main.raise Interrupt.new
57
+ end
58
+
59
+ Signal.trap('QUIT') do
60
+ logger.info 'Received SIGQUIT, initiating graceful shutdown...'
61
+ @shutdown_requested = true
62
+ # Immediately interrupt any blocking operations
63
+ Thread.main.raise Interrupt.new
64
+ end
65
+ end
66
+
42
67
  def process_messages
43
68
  logger.info 'Starting SQS message processing...'
44
69
  logger.info "Queue URL: #{@queue_url}"
@@ -46,13 +71,21 @@ module SQSProcessor
46
71
  logger.info "Visibility timeout: #{@visibility_timeout} seconds"
47
72
 
48
73
  loop do
74
+ break if @shutdown_requested
75
+
49
76
  receive_messages
50
77
  sleep 1 # Small delay between polling cycles
78
+ rescue Interrupt
79
+ logger.info 'Interrupted, initiating graceful shutdown...'
80
+ @shutdown_requested = true
81
+ break
51
82
  rescue StandardError => e
52
83
  logger.error "Error in message processing loop: #{e.message}"
53
84
  logger.error e.backtrace.join("\n")
54
85
  sleep 5 # Longer delay on error
55
86
  end
87
+
88
+ logger.info 'Graceful shutdown completed.'
56
89
  end
57
90
 
58
91
  def receive_messages
@@ -71,8 +104,13 @@ module SQSProcessor
71
104
  logger.info "Received #{response.messages.length} message(s)"
72
105
 
73
106
  response.messages.each do |message|
107
+ break if @shutdown_requested
108
+
74
109
  process_single_message(message)
75
110
  end
111
+ rescue Interrupt
112
+ logger.info 'Interrupted during message reception...'
113
+ raise
76
114
  end
77
115
 
78
116
  def process_single_message(message)
@@ -134,5 +172,14 @@ module SQSProcessor
134
172
 
135
173
  attributes
136
174
  end
175
+
176
+ def shutdown_requested?
177
+ @shutdown_requested
178
+ end
179
+
180
+ def request_shutdown
181
+ logger.info 'Shutdown requested manually...'
182
+ @shutdown_requested = true
183
+ end
137
184
  end
138
185
  end
@@ -1,3 +1,3 @@
1
1
  module SQSProcessor
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.4"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sqs_processor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Unni Tallman