philiprehberger-retry_queue 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 3dce27c1ada757443f23b10abbc62c9530a7495f85fb4616c5002f3d51eb0e6e
4
+ data.tar.gz: 72374dcd0b0ddb9624098cbf194ac63ab29543e62888eed93d9e6b527d56d5b5
5
+ SHA512:
6
+ metadata.gz: 8c4a789d3cc288ebd9ddacf23c498edda4914d288fef3f7f9b824d2e24545315d8e512cbe2cdb64b4250882e2295e93328d2d2dfd0ce6814af2c149e64baf4b0
7
+ data.tar.gz: 6f13bcef9ff0d5d44dbf88fea0f8b36cde68baabe3816b8ccc535889f901e4cad117c10c49d5ee1660d857ab51c6b6a1bfd45c4a20bc4ecfddfda2217653d3ad
data/CHANGELOG.md ADDED
@@ -0,0 +1,18 @@
1
+ # Changelog
2
+
3
+ All notable changes to this gem will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [Unreleased]
9
+
10
+ ## [0.1.0] - 2026-03-22
11
+
12
+ ### Added
13
+ - Initial release
14
+ - Batch processing with per-item retry and configurable max retries
15
+ - Exponential backoff with custom backoff strategy support
16
+ - Dead letter collection for items that exhaust retries
17
+ - Result object with succeeded, failed, and stats accessors
18
+ - Timing statistics with elapsed duration and success rate
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 philiprehberger
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,90 @@
1
+ # philiprehberger-retry_queue
2
+
3
+ [![Tests](https://github.com/philiprehberger/rb-retry-queue/actions/workflows/ci.yml/badge.svg)](https://github.com/philiprehberger/rb-retry-queue/actions/workflows/ci.yml)
4
+ [![Gem Version](https://badge.fury.io/rb/philiprehberger-retry_queue.svg)](https://rubygems.org/gems/philiprehberger-retry_queue)
5
+ [![License](https://img.shields.io/github/license/philiprehberger/rb-retry-queue)](LICENSE)
6
+
7
+ Batch processor with per-item retry, backoff, and dead letter collection
8
+
9
+ ## Requirements
10
+
11
+ - Ruby >= 3.1
12
+
13
+ ## Installation
14
+
15
+ Add to your Gemfile:
16
+
17
+ ```ruby
18
+ gem "philiprehberger-retry_queue"
19
+ ```
20
+
21
+ Or install directly:
22
+
23
+ ```bash
24
+ gem install philiprehberger-retry_queue
25
+ ```
26
+
27
+ ## Usage
28
+
29
+ ```ruby
30
+ require "philiprehberger/retry_queue"
31
+
32
+ result = Philiprehberger::RetryQueue.process(items, max_retries: 3) do |item|
33
+ process_item(item)
34
+ end
35
+
36
+ puts result.succeeded.size # => number of successful items
37
+ puts result.failed.size # => number of failed items
38
+ ```
39
+
40
+ ### Custom Backoff
41
+
42
+ ```ruby
43
+ result = Philiprehberger::RetryQueue.process(items, max_retries: 5, backoff: ->(n) { n * 0.5 }) do |item|
44
+ external_api_call(item)
45
+ end
46
+ ```
47
+
48
+ ### Dead Letter Inspection
49
+
50
+ ```ruby
51
+ result = Philiprehberger::RetryQueue.process(jobs, max_retries: 2) do |job|
52
+ job.execute!
53
+ end
54
+
55
+ result.failed.each do |entry|
56
+ puts "Item: #{entry[:item]}, Error: #{entry[:error].message}, Attempts: #{entry[:attempts]}"
57
+ end
58
+ ```
59
+
60
+ ### Statistics
61
+
62
+ ```ruby
63
+ result = Philiprehberger::RetryQueue.process(records, max_retries: 3) do |record|
64
+ save(record)
65
+ end
66
+
67
+ stats = result.stats
68
+ # => { total: 100, succeeded: 97, failed: 3, success_rate: 0.97, elapsed: 1.23 }
69
+ ```
70
+
71
+ ## API
72
+
73
+ | Method | Description |
74
+ |--------|-------------|
75
+ | `.process(items, max_retries:, concurrency:, backoff:) { \|item\| }` | Process items with retry logic |
76
+ | `Result#succeeded` | Array of successfully processed items |
77
+ | `Result#failed` | Array of hashes with `:item`, `:error`, `:attempts` |
78
+ | `Result#stats` | Hash with `:total`, `:succeeded`, `:failed`, `:success_rate`, `:elapsed` |
79
+
80
+ ## Development
81
+
82
+ ```bash
83
+ bundle install
84
+ bundle exec rspec # Run tests
85
+ bundle exec rubocop # Check code style
86
+ ```
87
+
88
+ ## License
89
+
90
+ MIT
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Philiprehberger
4
+ module RetryQueue
5
+ # Processes items with per-item retry, backoff, and dead letter collection.
6
+ class Processor
7
+ # Default backoff strategy: exponential with base 0.1s.
8
+ DEFAULT_BACKOFF = ->(attempt) { 0.1 * (2**attempt) }
9
+
10
+ # @param max_retries [Integer] maximum retry attempts per item
11
+ # @param concurrency [Integer] number of concurrent workers (reserved for future use)
12
+ # @param backoff [Proc, nil] proc receiving attempt number, returns sleep duration
13
+ def initialize(max_retries: 3, concurrency: 1, backoff: nil)
14
+ raise Error, 'max_retries must be non-negative' unless max_retries.is_a?(Integer) && max_retries >= 0
15
+
16
+ @max_retries = max_retries
17
+ @concurrency = concurrency
18
+ @backoff = backoff || DEFAULT_BACKOFF
19
+ end
20
+
21
+ # Process a collection of items with retry logic.
22
+ #
23
+ # @param items [Array] items to process
24
+ # @yield [item] block that processes a single item; raise to signal failure
25
+ # @return [Result] processing result with succeeded, failed, and stats
26
+ def call(items, &block)
27
+ raise Error, 'a processing block is required' unless block
28
+
29
+ succeeded = []
30
+ failed = []
31
+ start_time = now
32
+
33
+ items.each do |item|
34
+ process_item(item, succeeded, failed, &block)
35
+ end
36
+
37
+ Result.new(succeeded: succeeded, failed: failed, elapsed: now - start_time)
38
+ end
39
+
40
+ private
41
+
42
+ def process_item(item, succeeded, failed, &block)
43
+ attempts = 0
44
+
45
+ loop do
46
+ attempts += 1
47
+ block.call(item)
48
+ succeeded << item
49
+ return
50
+ rescue StandardError => e
51
+ if attempts > @max_retries
52
+ failed << { item: item, error: e, attempts: attempts }
53
+ return
54
+ end
55
+
56
+ sleep_duration = @backoff.call(attempts - 1)
57
+ sleep(sleep_duration) if sleep_duration.positive?
58
+ end
59
+ end
60
+
61
+ def now
62
+ Process.clock_gettime(Process::CLOCK_MONOTONIC)
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Philiprehberger
4
+ module RetryQueue
5
+ # Holds the outcome of a batch processing run.
6
+ class Result
7
+ # @return [Array] items that were processed successfully
8
+ attr_reader :succeeded
9
+
10
+ # @return [Array<Hash>] items that exhausted retries, each with :item, :error, :attempts
11
+ attr_reader :failed
12
+
13
+ # @param succeeded [Array] successfully processed items
14
+ # @param failed [Array<Hash>] items that failed after all retries
15
+ # @param elapsed [Float] total processing time in seconds
16
+ def initialize(succeeded:, failed:, elapsed:)
17
+ @succeeded = succeeded
18
+ @failed = failed
19
+ @elapsed = elapsed
20
+ end
21
+
22
+ # Return processing statistics.
23
+ #
24
+ # @return [Hash] stats including counts, success rate, and elapsed time
25
+ def stats
26
+ total = @succeeded.size + @failed.size
27
+ {
28
+ total: total,
29
+ succeeded: @succeeded.size,
30
+ failed: @failed.size,
31
+ success_rate: total.zero? ? 0.0 : @succeeded.size.to_f / total,
32
+ elapsed: @elapsed
33
+ }
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Philiprehberger
4
+ module RetryQueue
5
+ VERSION = '0.1.0'
6
+ end
7
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'retry_queue/version'
4
+ require_relative 'retry_queue/result'
5
+ require_relative 'retry_queue/processor'
6
+
7
+ module Philiprehberger
8
+ module RetryQueue
9
+ class Error < StandardError; end
10
+
11
+ # Process items with per-item retry, backoff, and dead letter collection.
12
+ #
13
+ # @param items [Array] items to process
14
+ # @param max_retries [Integer] maximum retry attempts per item
15
+ # @param concurrency [Integer] number of concurrent workers
16
+ # @param backoff [Proc, nil] custom backoff strategy
17
+ # @yield [item] block that processes a single item
18
+ # @return [Result] processing result
19
+ def self.process(items, max_retries: 3, concurrency: 1, backoff: nil, &block)
20
+ processor = Processor.new(max_retries: max_retries, concurrency: concurrency, backoff: backoff)
21
+ processor.call(items, &block)
22
+ end
23
+ end
24
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: philiprehberger-retry_queue
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Philip Rehberger
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2026-03-22 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Processes collections of items with configurable per-item retry logic,
14
+ exponential backoff, and dead letter collection for failed items. Returns detailed
15
+ results with success/failure counts and timing statistics.
16
+ email:
17
+ - me@philiprehberger.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - CHANGELOG.md
23
+ - LICENSE
24
+ - README.md
25
+ - lib/philiprehberger/retry_queue.rb
26
+ - lib/philiprehberger/retry_queue/processor.rb
27
+ - lib/philiprehberger/retry_queue/result.rb
28
+ - lib/philiprehberger/retry_queue/version.rb
29
+ homepage: https://github.com/philiprehberger/rb-retry-queue
30
+ licenses:
31
+ - MIT
32
+ metadata:
33
+ homepage_uri: https://github.com/philiprehberger/rb-retry-queue
34
+ source_code_uri: https://github.com/philiprehberger/rb-retry-queue
35
+ changelog_uri: https://github.com/philiprehberger/rb-retry-queue/blob/main/CHANGELOG.md
36
+ bug_tracker_uri: https://github.com/philiprehberger/rb-retry-queue/issues
37
+ rubygems_mfa_required: 'true'
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 3.1.0
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubygems_version: 3.5.22
54
+ signing_key:
55
+ specification_version: 4
56
+ summary: Batch processor with per-item retry, backoff, and dead letter collection
57
+ test_files: []