respawn 0.1.0 → 0.1.1

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: 5d53bd3ae13f4c1ef744cf22eff178445bc6dddd895a19aa6004edc1a3205903
4
- data.tar.gz: ba5ad886f2c32f2c77d55e661b9d8efcbff082fadbb5d3c5d27531fbe001443b
3
+ metadata.gz: 380a489e8801c8e378d2de4d06d3c21868751d79a0ff6b68981827c93214ee4a
4
+ data.tar.gz: a05ffb41acaa353de6df107acb2b1a5b1fd28e2789deecc11d7ebcb45f4181f4
5
5
  SHA512:
6
- metadata.gz: f30b3c3cac8dda00da0f15918dca3e129cb64dff3625200d3e0f044830ca7930d0a62fe4372f756a93cdceb9720e9452b1227e2161b77484a36a346e6e93de47
7
- data.tar.gz: fc4bc13b74bb108e659fead09d263ce3606aad8c8f16398180287921e2707be0870db94e0c1fa7f381108952ae7b17ca402baca2ac11a3f711fed1f4698c2a80
6
+ metadata.gz: ffdf8807fe188aebab1930b9eb244a87c16f46a8f833791630bd274828e8984fdc02ef0bc7b456292fdfca19f519aaefdff2544ffc1a082871ebca4fd2d2dc6e
7
+ data.tar.gz: 0a162ef250258b58b6ae2a317f42da649e98e0e2498938a0090a355413b34ef519e23bdf0b9c02434dc2b37a00a46feaecb73a2e6495e43a1df9cbf6d56776df
@@ -0,0 +1,119 @@
1
+ module Respawn
2
+ Environment = Data.define(:env) do
3
+ def test? = env == "test"
4
+ end
5
+
6
+ class Try
7
+ class Error < StandardError; end
8
+
9
+ COMMON_NETWORK_EXCEPTIONS = [
10
+ EOFError,
11
+ defined?(SocketError) && SocketError,
12
+ Errno::ECONNABORTED,
13
+ Errno::ECONNRESET,
14
+ Errno::EHOSTUNREACH,
15
+ # Faraday::ConnectionFailed,
16
+ # Faraday::TimeoutError,
17
+ # Faraday::ClientError,
18
+ # Faraday::ServerError,
19
+ # Net::OpenTimeout,
20
+ # Net::ReadTimeout,
21
+ # OpenSSL::SSL::SSLError,
22
+ # OpenURI::HTTPError,
23
+ ].compact.freeze
24
+
25
+ ONFAIL = [
26
+ :notify,
27
+ :nothing,
28
+ :raise,
29
+ :handler,
30
+ ].freeze
31
+
32
+ class Handler
33
+ attr_accessor :block
34
+
35
+ def define(&block)
36
+ self.block = block
37
+ end
38
+ end
39
+
40
+ class NullHandler
41
+ def define(&)
42
+ raise Error, "Cannot define a block unless onfail is :handler"
43
+ end
44
+ end
45
+
46
+ def self.call(*, **, &)
47
+ new(*, **).call(&)
48
+ end
49
+
50
+ def initialize(*exceptions, tries: 5, onfail: :notify, wait: 0.5, env: nil)
51
+ self.exceptions = parse_exceptions(exceptions)
52
+ self.tries = tries
53
+ self.onfail = ONFAIL.zip(ONFAIL).to_h.fetch(onfail)
54
+ self.wait = wait
55
+ self.handler = handler_for(onfail)
56
+ self.env = env || default_environment
57
+ end
58
+
59
+ def call
60
+ yield(handler)
61
+ rescue *exceptions => e
62
+ self.tries = tries - 1
63
+
64
+ if tries.positive?
65
+ Kernel.sleep(wait) unless env.test?
66
+ retry
67
+ end
68
+
69
+ perform_fail(e)
70
+ end
71
+
72
+ private
73
+
74
+ attr_accessor :exceptions, :tries, :onfail, :wait, :handler, :env
75
+
76
+ def default_environment
77
+ ENV.fetch("RUBY_ENV") do
78
+ ENV.fetch("RAILS_ENV") do
79
+ "production"
80
+ end
81
+ end
82
+ end
83
+
84
+ def handler_for(onfail)
85
+ if onfail == :handler
86
+ Handler.new
87
+ else
88
+ NullHandler.new
89
+ end
90
+ end
91
+
92
+ def perform_fail(exception)
93
+ case onfail
94
+ in :notify
95
+ # Sentry.capture_exception(exception)
96
+ in :nothing
97
+ nil
98
+ in :raise
99
+ raise
100
+ in :handler
101
+ handler.block.call(exception)
102
+ end
103
+ end
104
+
105
+ def parse_exceptions(list)
106
+ list.flat_map do |exception|
107
+ if exception == :network_errors
108
+ COMMON_NETWORK_EXCEPTIONS
109
+
110
+ # This comparision will raise an error if the exception is not
111
+ # a class, which is what we want.
112
+
113
+ elsif exception <= Exception
114
+ exception
115
+ end
116
+ end
117
+ end
118
+ end
119
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Respawn
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.1"
5
5
  end
