there_was_an_attempt 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e525a2b1fb1e190e2fd35989b49077232c2e996b0b832a25509b6d818ed79e32
4
+ data.tar.gz: 226466479816e907a096a653c81f3d6dd040d8baeabbf86f0f3bb6698ae69295
5
+ SHA512:
6
+ metadata.gz: f2709f0c45a7597f3abf26fa46b5a94ee853749fe9e38179e07bc9238bfeeb9245f8def95f1d2b72c101eea00ce654e580670efca2f0002c3bb516cab7914dff
7
+ data.tar.gz: cf860e73837100965fe5cfa944c87f1eb39b561fe3ec158a70537621faf06ce25450e010fed6fd4b8f2c2e679f513a9294d18fe81ad05e442f3da53ad27f799f
@@ -0,0 +1,35 @@
1
+ ---
2
+ name: RSpec
3
+ on: push
4
+ jobs:
5
+ build:
6
+ runs-on: ubuntu-18.04
7
+ steps:
8
+ - name: Checkout branch
9
+ uses: actions/checkout@v1
10
+ - name: Cache Ruby Install
11
+ uses: actions/cache@v1
12
+ id: ruby-install-cache
13
+ with:
14
+ path: "~/local/rubies"
15
+ key: ruby-2.6.5
16
+ - name: Install Ruby
17
+ uses: clupprich/ruby-build-action@master
18
+ id: ruby
19
+ with:
20
+ ruby-version: 2.6.5
21
+ cache-available: "${{ steps.ruby-install-cache.outputs.cache-hit == 'true'
22
+ }}"
23
+ - name: Cache gems
24
+ uses: actions/cache@v1
25
+ with:
26
+ path: vendor/bundle
27
+ key: "${{ runner.OS }}-gem-cache-${{ hashFiles('**/there_was_an_attempt.gemspec')
28
+ }}"
29
+ restore-keys: "${{ runner.OS }}-gem-cache-\n"
30
+ - name: Install bundler
31
+ run: (bundler -v | grep "2.0.2") || gem install bundler:2.0.2
32
+ - name: Install gems
33
+ run: bundle install --jobs $(nproc) --retry 3 --without metrics --path vendor/bundle
34
+ - name: Run RSpec test suite
35
+ run: bundle exec rspec spec
data/.gitignore ADDED
@@ -0,0 +1,20 @@
1
+ /Gemfile.lock
2
+ /.bundle/
3
+ /.yardoc
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
13
+
14
+ /shell.nix
15
+ /tags
16
+
17
+ *.log
18
+
19
+ .ruby-gemset
20
+ /lib/.DS_Store
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.6.5
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ 1.0.0
2
+
3
+ * Initial release to support need for backoff in [Bellroy](https://bellroy.com/) projects.
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in there_was_an_attempt.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,58 @@
1
+ ![There was an attempt](https://user-images.githubusercontent.com/2643026/78128526-ff02ec80-740d-11ea-9226-40b15c437518.png)
2
+
3
+ # There was an attempt
4
+
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
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'there_was_an_attempt'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install there_was_an_attempt
22
+
23
+ ## Usage
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`:
26
+
27
+ ```ruby
28
+ # Shortcut for: ThereWasAnAttempt.new.attempt
29
+ ThereWasAnAttempt.attempt do
30
+ Dry::Monads::Success(true)
31
+ end
32
+ ```
33
+
34
+ You can specify your backoff intervals manually:
35
+
36
+ ```ruby
37
+ ThereWasAnAttempt.new([1,2,4,8]).attempt do
38
+ Dry::Monads::Success(true)
39
+ end
40
+ ```
41
+
42
+ And you can also optionally specify your own "wait" logic:
43
+
44
+ ```ruby
45
+ ThereWasAnAttempt.new([1,2,4,8], ->(seconds) { puts seconds; sleep seconds }).attempt do
46
+ Dry::Monads::Success(true)
47
+ end
48
+ ```
49
+
50
+ ## Development
51
+
52
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
53
+
54
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
55
+
56
+ ## Contributing
57
+
58
+ Bug reports and pull requests are welcome on GitHub at https://github.com/samuelgiles/there_was_an_attempt.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'there_was_an_attempt'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require 'pry'
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ThereWasAnAttempt
4
+ VERSION = '1.0.0'
5
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'there_was_an_attempt/version'
4
+
5
+ class ThereWasAnAttempt
6
+ BACKOFF_INTERVALS_IN_SECONDS = [2, 4, 8, 16].freeze
7
+ DEFAULT_WAIT = ->(seconds) { sleep(seconds) }.freeze
8
+
9
+ def initialize(intervals = BACKOFF_INTERVALS_IN_SECONDS, wait = DEFAULT_WAIT)
10
+ @intervals = intervals
11
+ @wait = wait
12
+ end
13
+
14
+ def self.attempt(&block)
15
+ new.attempt(&block)
16
+ end
17
+
18
+ def attempt(&block)
19
+ @intervals.map do |seconds|
20
+ result = block.call
21
+ break [result] if result.success?
22
+
23
+ result.or { |failure| wait(seconds); Dry::Monads::Failure(failure) }
24
+ end.last
25
+ end
26
+
27
+ private
28
+
29
+ def wait(seconds)
30
+ @wait.call(seconds)
31
+ end
32
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'there_was_an_attempt/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'there_was_an_attempt'
9
+ spec.version = ThereWasAnAttempt::VERSION
10
+ spec.authors = ['Samuel Giles']
11
+ spec.email = ['sam@samuelgil.es']
12
+
13
+ spec.summary = 'A small utility to repeatedly attempt an operation sleeping between failed attempts.'
14
+ spec.homepage = "https://github.com/samuelgiles/there_was_an_attempt"
15
+
16
+ spec.metadata['homepage_uri'] = spec.homepage
17
+ spec.metadata['source_code_uri'] = spec.homepage
18
+ spec.metadata['changelog_uri'] = "#{spec.homepage}/blob/master/CHANGELOG.md"
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
23
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
24
+ end
25
+ spec.require_paths = ['lib']
26
+
27
+ spec.add_dependency 'dry-monads'
28
+
29
+ spec.add_development_dependency 'bundler'
30
+ spec.add_development_dependency 'rake'
31
+ spec.add_development_dependency 'rspec', '~> 3.0'
32
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: there_was_an_attempt
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Samuel Giles
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-04-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: dry-monads
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description:
70
+ email:
71
+ - sam@samuelgil.es
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".github/workflows/rspec.yml"
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".ruby-version"
80
+ - CHANGELOG.md
81
+ - Gemfile
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/setup
86
+ - lib/there_was_an_attempt.rb
87
+ - lib/there_was_an_attempt/version.rb
88
+ - there_was_an_attempt.gemspec
89
+ homepage: https://github.com/samuelgiles/there_was_an_attempt
90
+ licenses: []
91
+ metadata:
92
+ homepage_uri: https://github.com/samuelgiles/there_was_an_attempt
93
+ source_code_uri: https://github.com/samuelgiles/there_was_an_attempt
94
+ changelog_uri: https://github.com/samuelgiles/there_was_an_attempt/blob/master/CHANGELOG.md
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubygems_version: 3.0.3
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: A small utility to repeatedly attempt an operation sleeping between failed
114
+ attempts.
115
+ test_files: []