there_was_an_attempt 1.0.0 → 2.0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e525a2b1fb1e190e2fd35989b49077232c2e996b0b832a25509b6d818ed79e32
4
- data.tar.gz: 226466479816e907a096a653c81f3d6dd040d8baeabbf86f0f3bb6698ae69295
3
+ metadata.gz: 2703f0a8bc92d3f13e8419a4989a7cf3ab9ca20b7f5923bdbb3072459465e348
4
+ data.tar.gz: b6dc17d9d1476b699bfc3104eb6a21c6c42aed09fadd2b62672ac0d08ace1663
5
5
  SHA512:
6
- metadata.gz: f2709f0c45a7597f3abf26fa46b5a94ee853749fe9e38179e07bc9238bfeeb9245f8def95f1d2b72c101eea00ce654e580670efca2f0002c3bb516cab7914dff
7
- data.tar.gz: cf860e73837100965fe5cfa944c87f1eb39b561fe3ec158a70537621faf06ce25450e010fed6fd4b8f2c2e679f513a9294d18fe81ad05e442f3da53ad27f799f
6
+ metadata.gz: b6c89412698c889b751d3e4177d10e32b98cc2fa498ad782cbe77923af920b4ac3e75173e9ccf03ba52ba9683bf20b6e41bc597c85ff010a020a3e75200fb8f3
7
+ data.tar.gz: 7adbd39f2e23b1e5ea1191aa8acbdbb107600f4ad2475cdc2777cca305344591e5b82ca91c8d7abec5c5802d3c4a7519ac99b1a52b902e16f09e1373c4d7ff90
@@ -1,3 +1,10 @@
1
+ 2.0.0
2
+
3
+ * Added ability to specify a conditional retry, useful for only reattempting when its safe to do so.
4
+ * By default retries will always be attempted.
5
+ * Moved to keyword arguments for initialisation to support future features, this is a breaking change.
6
+ * Organised default arguments for future expansion.
7
+
1
8
  1.0.0
2
9
 
3
10
  * Initial release to support need for backoff in [Bellroy](https://bellroy.com/) projects.
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  ![There was an attempt](https://user-images.githubusercontent.com/2643026/78128526-ff02ec80-740d-11ea-9226-40b15c437518.png)
2
2
 
3
- # There was an attempt
3
+ # There was an attempt [![Gem Version](https://badge.fury.io/rb/there_was_an_attempt.svg)](https://badge.fury.io/rb/there_was_an_attempt) ![RSpec](https://github.com/samuelgiles/there_was_an_attempt/workflows/RSpec/badge.svg)
4
4
 
5
5
  A small utility designed to be used alongside [Dry::Monads::Result](https://dry-rb.org/gems/dry-monads/) to repeatedly attempt an operation sleeping between failed attempts.
6
6
 
@@ -22,7 +22,7 @@ Or install it yourself as:
22
22
 
23
23
  ## Usage
24
24
 
25
- Without any arguments given to the constructor a basic exponential backoff interval (2, 4, 8, 16 to be precise) is used with `sleep` being used to wait, as a nicety this is available as `.attempt`:
25
+ Without any arguments given to the constructor a basic backoff interval (2, 4, 8, 16 to be precise) is used with `sleep` being used to wait, as a nicety this is available as `.attempt`:
26
26
 
27
27
  ```ruby
28
28
  # Shortcut for: ThereWasAnAttempt.new.attempt
@@ -3,12 +3,22 @@
3
3
  require 'there_was_an_attempt/version'
4
4
 
5
5
  class ThereWasAnAttempt
6
- BACKOFF_INTERVALS_IN_SECONDS = [2, 4, 8, 16].freeze
7
- DEFAULT_WAIT = ->(seconds) { sleep(seconds) }.freeze
6
+ module Intervals
7
+ DEFAULT = [2, 4, 8, 16].freeze
8
+ end
9
+
10
+ module Wait
11
+ WITH_SLEEP = ->(seconds) { sleep(seconds) }.freeze
12
+ end
8
13
 
9
- def initialize(intervals = BACKOFF_INTERVALS_IN_SECONDS, wait = DEFAULT_WAIT)
14
+ module Reattempt
15
+ ALWAYS = -> (failure) { true }.freeze
16
+ end
17
+
18
+ def initialize(intervals: Intervals::DEFAULT, wait: Wait::WITH_SLEEP, reattempt: Reattempt::ALWAYS)
10
19
  @intervals = intervals
11
20
  @wait = wait
21
+ @reattempt = reattempt
12
22
  end
13
23
 
14
24
  def self.attempt(&block)
@@ -18,7 +28,7 @@ class ThereWasAnAttempt
18
28
  def attempt(&block)
19
29
  @intervals.map do |seconds|
20
30
  result = block.call
21
- break [result] if result.success?
31
+ break [result] if result.success? || !reattempt?(result.failure)
22
32
 
23
33
  result.or { |failure| wait(seconds); Dry::Monads::Failure(failure) }
24
34
  end.last
@@ -29,4 +39,8 @@ class ThereWasAnAttempt
29
39
  def wait(seconds)
30
40
  @wait.call(seconds)
31
41
  end
42
+
43
+ def reattempt?(failure)
44
+ @reattempt.call(failure)
45
+ end
32
46
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class ThereWasAnAttempt
4
- VERSION = '1.0.0'
4
+ VERSION = '2.0.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: there_was_an_attempt
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Giles
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-04-01 00:00:00.000000000 Z
11
+ date: 2020-08-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-monads
@@ -66,7 +66,7 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '3.0'
69
- description:
69
+ description:
70
70
  email:
71
71
  - sam@samuelgil.es
72
72
  executables: []
@@ -92,7 +92,7 @@ metadata:
92
92
  homepage_uri: https://github.com/samuelgiles/there_was_an_attempt
93
93
  source_code_uri: https://github.com/samuelgiles/there_was_an_attempt
94
94
  changelog_uri: https://github.com/samuelgiles/there_was_an_attempt/blob/master/CHANGELOG.md
95
- post_install_message:
95
+ post_install_message:
96
96
  rdoc_options: []
97
97
  require_paths:
98
98
  - lib
@@ -108,7 +108,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
108
108
  version: '0'
109
109
  requirements: []
110
110
  rubygems_version: 3.0.3
111
- signing_key:
111
+ signing_key:
112
112
  specification_version: 4
113
113
  summary: A small utility to repeatedly attempt an operation sleeping between failed
114
114
  attempts.