data/lib/respawn.rb CHANGED
@@ -1,8 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "respawn/version"
3
+ require "zeitwerk"
4
4
 
5
5
  module Respawn
6
6
  class Error < StandardError; end
7
- # Your code goes here...
8
7
  end
8
+
9
+ loader = Zeitwerk::Loader.for_gem
10
+ loader.setup
11
+ loader.eager_load
metadata CHANGED
@@ -1,31 +1,55 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: respawn
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mariusz Drozdziel
8
- bindir: exe
8
+ bindir: bin
9
9
  cert_chain: []
10
10
  date: 1980-01-02 00:00:00.000000000 Z
11
- dependencies: []
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: rspec
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '3'
19
+ type: :development
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: '3'
26
+ - !ruby/object:Gem::Dependency
27
+ name: zeitwerk
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '2.6'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '2.6'
12
40
  email:
13
41
  - marzdrel@dotpro.org
14
42
  executables: []
15
43
  extensions: []
16
44
  extra_rdoc_files: []
17
45
  files:
18
- - LICENSE.txt
19
- - README.md
20
- - Rakefile
21
46
  - lib/respawn.rb
47
+ - lib/respawn/try.rb
22
48
  - lib/respawn/version.rb
23
- - sig/respawn.rbs
24
49
  homepage: https://github.com/marzdrel/respawn
25
50
  licenses:
26
51
  - MIT
27
- metadata:
28
- homepage_uri: https://github.com/marzdrel/respawn
52
+ metadata: {}
29
53
  rdoc_options: []
30
54
  require_paths:
31
55
  - lib
data/LICENSE.txt DELETED
@@ -1,21 +0,0 @@
1
- The MIT License (MIT)
2
-
3
- Copyright (c) 2025 John Doe
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
13
- all 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
21
- THE SOFTWARE.
data/README.md DELETED
@@ -1,43 +0,0 @@
1
- # Respawn
2
-
3
- TODO: Delete this and the text below, and describe your gem
4
-
5
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/respawn`. To experiment with that code, run `bin/console` for an interactive prompt.
6
-
7
- ## Installation
8
-
9
- TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
-
11
- Install the gem and add to the application's Gemfile by executing:
12
-
13
- ```bash
14
- bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
15
- ```
16
-
17
- If bundler is not being used to manage dependencies, install the gem by executing:
18
-
19
- ```bash
20
- gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
21
- ```
22
-
23
- ## Usage
24
-
25
- TODO: Write usage instructions here
26
-
27
- ## Development
28
-
29
- 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.
30
-
31
- 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
-
33
- ## Contributing
34
-
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/respawn. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/respawn/blob/main/CODE_OF_CONDUCT.md).
36
-
37
- ## License
38
-
39
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
-
41
- ## Code of Conduct
42
-
43
- Everyone interacting in the Respawn project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/respawn/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile DELETED
@@ -1,8 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "bundler/gem_tasks"
4
- require "rspec/core/rake_task"
5
-
6
- RSpec::Core::RakeTask.new(:spec)
7
-
8
- task default: :spec
data/sig/respawn.rbs DELETED
@@ -1,4 +0,0 @@
1
- module Respawn
2
- VERSION: String
3
- # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
- end