├── .coditsu └── ci.yml ├── .diffend.yml ├── .github └── workflows │ └── ci.yml ├── .gitignore ├── .rspec ├── .ruby-gemset ├── .ruby-version ├── CHANGELOG.md ├── Gemfile ├── Gemfile.lock ├── LICENSE ├── README.md ├── certs └── mensfeld.pem ├── lib ├── worker_glass.rb └── worker_glass │ ├── errors.rb │ ├── reentrancy.rb │ ├── timeout.rb │ └── version.rb ├── spec ├── lib │ ├── worker_glass │ │ ├── errors_spec.rb │ │ ├── reentrancy_spec.rb │ │ ├── timeout_spec.rb │ │ └── version_spec.rb │ └── worker_glass_spec.rb ├── spec_helper.rb └── support │ └── class_builder.rb └── worker-glass.gemspec /.coditsu/ci.yml: -------------------------------------------------------------------------------- 1 | repository_id: 'fe64252b-4b9f-42e6-bb71-c07ac97cabef' 2 | api_key: <%= ENV['CODITSU_API_KEY'] %> 3 | api_secret: <%= ENV['CODITSU_API_SECRET'] %> 4 | -------------------------------------------------------------------------------- /.diffend.yml: -------------------------------------------------------------------------------- 1 | project_id: '00715716-48ee-4b00-87af-894df2dc9d3c' 2 | shareable_id: 'c278c628-7369-4188-b8c9-d7051465c8e5' 3 | shareable_key: '78cd49de-52fd-4a6e-b061-0a07fd1b2b47' 4 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: ci 2 | 3 | on: 4 | pull_request: 5 | push: 6 | schedule: 7 | - cron: '0 1 * * *' 8 | 9 | jobs: 10 | specs: 11 | runs-on: ubuntu-latest 12 | needs: diffend 13 | strategy: 14 | fail-fast: false 15 | matrix: 16 | ruby: 17 | - '3.0' 18 | - '2.7' 19 | - '2.6' 20 | - '2.5' 21 | - 'jruby' 22 | - 'truffleruby' 23 | include: 24 | - ruby: '3.0' 25 | coverage: 'true' 26 | steps: 27 | - uses: actions/checkout@v2 28 | - name: Install package dependencies 29 | run: "[ -e $APT_DEPS ] || sudo apt-get install -y --no-install-recommends $APT_DEPS" 30 | - name: Set up Ruby 31 | uses: ruby/setup-ruby@v1 32 | with: 33 | ruby-version: ${{matrix.ruby}} 34 | - name: Install latest bundler 35 | run: | 36 | gem install bundler --no-document 37 | bundle config set without 'tools benchmarks docs' 38 | - name: Bundle install 39 | run: bundle install --jobs 4 --retry 3 40 | - name: Run all tests 41 | env: 42 | GITHUB_COVERAGE: ${{matrix.coverage}} 43 | run: bundle exec rspec 44 | 45 | diffend: 46 | runs-on: ubuntu-latest 47 | strategy: 48 | fail-fast: false 49 | steps: 50 | - uses: actions/checkout@v2 51 | with: 52 | fetch-depth: 0 53 | - name: Set up Ruby 54 | uses: ruby/setup-ruby@v1 55 | with: 56 | ruby-version: 3.0 57 | - name: Install latest bundler 58 | run: gem install bundler --no-document 59 | - name: Install Diffend plugin 60 | run: bundle plugin install diffend 61 | - name: Bundle Secure 62 | run: bundle secure 63 | 64 | coditsu: 65 | runs-on: ubuntu-latest 66 | strategy: 67 | fail-fast: false 68 | steps: 69 | - uses: actions/checkout@v2 70 | with: 71 | fetch-depth: 0 72 | - name: Run Coditsu 73 | run: \curl -sSL https://api.coditsu.io/run/ci | bash 74 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.gem 2 | *.rbc 3 | .bundle 4 | .config 5 | .idea 6 | .yardoc 7 | .coditsu/local.yml 8 | InstalledFiles 9 | _yardoc 10 | coverage 11 | doc/ 12 | lib/bundler/man 13 | pkg 14 | rdoc 15 | spec/reports 16 | test/tmp 17 | test/version_tmp 18 | tmp 19 | *.bundle 20 | *.so 21 | *.o 22 | *.a 23 | mkmf.log 24 | -------------------------------------------------------------------------------- /.rspec: -------------------------------------------------------------------------------- 1 | --require spec_helper 2 | -------------------------------------------------------------------------------- /.ruby-gemset: -------------------------------------------------------------------------------- 1 | worker_glass 2 | -------------------------------------------------------------------------------- /.ruby-version: -------------------------------------------------------------------------------- 1 | 3.0.0 2 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # WorkerGlass changelog 2 | 3 | ## 0.2.5 (2020-04-24) 4 | - drop ruby 2.2 support 5 | - drop ruby 2.3 support 6 | - drop ruby 2.4 support 7 | - Ruby 2.5.5 support 8 | - Ruby 2.6.5 support 9 | - Ruby 2.7.1 support 10 | - Removed rake as one of dependencies 11 | - Replace Travis with GH Actions 12 | - Add certificates support 13 | - Add JRuby support 14 | - Add TruffleRuby support 15 | - Change license to LGPL-3.0 16 | 17 | ## 0.2.3 18 | - Gem bump 19 | - Ruby 2.4.1 as default 20 | - Coditsu quality hookup 21 | - Readme updates 22 | 23 | ## 0.2.2 24 | - Gem dump x2 25 | - Ruby 2.1.* support dropped 26 | - ~~Ruby 2.3.3 as default~~ 27 | - Ruby 2.4.0 as default 28 | - Ruby 2.2 as minimal version 29 | - Added .rspec for default spec helper require 30 | 31 | ## 0.2.0 32 | - Renamed from SidekiqGlass to WorkerGlass because if now does not require Sidekiq anymore to work. It will provide additional worker functionalities to any type of backend processing layer as long as it has a #perform method. 33 | - Switched to Mongoid like feature providing by including/prepending functionalities 34 | - WorkerGlass no longer required 35 | - WorkerGlass::Timeout 36 | - WorkerGlass::Reentrancy 37 | - Dev Tools and gems update 38 | 39 | ## 0.1.4 40 | 41 | - New Sidekiq (4.0.1) version dump 42 | 43 | ## 0.1.3 44 | 45 | - Added null-logger gem 46 | 47 | ## 0.1.2 48 | 49 | - SidekiqGlass logger implementation 50 | 51 | ## 0.1.1 52 | 53 | - Celluloid lock due to some Celluloid internal issues 54 | 55 | ## 0.1.0 56 | 57 | - Initial released version 58 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | source 'https://rubygems.org' 4 | 5 | plugin 'diffend' 6 | 7 | gemspec 8 | 9 | group :test do 10 | gem 'rspec' 11 | gem 'simplecov' 12 | end 13 | -------------------------------------------------------------------------------- /Gemfile.lock: -------------------------------------------------------------------------------- 1 | PATH 2 | remote: . 3 | specs: 4 | worker-glass (0.2.5) 5 | activesupport 6 | null-logger 7 | 8 | GEM 9 | remote: https://rubygems.org/ 10 | specs: 11 | activesupport (6.1.3.1) 12 | concurrent-ruby (~> 1.0, >= 1.0.2) 13 | i18n (>= 1.6, < 2) 14 | minitest (>= 5.1) 15 | tzinfo (~> 2.0) 16 | zeitwerk (~> 2.3) 17 | concurrent-ruby (1.1.8) 18 | diff-lcs (1.4.4) 19 | docile (1.3.5) 20 | i18n (1.8.10) 21 | concurrent-ruby (~> 1.0) 22 | minitest (5.14.4) 23 | null-logger (0.1.7) 24 | rspec (3.10.0) 25 | rspec-core (~> 3.10.0) 26 | rspec-expectations (~> 3.10.0) 27 | rspec-mocks (~> 3.10.0) 28 | rspec-core (3.10.1) 29 | rspec-support (~> 3.10.0) 30 | rspec-expectations (3.10.1) 31 | diff-lcs (>= 1.2.0, < 2.0) 32 | rspec-support (~> 3.10.0) 33 | rspec-mocks (3.10.2) 34 | diff-lcs (>= 1.2.0, < 2.0) 35 | rspec-support (~> 3.10.0) 36 | rspec-support (3.10.2) 37 | simplecov (0.21.2) 38 | docile (~> 1.1) 39 | simplecov-html (~> 0.11) 40 | simplecov_json_formatter (~> 0.1) 41 | simplecov-html (0.12.3) 42 | simplecov_json_formatter (0.1.2) 43 | tzinfo (2.0.4) 44 | concurrent-ruby (~> 1.0) 45 | zeitwerk (2.4.2) 46 | 47 | PLATFORMS 48 | x86_64-darwin-19 49 | x86_64-linux 50 | 51 | DEPENDENCIES 52 | rspec 53 | simplecov 54 | worker-glass! 55 | 56 | BUNDLED WITH 57 | 2.2.15 58 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | 9 | This version of the GNU Lesser General Public License incorporates 10 | the terms and conditions of version 3 of the GNU General Public 11 | License, supplemented by the additional permissions listed below. 12 | 13 | 0. Additional Definitions. 14 | 15 | As used herein, "this License" refers to version 3 of the GNU Lesser 16 | General Public License, and the "GNU GPL" refers to version 3 of the GNU 17 | General Public License. 18 | 19 | "The Library" refers to a covered work governed by this License, 20 | other than an Application or a Combined Work as defined below. 21 | 22 | An "Application" is any work that makes use of an interface provided 23 | by the Library, but which is not otherwise based on the Library. 24 | Defining a subclass of a class defined by the Library is deemed a mode 25 | of using an interface provided by the Library. 26 | 27 | A "Combined Work" is a work produced by combining or linking an 28 | Application with the Library. The particular version of the Library 29 | with which the Combined Work was made is also called the "Linked 30 | Version". 31 | 32 | The "Minimal Corresponding Source" for a Combined Work means the 33 | Corresponding Source for the Combined Work, excluding any source code 34 | for portions of the Combined Work that, considered in isolation, are 35 | based on the Application, and not on the Linked Version. 36 | 37 | The "Corresponding Application Code" for a Combined Work means the 38 | object code and/or source code for the Application, including any data 39 | and utility programs needed for reproducing the Combined Work from the 40 | Application, but excluding the System Libraries of the Combined Work. 41 | 42 | 1. Exception to Section 3 of the GNU GPL. 43 | 44 | You may convey a covered work under sections 3 and 4 of this License 45 | without being bound by section 3 of the GNU GPL. 46 | 47 | 2. Conveying Modified Versions. 48 | 49 | If you modify a copy of the Library, and, in your modifications, a 50 | facility refers to a function or data to be supplied by an Application 51 | that uses the facility (other than as an argument passed when the 52 | facility is invoked), then you may convey a copy of the modified 53 | version: 54 | 55 | a) under this License, provided that you make a good faith effort to 56 | ensure that, in the event an Application does not supply the 57 | function or data, the facility still operates, and performs 58 | whatever part of its purpose remains meaningful, or 59 | 60 | b) under the GNU GPL, with none of the additional permissions of 61 | this License applicable to that copy. 62 | 63 | 3. Object Code Incorporating Material from Library Header Files. 64 | 65 | The object code form of an Application may incorporate material from 66 | a header file that is part of the Library. You may convey such object 67 | code under terms of your choice, provided that, if the incorporated 68 | material is not limited to numerical parameters, data structure 69 | layouts and accessors, or small macros, inline functions and templates 70 | (ten or fewer lines in length), you do both of the following: 71 | 72 | a) Give prominent notice with each copy of the object code that the 73 | Library is used in it and that the Library and its use are 74 | covered by this License. 75 | 76 | b) Accompany the object code with a copy of the GNU GPL and this license 77 | document. 78 | 79 | 4. Combined Works. 80 | 81 | You may convey a Combined Work under terms of your choice that, 82 | taken together, effectively do not restrict modification of the 83 | portions of the Library contained in the Combined Work and reverse 84 | engineering for debugging such modifications, if you also do each of 85 | the following: 86 | 87 | a) Give prominent notice with each copy of the Combined Work that 88 | the Library is used in it and that the Library and its use are 89 | covered by this License. 90 | 91 | b) Accompany the Combined Work with a copy of the GNU GPL and this license 92 | document. 93 | 94 | c) For a Combined Work that displays copyright notices during 95 | execution, include the copyright notice for the Library among 96 | these notices, as well as a reference directing the user to the 97 | copies of the GNU GPL and this license document. 98 | 99 | d) Do one of the following: 100 | 101 | 0) Convey the Minimal Corresponding Source under the terms of this 102 | License, and the Corresponding Application Code in a form 103 | suitable for, and under terms that permit, the user to 104 | recombine or relink the Application with a modified version of 105 | the Linked Version to produce a modified Combined Work, in the 106 | manner specified by section 6 of the GNU GPL for conveying 107 | Corresponding Source. 108 | 109 | 1) Use a suitable shared library mechanism for linking with the 110 | Library. A suitable mechanism is one that (a) uses at run time 111 | a copy of the Library already present on the user's computer 112 | system, and (b) will operate properly with a modified version 113 | of the Library that is interface-compatible with the Linked 114 | Version. 115 | 116 | e) Provide Installation Information, but only if you would otherwise 117 | be required to provide such information under section 6 of the 118 | GNU GPL, and only to the extent that such information is 119 | necessary to install and execute a modified version of the 120 | Combined Work produced by recombining or relinking the 121 | Application with a modified version of the Linked Version. (If 122 | you use option 4d0, the Installation Information must accompany 123 | the Minimal Corresponding Source and Corresponding Application 124 | Code. If you use option 4d1, you must provide the Installation 125 | Information in the manner specified by section 6 of the GNU GPL 126 | for conveying Corresponding Source.) 127 | 128 | 5. Combined Libraries. 129 | 130 | You may place library facilities that are a work based on the 131 | Library side by side in a single library together with other library 132 | facilities that are not Applications and are not covered by this 133 | License, and convey such a combined library under terms of your 134 | choice, if you do both of the following: 135 | 136 | a) Accompany the combined library with a copy of the same work based 137 | on the Library, uncombined with any other library facilities, 138 | conveyed under the terms of this License. 139 | 140 | b) Give prominent notice with the combined library that part of it 141 | is a work based on the Library, and explaining where to find the 142 | accompanying uncombined form of the same work. 143 | 144 | 6. Revised Versions of the GNU Lesser General Public License. 145 | 146 | The Free Software Foundation may publish revised and/or new versions 147 | of the GNU Lesser General Public License from time to time. Such new 148 | versions will be similar in spirit to the present version, but may 149 | differ in detail to address new problems or concerns. 150 | 151 | Each version is given a distinguishing version number. If the 152 | Library as you received it specifies that a certain numbered version 153 | of the GNU Lesser General Public License "or any later version" 154 | applies to it, you have the option of following the terms and 155 | conditions either of that published version or of any later version 156 | published by the Free Software Foundation. If the Library as you 157 | received it does not specify a version number of the GNU Lesser 158 | General Public License, you may choose any version of the GNU Lesser 159 | General Public License ever published by the Free Software Foundation. 160 | 161 | If the Library as you received it specifies that a proxy can decide 162 | whether future versions of the GNU Lesser General Public License shall 163 | apply, that proxy's public statement of acceptance of any version is 164 | permanent authorization for you to choose that version for the 165 | Library. 166 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Worker Glass [Unmaintained] 2 | 3 | **Note**: This library is no longer in use in the Karafka ecosystem. It was developed for Karafka versions prior to `1.0`. If you're using this library and want to take it over, please ping us. 4 | 5 | [![Build Status](https://github.com/karafka/worker-glass/workflows/ci/badge.svg)](https://github.com/karafka/worker-glass/actions?query=workflow%3Aci) 6 | [![Gem Version](https://badge.fury.io/rb/worker-glass.svg)](http://badge.fury.io/rb/worker-glass) 7 | [![Join the chat at https://slack.karafka.io](https://raw.githubusercontent.com/karafka/misc/master/slack.svg)](https://slack.karafka.io) 8 | 9 | WorkerGlass provides optional timeout and after failure (reentrancy) for background processing worker engines (like Sidekiq, Resque, etc). 10 | 11 | ## Reentrancy 12 | 13 | If you don't know what is reentrancy, you can read about it [here](http://dev.mensfeld.pl/2014/05/ruby-rails-sinatra-background-processing-reentrancy-for-your-workers-is-a-must-be/). 14 | 15 | ## Setup 16 | 17 | If you want to use timeout and/or reentrancy, please add appropriate modules into your worker. 18 | 19 | WorkerGlass allows to configure following options: 20 | 21 | | Method | Arguments | Description | 22 | |------------------|-----------|------------------------------------------------------------------------------------------| 23 | | self.logger= | Logger | Set logger which will be used by Worker Glass (if not defined, null logger will be used) | 24 | 25 | ## Usage 26 | 27 | WorkerGlass has few submodules that you can prepend to your workers to obtain given functionalities: 28 | 29 | | Module | Description | 30 | |-------------------------|-------------------------------------------------------------------| 31 | | WorkerGlass::Reentrancy | Provides additional reentrancy layer if anything goes wrong | 32 | | WorkerGlass::Timeout | Allows to set a timeout after which a given worker task will fail | 33 | 34 | 35 | ### WorkerGlass::Timeout 36 | 37 | If you want to provide timeouts for your workers, just prepend WorkerGlass::Timeout to your worker and set the timeout value: 38 | 39 | ```ruby 40 | class Worker2 41 | prepend WorkerGlass::Timeout 42 | 43 | self.timeout = 60 # 1 minute timeout 44 | 45 | def perform(first_param, second_param, third_param) 46 | SomeService.new.process(first_param, second_param, third_param) 47 | end 48 | end 49 | 50 | Worker2.perform_async(example1, example2, example3) 51 | ``` 52 | 53 | ### WorkerGlass::Reentrancy 54 | 55 | If you want to provide reentrancy for your workers, just prepend WorkerGlass::Reentrancy to your worker and define **after_failure** method that will be executed upon failure: 56 | 57 | ```ruby 58 | class Worker3 59 | prepend WorkerGlass::Reentrancy 60 | 61 | def perform(first_param, second_param, third_param) 62 | SomeService.new.process(first_param, second_param, third_param) 63 | end 64 | 65 | def after_failure(first_param, second_param, third_param) 66 | SomeService.new.reset_state(first_param, second_param, third_param) 67 | end 68 | end 69 | 70 | Worker3.perform_async(example1, example2, example3) 71 | ``` 72 | 73 | ## References 74 | 75 | * [Karafka framework](https://github.com/karafka/karafka) 76 | * [Worker Glass Actions CI](https://github.com/karafka/worker-glass/actions?query=workflow%3Aci) 77 | * [Worker Glass Coditsu](https://app.coditsu.io/karafka/repositories/worker-glass) 78 | 79 | ## Note on contributions 80 | 81 | First, thank you for considering contributing to the Karafka ecosystem! It's people like you that make the open source community such a great community! 82 | 83 | Each pull request must pass all the RSpec specs, integration tests and meet our quality requirements. 84 | 85 | Fork it, update and wait for the Github Actions results. 86 | -------------------------------------------------------------------------------- /certs/mensfeld.pem: -------------------------------------------------------------------------------- 1 | -----BEGIN CERTIFICATE----- 2 | MIIEODCCAqCgAwIBAgIBATANBgkqhkiG9w0BAQsFADAjMSEwHwYDVQQDDBhtYWNp 3 | ZWovREM9bWVuc2ZlbGQvREM9cGwwHhcNMTkwNzMwMTQ1NDU0WhcNMjAwNzI5MTQ1 4 | NDU0WjAjMSEwHwYDVQQDDBhtYWNpZWovREM9bWVuc2ZlbGQvREM9cGwwggGiMA0G 5 | CSqGSIb3DQEBAQUAA4IBjwAwggGKAoIBgQC9fCwtaHZG2SyyNXiH8r0QbJQx/xxl 6 | dkvwWz9QGJO+O8rEx20FB1Ab+MVkfOscwIv5jWpmk1U9whzDPl1uFtIbgu+sk+Zb 7 | uQlZyK/DPN6c+/BbBL+RryTBRyvkPLoCVwm7uxc/JZ1n4AI6eF4cCZ2ieZ9QgQbU 8 | MQs2QPqs9hT50Ez/40GnOdadVfiDDGz+NME2C4ms0BriXwZ1tcRTfJIHe2xjIbbb 9 | y5qRGfsLKcgMzvLQR24olixyX1MR0s4+Wveq3QL/gBhL4veUcv+UABJA8IJR0kyB 10 | seHHutusiwZ1v3SjjjW1xLLrc2ARV0mgCb0WaK2T4iA3oFTGLh6Ydz8LNl31KQFv 11 | 94nRd8IhmJxrhQ6dQ/WT9IXoa5S9lfT5lPJeINemH4/6QPABzf9W2IZlCdI9wCdB 12 | TBaw57MKneGAYZiKjw6OALSy2ltQUCl3RqFl3VP7n8uFy1U987Q5VIIQ3O1UUsQD 13 | Oe/h+r7GUU4RSPKgPlrwvW9bD/UQ+zF51v8CAwEAAaN3MHUwCQYDVR0TBAIwADAL 14 | BgNVHQ8EBAMCBLAwHQYDVR0OBBYEFJNIBHdfEUD7TqHqIer2YhWaWhwcMB0GA1Ud 15 | EQQWMBSBEm1hY2llakBtZW5zZmVsZC5wbDAdBgNVHRIEFjAUgRJtYWNpZWpAbWVu 16 | c2ZlbGQucGwwDQYJKoZIhvcNAQELBQADggGBAKA4eqko6BTNhlysip6rfBkVTGri 17 | ZXsL+kRb2hLvsQJS/kLyM21oMlu+LN0aPj3qEFR8mE/YeDD8rLAfruBRTltPNbR7 18 | xA5eE1gkxY5LfExUtK3b2wPqfmo7mZgfcsMwfYg/tUXw1WpBCnrhAJodpGH6SXmp 19 | A40qFUZst0vjiOoO+aTblIHPmMJXoZ3K42dTlNKlEiDKUWMRKSgpjjYGEYalFNWI 20 | hHfCz2r8L2t+dYdMZg1JGbEkq4ADGsAA8ioZIpJd7V4hI17u5TCdi7X5wh/0gN0E 21 | CgP+nLox3D+l2q0QuQEkayr+auFYkzTCkF+BmEk1D0Ru4mcf3F4CJvEmW4Pzbjqt 22 | i1tsCWPtJ4E/UUKnKaWKqGbjrjHJ0MuShYzHkodox5IOiCXIQg+1+YSzfXUV6WEK 23 | KJG/fhg1JV5vVDdVy6x+tv5SQ5ctU0feCsVfESi3rE3zRd+nvzE9HcZ5aXeL1UtJ 24 | nT5Xrioegu2w1jPyVEgyZgTZC5rvD0nNS5sFNQ== 25 | -----END CERTIFICATE----- 26 | -------------------------------------------------------------------------------- /lib/worker_glass.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | %w[ 4 | null_logger 5 | timeout 6 | active_support/core_ext/class/attribute 7 | ].each { |lib| require lib } 8 | 9 | base_path = File.join(File.dirname(__FILE__), 'worker_glass') 10 | 11 | %w[ 12 | version 13 | errors 14 | timeout 15 | reentrancy 16 | ].each { |lib| require File.join(base_path, lib) } 17 | 18 | # Background worker wrappers that provides optional timeout and after failure (reentrancy) 19 | module WorkerGlass 20 | class << self 21 | attr_writer :logger 22 | 23 | # @return [Logger] logger that we want to use 24 | def logger 25 | @logger ||= NullLogger.new 26 | end 27 | end 28 | end 29 | -------------------------------------------------------------------------------- /lib/worker_glass/errors.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | module WorkerGlass 4 | # Module enclosing all the errors that might be raised from this library 5 | module Errors 6 | # Base class for all the WorkerGlass internal errors 7 | class BaseError < StandardError; end 8 | # Raised when we use Timeout feature and timeout occurs 9 | class TimeoutError < BaseError; end 10 | # Raised when we use Timeout feature but we don't specify default timeout 11 | class TimeoutNotDefined < BaseError; end 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /lib/worker_glass/reentrancy.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | module WorkerGlass 4 | # This module provides a reentrancy functionality for background processing engine 5 | # @note If will reraise a given error - it does not silence them 6 | # @example Example usage with Sidekiq - if something fails, after_failure will be executed 7 | # class Worker 8 | # include Sidekiq::Worker 9 | # prepend WorkerGlass::Reentrancy 10 | # 11 | # def perform(*args) 12 | # FailingService.new.run(*args) 13 | # end 14 | # 15 | # def after_failure(*args) 16 | # FailingService.new.reset(*args) 17 | # end 18 | # end 19 | module Reentrancy 20 | # Executes a business logic with additional timeouts 21 | # @param args Any arguments that we passed when scheduling a background job 22 | def perform(*args) 23 | super 24 | rescue StandardError => e 25 | WorkerGlass.logger.fatal(e) 26 | after_failure(*args) 27 | raise e 28 | end 29 | end 30 | end 31 | -------------------------------------------------------------------------------- /lib/worker_glass/timeout.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | module WorkerGlass 4 | # This module provides additional timeout functionality for background processing engine 5 | # @example Example usage with Sidekiq - will fail with timeout error after 10 seconds 6 | # class Worker 7 | # include Sidekiq::Worker 8 | # prepend WorkerGlass::Timeout 9 | # 10 | # self.timeout = 10 11 | # 12 | # def perform(*args) 13 | # SlowService.new.run(*args) 14 | # end 15 | # end 16 | module Timeout 17 | # Adds a timeout class attribute to prepended class 18 | # @param base [Class] base class to which we prepend this module 19 | def self.prepended(base) 20 | base.class_attribute :timeout 21 | end 22 | 23 | # Executes a business logic with additional timeouts 24 | # @param args Any arguments that we passed when scheduling a background job 25 | # @raise [WorkerGlass::Errors::TimeoutNotDefined] if we didn't define timeout 26 | def perform(*args) 27 | raise Errors::TimeoutNotDefined unless self.class.timeout 28 | 29 | ::Timeout.timeout(self.class.timeout, Errors::TimeoutError) { super } 30 | end 31 | end 32 | end 33 | -------------------------------------------------------------------------------- /lib/worker_glass/version.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | # Main wrapper 4 | module WorkerGlass 5 | # Current version of WorkerGlass gem 6 | VERSION = '0.2.5' 7 | end 8 | -------------------------------------------------------------------------------- /spec/lib/worker_glass/errors_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | -------------------------------------------------------------------------------- /spec/lib/worker_glass/reentrancy_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | RSpec.describe WorkerGlass::Reentrancy do 4 | subject(:dummy_instance) { dummy_class.new } 5 | 6 | let(:param_value) { rand } 7 | let(:base_class) do 8 | prepended = described_class 9 | 10 | ClassBuilder.build do 11 | prepend prepended 12 | 13 | def perform(*args) 14 | run 15 | args.first * 2 16 | end 17 | 18 | def run 19 | 10 20 | end 21 | 22 | def after_failure(*_args); end 23 | end 24 | end 25 | let(:dummy_class) { base_class } 26 | 27 | describe '#perform' do 28 | context 'when it runs without issues' do 29 | it 'expect to run and return value of #perform' do 30 | expect(dummy_instance.perform(param_value)).to eq param_value * 2 31 | end 32 | end 33 | 34 | context 'when it fails to run' do 35 | let(:error) { StandardError } 36 | 37 | let(:dummy_class) do 38 | Class.new(base_class) do 39 | def run 40 | raise StandardError 41 | end 42 | end 43 | end 44 | 45 | it 'expect to log failure' do 46 | expect(WorkerGlass.logger).to receive(:fatal) 47 | expect { dummy_instance.perform(param_value) }.to raise_error error 48 | end 49 | 50 | it 'expect to run after_failure and reraise' do 51 | expect { dummy_instance.perform(param_value) }.to raise_error error 52 | end 53 | end 54 | end 55 | end 56 | -------------------------------------------------------------------------------- /spec/lib/worker_glass/timeout_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | RSpec.describe WorkerGlass::Timeout do 4 | subject(:dummy_instance) { dummy_klass.new } 5 | 6 | let(:params_values) { Array.new(rand(10)) { rand } } 7 | let(:dummy_klass) do 8 | prepended = described_class 9 | 10 | ClassBuilder.build do 11 | prepend prepended 12 | 13 | self.timeout = 1 14 | 15 | def perform(*args) 16 | sleep(args.first) 17 | args.first * 2 18 | end 19 | end 20 | end 21 | 22 | describe '#perform' do 23 | context 'when timeout is defined and it runs fast enough' do 24 | let(:sleep_time) { 0 } 25 | 26 | it 'expect to run and return value of #perform' do 27 | expect(dummy_instance.perform(sleep_time)).to eq sleep_time * 2 28 | end 29 | end 30 | 31 | context 'when timeout is defined and it runs too slow' do 32 | let(:sleep_time) { 2 } 33 | 34 | it 'expect to fail with Timeout error' do 35 | error = WorkerGlass::Errors::TimeoutError 36 | expect { dummy_instance.perform(sleep_time) }.to raise_error error 37 | end 38 | end 39 | end 40 | 41 | context 'when timeout is not defined' do 42 | let(:dummy_klass) do 43 | prepended = described_class 44 | 45 | ClassBuilder.build do 46 | prepend prepended 47 | 48 | def perform(*args); end 49 | end 50 | end 51 | 52 | it 'expect to raise TimeoutNotDefined error' do 53 | expect { dummy_instance.perform }.to raise_error WorkerGlass::Errors::TimeoutNotDefined 54 | end 55 | end 56 | end 57 | -------------------------------------------------------------------------------- /spec/lib/worker_glass/version_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | RSpec.describe WorkerGlass do 4 | it { expect { described_class::VERSION }.not_to raise_error } 5 | end 6 | -------------------------------------------------------------------------------- /spec/lib/worker_glass_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | # This file basically only requires stuff 4 | RSpec.describe WorkerGlass do 5 | subject(:worker_glass) { described_class } 6 | 7 | it 'expect to be defined' do 8 | expect(defined?(worker_glass::VERSION)).to eq 'constant' 9 | end 10 | 11 | describe '#logger' do 12 | context 'when we set our custom logger' do 13 | let(:logger) { double } 14 | 15 | before do 16 | worker_glass.logger = logger 17 | end 18 | 19 | after do 20 | worker_glass.instance_variable_set(:@logger, nil) 21 | end 22 | 23 | it { expect(worker_glass.logger).to eq logger } 24 | end 25 | 26 | context 'when we dont set custom logger' do 27 | it { expect(worker_glass.logger).to be_a NullLogger } 28 | end 29 | end 30 | end 31 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | coverage = !ENV.key?('GITHUB_WORKFLOW') 4 | coverage = true if ENV['GITHUB_COVERAGE'] == 'true' 5 | 6 | if coverage 7 | require 'simplecov' 8 | 9 | # Don't include unnecessary stuff into rcov 10 | SimpleCov.start do 11 | add_filter '/spec/' 12 | add_filter '/vendor/' 13 | add_filter '/gems/' 14 | add_filter '/.bundle/' 15 | add_filter '/doc/' 16 | add_filter '/config/' 17 | 18 | merge_timeout 600 19 | minimum_coverage 100 20 | enable_coverage :branch 21 | end 22 | end 23 | 24 | Dir["#{File.dirname(__FILE__)}/support/**/*.rb"] 25 | .sort 26 | .each { |f| require f } 27 | 28 | RSpec.configure do |config| 29 | config.disable_monkey_patching! 30 | config.order = :random 31 | 32 | config.expect_with :rspec do |expectations| 33 | expectations.include_chain_clauses_in_custom_matcher_descriptions = true 34 | end 35 | end 36 | 37 | require 'worker_glass' 38 | -------------------------------------------------------------------------------- /spec/support/class_builder.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | # Class builder helps creating anonymous classes that we can use to spec our code 4 | # We need co create new class instances to have an "empty" and "clear" class for each spec 5 | # This module acts as an interface to create classes 6 | module ClassBuilder 7 | class << self 8 | # Creates an empty class without any predefined methods 9 | # @param block [Proc, nil] block that should be evaluated (if given) 10 | # @return [Class] created anonymous class 11 | def build(&block) 12 | klass = Class.new 13 | 14 | klass.class_eval(&block) if block_given? 15 | klass 16 | end 17 | 18 | # This method allows us to create a class that inherits from any other 19 | # @param klass [Class] any class from which we want to inherit in our anonymous class 20 | # @param block [Proc] a block of code that should be evaluated in a new anonymous class body 21 | # @return [Class] new anonymous class 22 | def inherit(klass, &block) 23 | Class.new(klass, &block) 24 | end 25 | end 26 | end 27 | -------------------------------------------------------------------------------- /worker-glass.gemspec: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | lib = File.expand_path('lib', __dir__) 4 | $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) 5 | 6 | require 'worker_glass/version' 7 | 8 | Gem::Specification.new do |spec| 9 | spec.name = 'worker-glass' 10 | spec.version = WorkerGlass::VERSION 11 | spec.platform = Gem::Platform::RUBY 12 | spec.authors = ['Maciej Mensfeld', 'Pavlo Vavruk'] 13 | spec.email = %w[maciej@mensfeld.pl pavlo.vavruk@gmail.com] 14 | spec.summary = 'Timeout and Reentrancy for your background processing workers!' 15 | spec.description = 'Background worker wrappers that provides optional timeout and reentrancy' 16 | spec.homepage = 'https://github.com/karafka/worker-glass' 17 | spec.license = 'LGPL-3.0' 18 | spec.required_ruby_version = '>= 2.5.0' 19 | 20 | spec.add_dependency 'activesupport' 21 | spec.add_dependency 'null-logger' 22 | 23 | if $PROGRAM_NAME.end_with?('gem') 24 | spec.signing_key = File.expand_path('~/.ssh/gem-private_key.pem') 25 | end 26 | 27 | spec.cert_chain = %w[certs/mensfeld.pem] 28 | spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(spec)/}) } 29 | spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } 30 | spec.require_paths = %w[lib] 31 | end 32 | --------------------------------------------------------------------------------