Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add retryable #1

Merged
merged 4 commits into from
Nov 16, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
source "https://rubygems.org"

gem "rbs", git: "https://github.com/ruby/rbs.git", branch: "repo"
ybiquitous marked this conversation as resolved.
Show resolved Hide resolved
gem "steep", git: "https://github.com/soutaro/steep.git", branch: "update-rbs"
gem "rbs", git: "https://github.com/ruby/rbs.git", branch: "master"
gem "steep", git: "https://github.com/soutaro/steep.git", branch: "master"

gem "minitest"
20 changes: 10 additions & 10 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
GIT
remote: https://github.com/ruby/rbs.git
revision: 18635b273787ba0b8e0bd8f97ccb7a5de92b2c57
branch: repo
revision: 219bd9cce48706894ed0de9e82a94af93ce28846
branch: master
specs:
rbs (0.13.1)
rbs (0.16.0)

GIT
remote: https://github.com/soutaro/steep.git
revision: 26bec8810e0784ddd8eb89649be84e0642924441
branch: update-rbs
revision: 2928ed22d01ff89671d90876c4660499e5ed3f8c
branch: master
specs:
steep (0.33.0)
steep (0.34.0)
activesupport (>= 5.1)
ast_utils (~> 0.3.0)
language_server-protocol (~> 3.15.0.1)
listen (~> 3.0)
parser (~> 2.7.0)
rainbow (>= 2.2.2, < 4.0)
rbs (~> 0.13.0)
rbs (~> 0.16.0)

GEM
remote: https://rubygems.org/
Expand All @@ -37,7 +37,7 @@ GEM
i18n (1.8.5)
concurrent-ruby (~> 1.0)
language_server-protocol (3.15.0.1)
listen (3.2.1)
listen (3.3.0)
rb-fsevent (~> 0.10, >= 0.10.3)
rb-inotify (~> 0.9, >= 0.9.10)
minitest (5.14.2)
Expand All @@ -49,9 +49,9 @@ GEM
ffi (~> 1.0)
thor (1.0.1)
thread_safe (0.3.6)
tzinfo (1.2.7)
tzinfo (1.2.8)
thread_safe (~> 0.1)
zeitwerk (2.4.0)
zeitwerk (2.4.1)

PLATFORMS
ruby
Expand Down
9 changes: 9 additions & 0 deletions gems/retryable/3.0/_test/Steepfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
target :test do
check "."

repo_path "../../../"
library "retryable"
library "forwardable"

typing_options :strict
end
26 changes: 26 additions & 0 deletions gems/retryable/3.0/_test/test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require "retryable"

Retryable.retryable do
# ...
end

Retryable.configuration

Retryable.configure do |config|
config.contexts = {}
config.ensure = ->(retries) { puts "retries: #{retries}" }
config.exception_cb = ->(exception) { puts exception.message }
config.log_method = ->(retries, exception) { puts "retries: #{retries}, exception=#{exception.message}" }
config.matching = /foo/
config.matching = "bar"
config.matching = [/foo/, "bar"]
config.not = ArgumentError
config.not = [ArgumentError]
config.on = ArgumentError
config.on = [ArgumentError]
config.sleep = 1
config.sleep = 0.5
config.sleep = ->(retries) { retries ** 2 }
config.sleep_method = ->(n) { sleep(n.to_int + 1.5) }
config.tries = 3
end
64 changes: 64 additions & 0 deletions gems/retryable/3.0/configuration.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
module Retryable
# Used to set up and modify settings for the retryable.
class Configuration
type option_key = :contexts
| :ensure
| :exception_cb
| :log_method
| :matching
| :not
| :on
| :sleep
| :sleep_method
| :tries

type options = {
contexts: Hash[Symbol, options],
ensure: ^(Integer) -> void,
exception_cb: ^(Exception) -> void,
log_method: ^(Integer, Exception) -> void,
matching: (String | Regexp) | Array[String | Regexp],
not: _Exception | Array[_Exception],
on: _Exception | Array[_Exception],
sleep: real | (^(real) -> void),
sleep_method: ^(real) -> void,
tries: Integer
}

VALID_OPTION_KEYS: Array[option_key]

attr_accessor contexts: Hash[Symbol, options]
attr_accessor ensure: ^(Integer) -> void
attr_accessor exception_cb: ^(Exception) -> void
attr_accessor log_method: ^(Integer, Exception) -> void
attr_accessor matching: (String | Regexp) | Array[String | Regexp]
attr_accessor not: _Exception | Array[_Exception]
attr_accessor on: _Exception | Array[_Exception]
attr_accessor sleep: real | (^(real) -> void)
attr_accessor sleep_method: ^(real) -> void
attr_accessor tries: Integer

attr_accessor enabled: bool

def initialize: () -> void

def enable: () -> void

alias enabled? enabled

def disable: () -> void

# Allows config options to be read like a hash
#
# @param [Symbol] option Key for a given attribute
def []: (option_key option) -> untyped

# Returns a hash of all configurable options
def to_hash: () -> options

# Returns a hash of all configurable options merged with +hash+
#
# @param [Hash] hash A set of configuration options that will take precedence over the defaults
def merge: (options hash) -> options
end
end
45 changes: 45 additions & 0 deletions gems/retryable/3.0/retryable.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Runs a code block, and retries it when an exception occurs. It's great when working with flakey webservices (for example).
module Retryable
extend Forwardable

# A Retryable configuration object. Must act like a hash and return sensible
# values for all Retryable configuration options. See Retryable::Configuration.
attr_writer configuration: Configuration

# Call this method to modify defaults in your initializers.
#
# @example
# Retryable.configure do |config|
# config.contexts = {}
# config.ensure = proc {}
# config.exception_cb = proc {}
# config.log_method = proc {}
# config.matching = /.*/
# config.not = []
# config.on = StandardError
# config.sleep = 1
# config.sleep_method = ->(seconds) { Kernel.sleep(seconds) }
# config.tries = 2
# end
def self.configure: () { (Configuration) -> void } -> void

# The configuration object.
# @see Retryable.configure
def self.configuration: () -> Configuration

def self.enabled?: () -> bool
def self.enable: () -> void
def self.disable: () -> void

def self.with_context: [X] (Symbol context_key, ?Configuration::options options) { () -> X } -> X

alias self.retryable_with_context self.with_context

def self.retryable: [X] (?Configuration::options options) { (Integer, Exception) -> X } -> X?

private

def self.check_for_invalid_options: (Configuration::options custom_options, Configuration::options default_options) -> void

def self.matches?: (String message, Array[String | Regexp] candidates) -> bool
end
16 changes: 16 additions & 0 deletions gems/retryable/3.0/version.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Retryable
# This module holds the Retryable version information.
module Version
def major: () -> Integer

def minor: () -> Integer

def patch: () -> Integer

def to_h: () -> { major: Integer, minor: Integer, patch: Integer }

def to_a: () -> Array[Integer]

def to_s: () -> String
end
end