sidekiq_backgrounder 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: '034091fb511933785bf520755a47ab8be12f4e3aeba2edb456f3af968ce6c29b'
4
+ data.tar.gz: b0fb911f79a14146738b407253e08cfad6ac3ef57fccb9d36180f3b61a4a3381
5
+ SHA512:
6
+ metadata.gz: 22ebfe46d4dbd4decd9a0abe27ba2d761ac3633f11a557ba15689ef8a58bc219c08e1b538f06c3ea5854c15a895b9aa07d02bb575231fda469a1c35a151a1c18
7
+ data.tar.gz: e96f76c547dd0a074d06502abb7d8e2df51ae14c001c17318a3ba9e9c5c076901624ee65a2704f2d01c3612c3205f3a1f781406fd0eb8bbc09bc10f9230cf741
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ sidekiq-backgrounder
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.2.2
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in backgrounder.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "minitest", "~> 5.0"
11
+ gem "awesome_print"
data/Gemfile.lock ADDED
@@ -0,0 +1,48 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ sidekiq_backgrounder (0.1.0)
5
+ error_extractor (~> 0.1.0)
6
+ globalid
7
+ sidekiq
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ activesupport (7.0.7.2)
13
+ concurrent-ruby (~> 1.0, >= 1.0.2)
14
+ i18n (>= 1.6, < 2)
15
+ minitest (>= 5.1)
16
+ tzinfo (~> 2.0)
17
+ awesome_print (1.9.2)
18
+ concurrent-ruby (1.2.2)
19
+ connection_pool (2.4.1)
20
+ error_extractor (0.1.0)
21
+ globalid (1.1.0)
22
+ activesupport (>= 5.0)
23
+ i18n (1.14.1)
24
+ concurrent-ruby (~> 1.0)
25
+ minitest (5.19.0)
26
+ rack (3.0.8)
27
+ rake (13.0.6)
28
+ redis-client (0.16.0)
29
+ connection_pool
30
+ sidekiq (7.1.2)
31
+ concurrent-ruby (< 2)
32
+ connection_pool (>= 2.3.0)
33
+ rack (>= 2.2.4)
34
+ redis-client (>= 0.14.0)
35
+ tzinfo (2.0.6)
36
+ concurrent-ruby (~> 1.0)
37
+
38
+ PLATFORMS
39
+ x86_64-darwin-20
40
+
41
+ DEPENDENCIES
42
+ awesome_print
43
+ minitest (~> 5.0)
44
+ rake (~> 13.0)
45
+ sidekiq_backgrounder!
46
+
47
+ BUNDLED WITH
48
+ 2.4.10
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Daniel P Zepeda
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 ADDED
@@ -0,0 +1,105 @@
1
+ # SidekiqBackgrounder
2
+
3
+ SidekiqBackgrounder is a simple utility that abstracts the creation of a background job into a single class. The
4
+ worker class takes a GlobalID string or class name, finds or instantiates the object as appropriate, then runs
5
+ a given method on the instance.
6
+
7
+ This approach is appropriate when your application has classes that have operations that should work in the background.
8
+ The advantage is that a class's background processing code can be included in the class, rather than spread out
9
+ over separately defined worker classes. This increases cohesiveness in the codebase. This approach also
10
+ cuts down on boilerplate code needed to define workers.
11
+
12
+ ## Installation
13
+
14
+ ### Bundler
15
+
16
+ gem "sidekiq_backgrounder", "~> 0.1.0"
17
+
18
+ ### Gem install
19
+
20
+ $ gem install sidekiq_backgrounder
21
+
22
+ ### Configuration
23
+
24
+ There is a configuration generator included in this gem for use in a rails project
25
+
26
+ $ rails generate sidekiq_backgrounder_initializer
27
+
28
+ If using outside of rails, configuration must be included manually:
29
+
30
+ ```
31
+ SidekiqBackgrounder.configure do |config|
32
+ config.logger = Rails.logger
33
+ config.queue = "default"
34
+ config.retry = false
35
+ config.backtrace = true
36
+ config.use_honeybadger = false
37
+ # config.pool = "some_pool_name"
38
+ end
39
+ ```
40
+
41
+ #### Configuration options
42
+
43
+ * `logger` - required. A logging object that responds to `:info` and `:debug`
44
+ * `queue` - Sidekiq queue option, required. A sidekiq queue name to queue a job to. Can be overridden in method call.
45
+ * `retry` - Sidekiq queue option, required. Whether to retry failed jobs by default. Can be overridden in method call.
46
+ * `backtrace` - Sidekiq queue option, required. Whether to provide a backtrace when a job fails. Can be overridden in method call.
47
+ * `pool` - Sidekiq queue option, optional. A sidekiq pool name to use to point a job to a particular redis shard.
48
+ Can be overridden in method call.
49
+ * `use_honeybadger` - required. If you use Honeybadger, enabling this option will force a Honeybadger notification.
50
+
51
+ ## Usage
52
+
53
+ SidekiqBackgrounder uses [GlobalID](https://github.com/rails/globalid) to reference models backed a by database table.
54
+ If you are not using ActiveRecord, take a look at the tests to see hints on how to set up a class to use GlobalID
55
+ manually. If the target instance is a plain ruby object, simply provide the class name.
56
+
57
+ This invocation will call the `run` method on the instance of the `Mock` class in a Sidekiq job:
58
+
59
+ SidekiqBackgrounder.queue.perform_async("gid://sidekiq-backgrounder/Mock/1", "run")
60
+
61
+ This invocation will call the `run` method on an instance of the `BareMock` class in a Sidekiq job:
62
+
63
+ SidekiqBackgrounder.queue.perform_async("BareMock", "run")
64
+
65
+ ### Sidekiq Queue Options
66
+
67
+ All of the default Sidekiq queue options can be overridden in the queue method, an example:
68
+
69
+ SidekiqBackgrounder.queue(queue: "priority1", pool: "us-east").perform_async("BareMock", "run")
70
+
71
+ ### Full method signature for perform
72
+
73
+ SidekiqBackgrounder.queue.perform_async(
74
+ "class name | GlobalID",
75
+ "method",
76
+ method_args = nil,
77
+ raise_exception = true,
78
+ exception_handler_method = nil
79
+ )
80
+
81
+ * `method_args` - Optionally pass arguments to the method. These must follow the rules of passing method arguments
82
+ to Sidekiq jobs.
83
+ * `raise_exception` - Use this option to force the run not to raise an exception.
84
+ * `exception_handler_method` - If provided, this method will be called on the object if an exception occurs instead
85
+ of the default actions for exceptions.
86
+
87
+ ## Testing/Debugging
88
+
89
+ In case you want to test/debug your invocation directly, call `new` directly on the worker class, skipping the call to
90
+ `queue`:
91
+
92
+ SidekiqBackgrounder::Worker.new.perform("Mock", "run")
93
+
94
+ ## Development
95
+
96
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests.
97
+ You can also run `bin/console` for an interactive prompt that will allow you to experiment.
98
+
99
+ ## Contributing
100
+
101
+ Bug reports and pull requests are welcome on GitHub at https://github.com/duskhacker/backgrounder.
102
+
103
+ ## License
104
+
105
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/*_test.rb"]
10
+ end
11
+
12
+ task default: :test
@@ -0,0 +1,15 @@
1
+ class SidekiqBackgrounderInitializerGenerator < Rails::Generators::Base
2
+ # :logger, :use_honeybadger, :queue, :retry, :backtrace, :pool, :raise_exception
3
+ def create_initializer_file
4
+ create_file "config/initializers/sidekiq_backgrounder.rb", <<~RUBY
5
+ SidekiqBackgrounder.configure do |config|
6
+ config.logger = Rails.logger
7
+ config.queue = "default"
8
+ config.retry = false
9
+ config.backtrace = true
10
+ config.use_honeybadger = false
11
+ # config.pool = "some_pool_name"
12
+ end
13
+ RUBY
14
+ end
15
+ end
@@ -0,0 +1,3 @@
1
+ module SidekiqBackgrounder
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,92 @@
1
+ require "sidekiq_backgrounder/version"
2
+ require "globalid"
3
+ require "sidekiq"
4
+ require "active_support"
5
+ require "error_extractor"
6
+
7
+ module SidekiqBackgrounder
8
+ class Error < StandardError; end
9
+
10
+ class Worker
11
+ include Sidekiq::Worker
12
+
13
+ attr_reader :object, :method, :gid_str, :exception_handler_method, :logger, :config
14
+
15
+ def initialize
16
+ @config = SidekiqBackgrounder.configuration
17
+ @logger = config.logger
18
+ end
19
+
20
+ def perform(identifier, method, method_args = nil, raise_exception = true, exception_handler_method = nil)
21
+ @object = if identifier.match(/gid:/)
22
+ GlobalID.new(identifier).find rescue nil
23
+ else
24
+ identifier.constantize.new
25
+ end
26
+ logger.debug("#{object.class}: found/instantiated object ")
27
+
28
+ if object.blank?
29
+ logger.error "#{identifier.inspect} not found to call #{method.to_s.inspect} on, exiting..."
30
+ return
31
+ end
32
+ @exception_handler_method = exception_handler_method
33
+
34
+ begin
35
+ logger.info "#{self.class}: running: #{object.class rescue object.inspect}##{method}, " +
36
+ "method_args: #{method_args.inspect}"
37
+ object.public_send(method, *method_args)
38
+ rescue Exception => e
39
+ if exception_handler_method.present?
40
+ object.public_send(exception_handler_method, e)
41
+ else
42
+ msg = "class: \"#{object.to_gid.to_str rescue object.class.to_s || "N/A"}\", method: \"#{method}\" " +
43
+ "method_args: #{method_args.inspect} encountered_error: #{extract_errors_with_backtrace(e)}"
44
+
45
+ if defined?(:Honeybadger) && config.use_honeybadger
46
+ Honeybadger.notify(
47
+ msg,
48
+ force: true,
49
+ fingerprint: Digest::SHA1.hexdigest(msg),
50
+ error_class: "SideKiqBackgrounder::Worker Error",
51
+ )
52
+ end
53
+
54
+ unless raise_exception
55
+ logger.error msg
56
+ return
57
+ end
58
+ raise SidekiqBackgrounder::Error.new(msg)
59
+ end
60
+ end
61
+ end
62
+ end
63
+
64
+ class << self
65
+ attr_accessor :configuration
66
+
67
+ def queue(options = {})
68
+ c = SidekiqBackgrounder.configuration
69
+ options.symbolize_keys!
70
+
71
+ opts = %i(queue retry backtrace).inject({}) do |m, opt|
72
+ m[opt] = c.public_send(opt)
73
+ m[opt] = options[opt] if options.has_key?(opt)
74
+ m
75
+ end
76
+
77
+ opts[:pool] = c.pool if c.pool.present?
78
+ opts[:pool] = options[:pool] if options.has_key?(:pool).present?
79
+
80
+ Worker.set(opts)
81
+ end
82
+
83
+ def configure
84
+ self.configuration ||= Configuration.new
85
+ yield(configuration)
86
+ end
87
+ end
88
+
89
+ class Configuration
90
+ attr_accessor :logger, :use_honeybadger, :queue, :retry, :backtrace, :pool
91
+ end
92
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/sidekiq_backgrounder/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "sidekiq_backgrounder"
7
+ spec.version = SidekiqBackgrounder::VERSION
8
+ spec.authors = ["Daniel P Zepeda"]
9
+ spec.email = ["daniel@zepeda.ws"]
10
+
11
+ spec.summary = "Simple Abstraction for executing background jobs in Sidekiq"
12
+ spec.description = "Simple Abstraction for executing background jobs in Sidekiq"
13
+ spec.homepage = "https://github.com/duskhacker/sidekiq-backgrounder"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.6.0"
16
+
17
+ # spec.metadata["allowed_push_host"] = "TODO: Set to your gem server 'https://example.com'"
18
+
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ spec.metadata["source_code_uri"] = spec.homepage
21
+
22
+ # Specify which files should be added to the gem when it is released.
23
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
+ spec.files = Dir.chdir(__dir__) do
25
+ `git ls-files -z`.split("\x0").reject do |f|
26
+ (File.expand_path(f) == __FILE__) || f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor])
27
+ end
28
+ end
29
+ spec.require_paths = ["lib"]
30
+
31
+ spec.add_dependency "sidekiq"
32
+ spec.add_dependency "error_extractor", "~> 0.1.0"
33
+ spec.add_dependency "globalid"
34
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sidekiq_backgrounder
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Daniel P Zepeda
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-08-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sidekiq
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: error_extractor
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.1.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.1.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: globalid
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Simple Abstraction for executing background jobs in Sidekiq
56
+ email:
57
+ - daniel@zepeda.ws
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".ruby-gemset"
63
+ - ".ruby-version"
64
+ - Gemfile
65
+ - Gemfile.lock
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - lib/generators/sidekiq_backgrounder_initializer_generator.rb
70
+ - lib/sidekiq_backgrounder.rb
71
+ - lib/sidekiq_backgrounder/version.rb
72
+ - sidekiq_backgrounder.gemspec
73
+ homepage: https://github.com/duskhacker/sidekiq-backgrounder
74
+ licenses:
75
+ - MIT
76
+ metadata:
77
+ homepage_uri: https://github.com/duskhacker/sidekiq-backgrounder
78
+ source_code_uri: https://github.com/duskhacker/sidekiq-backgrounder
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: 2.6.0
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubygems_version: 3.0.3.1
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Simple Abstraction for executing background jobs in Sidekiq
98
+ test_files: []