background_worker 0.2.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.bundle/config +2 -0
- data/.github/dependabot.yml +6 -0
- data/.github/workflows/release.yml +59 -0
- data/.github/workflows/ruby.yml +24 -0
- data/.gitignore +2 -0
- data/.ruby-version +1 -0
- data/CHANGELOG.md +15 -1
- data/README.md +35 -9
- data/Rakefile +12 -1
- data/background_worker.gemspec +4 -2
- data/gemfiles/rails60.gemfile +8 -0
- data/gemfiles/rails61.gemfile +8 -0
- data/gemfiles/rails70.gemfile +8 -0
- data/lib/background_worker/base.rb +3 -5
- data/lib/background_worker/config.rb +2 -1
- data/lib/background_worker/version.rb +1 -1
- data/lib/background_worker.rb +4 -6
- data/spec/base_spec.rb +39 -0
- data/spec/schema.rb +5 -0
- data/spec/spec_helper.rb +1 -0
- data/spec/support/coverage_loader.rb +2 -3
- metadata +19 -9
- data/.travis.yml +0 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 851e96fbed1359db54547088ea0be4bb7a6c145169b61b82b42ab7e4a2825498
|
4
|
+
data.tar.gz: 608ed8a043ff96f7104262ad5d38691b8c27ce947060708a960fa85280351d6d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e3c702150edadf96f131e54a8f95477a28fd79ded9387d4df29e59cb8a5101f334bb7945440b7fe56f149ab661f0d847d7c0b4d488d182930c4781aa51f36831
|
7
|
+
data.tar.gz: 44212955d5f1410e42b69a7344f8eb8079e0cacf184b6ed5030ce03a5da2c21cfc4f2a158fba0e8cd5110fb39b67df487b6ac1db386e437b4ca1231dcd4c91e6
|
data/.bundle/config
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
name: Release
|
2
|
+
|
3
|
+
on:
|
4
|
+
push:
|
5
|
+
tags:
|
6
|
+
- "v*"
|
7
|
+
|
8
|
+
jobs:
|
9
|
+
build:
|
10
|
+
name: Build
|
11
|
+
runs-on: ubuntu-latest
|
12
|
+
steps:
|
13
|
+
- name: Checkout
|
14
|
+
uses: actions/checkout@v2
|
15
|
+
- uses: ruby/setup-ruby@v1
|
16
|
+
with:
|
17
|
+
bundler-cache: true
|
18
|
+
- run: bundle exec rake
|
19
|
+
|
20
|
+
release:
|
21
|
+
needs: build
|
22
|
+
name: Release
|
23
|
+
runs-on: ubuntu-latest
|
24
|
+
steps:
|
25
|
+
- name: Checkout
|
26
|
+
uses: actions/checkout@v2
|
27
|
+
|
28
|
+
- name: Generate Changelog
|
29
|
+
run: |
|
30
|
+
# Get version from github ref (remove 'refs/tags/' and prefix 'v')
|
31
|
+
version="${GITHUB_REF#refs/tags/v}"
|
32
|
+
npx changelog-parser CHANGELOG.md | jq -cr ".versions | .[] | select(.version == \"$version\") | .body" > ${{ github.workflow }}-CHANGELOG.txt
|
33
|
+
|
34
|
+
- name: Release
|
35
|
+
uses: softprops/action-gh-release@v1
|
36
|
+
with:
|
37
|
+
body_path: ${{ github.workflow }}-CHANGELOG.txt
|
38
|
+
env:
|
39
|
+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
40
|
+
|
41
|
+
publish:
|
42
|
+
needs: [build, release]
|
43
|
+
name: Publish
|
44
|
+
runs-on: ubuntu-latest
|
45
|
+
|
46
|
+
steps:
|
47
|
+
- uses: actions/checkout@v2
|
48
|
+
- uses: ruby/setup-ruby@v1
|
49
|
+
|
50
|
+
- name: Publish to RubyGems
|
51
|
+
run: |
|
52
|
+
mkdir -p $HOME/.gem
|
53
|
+
touch $HOME/.gem/credentials
|
54
|
+
chmod 0600 $HOME/.gem/credentials
|
55
|
+
printf -- "---\n:rubygems_api_key: ${GEM_HOST_API_KEY}\n" > $HOME/.gem/credentials
|
56
|
+
gem build *.gemspec
|
57
|
+
gem push *.gem
|
58
|
+
env:
|
59
|
+
GEM_HOST_API_KEY: "${{secrets.RUBYGEMS_AUTH_TOKEN}}"
|
@@ -0,0 +1,24 @@
|
|
1
|
+
name: Build and Test
|
2
|
+
on: [push, pull_request]
|
3
|
+
jobs:
|
4
|
+
test:
|
5
|
+
strategy:
|
6
|
+
fail-fast: false
|
7
|
+
matrix:
|
8
|
+
gemfile: [rails60, rails61, rails70]
|
9
|
+
ruby: ["2.7", "3.0", "3.1"]
|
10
|
+
runs-on: ubuntu-latest
|
11
|
+
env:
|
12
|
+
BUNDLE_GEMFILE: gemfiles/${{ matrix.gemfile }}.gemfile
|
13
|
+
steps:
|
14
|
+
- uses: actions/checkout@v2
|
15
|
+
- uses: ruby/setup-ruby@v1
|
16
|
+
with:
|
17
|
+
ruby-version: ${{ matrix.ruby }}
|
18
|
+
bundler-cache: true
|
19
|
+
- run: bundle exec rake
|
20
|
+
- name: Coveralls
|
21
|
+
uses: coverallsapp/github-action@master
|
22
|
+
with:
|
23
|
+
github-token: ${{ secrets.GITHUB_TOKEN }}
|
24
|
+
path-to-lcov: coverage/lcov.info
|
data/.gitignore
CHANGED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
3.1.0
|
data/CHANGELOG.md
CHANGED
@@ -3,7 +3,21 @@ All notable changes to this project will be documented in this file.
|
|
3
3
|
This project adheres to [Semantic Versioning](http://semver.org/).
|
4
4
|
This changelog adheres to [Keep a CHANGELOG](http://keepachangelog.com/).
|
5
5
|
|
6
|
-
##
|
6
|
+
## 0.5.0
|
7
|
+
|
8
|
+
- [PLAT-183] Ruby 3.1, Rails 7.0 and push coverage with github action
|
9
|
+
|
10
|
+
## 0.4.0
|
11
|
+
|
12
|
+
- [TT-8623] Update to build with github actions / ruby 3.0 / rails 6.1
|
13
|
+
|
14
|
+
## 0.3.0
|
15
|
+
|
16
|
+
- [TT-6292] Support Rails 5.2 built-in redis cache, remove legacy supports
|
17
|
+
|
18
|
+
## 0.2.1
|
19
|
+
### Fixed
|
20
|
+
- [RU-123] Worker disconnecting within transactions in rails 4+
|
7
21
|
|
8
22
|
## 0.2.0
|
9
23
|
### Added
|
data/README.md
CHANGED
@@ -1,10 +1,14 @@
|
|
1
|
-
Background Worker
|
2
|
-
|
1
|
+
# Background Worker
|
2
|
+
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/background_worker.svg)](http://badge.fury.io/rb/background_worker)
|
4
|
+
[![Build Status](https://github.com/sealink/background_worker/workflows/Build%20and%20Test/badge.svg?branch=master)](https://github.com/sealink/background_worker/actions)
|
5
|
+
[![Coverage Status](https://coveralls.io/repos/sealink/background_worker/badge.png)](https://coveralls.io/r/sealink/background_worker)
|
3
6
|
|
4
7
|
Provides a worker abstraction with an additional status channel.
|
5
8
|
|
6
9
|
Start by making a worker class which extends from BackgroundWorker::Base
|
7
10
|
|
11
|
+
```ruby
|
8
12
|
class MyWorker < BackgroundWorker::Base
|
9
13
|
def my_task(options={})
|
10
14
|
report_progress('Starting')
|
@@ -17,22 +21,27 @@ Start by making a worker class which extends from BackgroundWorker::Base
|
|
17
21
|
{original_message: message}
|
18
22
|
end
|
19
23
|
end
|
24
|
+
```
|
20
25
|
|
21
26
|
Then, when you want to perform a task in the background, use
|
22
27
|
klass#perform_in_background which exists in Base:
|
23
28
|
|
29
|
+
```ruby
|
24
30
|
worker_id = MyWorker.perform_in_background(:my_task, message: "hello!")
|
31
|
+
```
|
25
32
|
|
26
33
|
# Backgrounded
|
27
34
|
|
28
35
|
By default this will call your instance method in the foreground -- you have to
|
29
36
|
provide an #enqueue_with configuration like so:
|
30
37
|
|
38
|
+
```ruby
|
31
39
|
BackgroundWorker.configure(
|
32
40
|
enqueue_with: -> klass, method_name, options {
|
33
41
|
Resque.enqueue(klass, method_name, options)
|
34
42
|
}
|
35
43
|
)
|
44
|
+
```
|
36
45
|
|
37
46
|
This is independent of the status reporting which (currently) always uses Redis.
|
38
47
|
|
@@ -41,23 +50,40 @@ This is independent of the status reporting which (currently) always uses Redis.
|
|
41
50
|
The worker_id you are returned can be used to get the status and
|
42
51
|
whether the worker has finished successfully, failed, or is still in progress:
|
43
52
|
|
53
|
+
```ruby
|
44
54
|
state = BackgroundWorker.get_state_of(worker_id)
|
55
|
+
```
|
45
56
|
|
46
57
|
The state is represented by a hash with the following keys:
|
47
58
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
59
|
+
| key | description |
|
60
|
+
| ------------------------------------------------------------ | -------------------------------------------------------------------- |
|
61
|
+
| message | Reported message |
|
62
|
+
| detailed_message | Detailed version of above when provided |
|
63
|
+
| status | :successful, :failed, or null if still processing |
|
64
|
+
| completed | True if report_failed, report_successful called (or worker |
|
65
|
+
| finished without exception -- which calls report_successful) |
|
66
|
+
| data | Arbitrary data returned by worker method on success or report_failed |
|
54
67
|
|
55
68
|
If an exception is raised, the worker will call #report_failed with the
|
56
69
|
details. You can provide a callback with #after_exception in the config.
|
57
70
|
|
58
|
-
#
|
71
|
+
# Installation
|
59
72
|
|
60
73
|
Add to your Gemfile:
|
61
74
|
|
75
|
+
```ruby
|
62
76
|
gem 'background_worker'
|
77
|
+
```
|
78
|
+
|
79
|
+
# Release
|
80
|
+
|
81
|
+
To publish a new version of this gem the following steps must be taken.
|
63
82
|
|
83
|
+
- Update the version in the following files
|
84
|
+
```
|
85
|
+
CHANGELOG.md
|
86
|
+
lib/background_worker/version.rb
|
87
|
+
```
|
88
|
+
- Create a tag using the format v0.1.0
|
89
|
+
- Follow build progress in GitHub actions
|
data/Rakefile
CHANGED
@@ -1 +1,12 @@
|
|
1
|
-
require
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
|
3
|
+
desc 'Default: run specs.'
|
4
|
+
task :default => :spec
|
5
|
+
|
6
|
+
require 'rspec/core/rake_task'
|
7
|
+
|
8
|
+
desc "Run specs"
|
9
|
+
RSpec::Core::RakeTask.new do |t|
|
10
|
+
t.pattern = "./spec/**/*_spec.rb" # don't need this, it's default.
|
11
|
+
# Put spec opts in a file named .rspec in root
|
12
|
+
end
|
data/background_worker.gemspec
CHANGED
@@ -19,13 +19,15 @@ Gem::Specification.new do |spec|
|
|
19
19
|
spec.executables = spec.files.grep(/^bin\//) { |f| File.basename(f) }
|
20
20
|
spec.test_files = spec.files.grep(/^(test|spec|features)\//)
|
21
21
|
spec.require_paths = ['lib']
|
22
|
+
spec.required_ruby_version = '>= 2.7'
|
22
23
|
|
23
24
|
spec.add_development_dependency 'activesupport'
|
24
25
|
spec.add_development_dependency 'bundler'
|
25
26
|
spec.add_development_dependency 'rake'
|
26
27
|
spec.add_development_dependency 'rspec'
|
27
28
|
spec.add_development_dependency 'coverage-kit'
|
28
|
-
spec.add_development_dependency 'coveralls'
|
29
|
-
spec.add_development_dependency 'simplecov-rcov'
|
30
29
|
spec.add_development_dependency 'rubocop'
|
30
|
+
|
31
|
+
spec.add_development_dependency 'activerecord'
|
32
|
+
spec.add_development_dependency 'sqlite3'
|
31
33
|
end
|
@@ -7,14 +7,12 @@ module BackgroundWorker
|
|
7
7
|
attr_accessor :uid, :state
|
8
8
|
|
9
9
|
def initialize(options = {})
|
10
|
-
Time.zone = Setting.time_zone
|
11
|
-
|
12
10
|
@uid = options[:uid]
|
13
11
|
|
14
12
|
# Store state persistently, to enable status checkups & progress reporting
|
15
13
|
@state = BackgroundWorker::PersistentState.new(@uid, options.except(:uid))
|
16
14
|
log("Created #{self.class}")
|
17
|
-
log("Options are: #{options.
|
15
|
+
log("Options are: #{options.inspect}")
|
18
16
|
end
|
19
17
|
|
20
18
|
# Report progress...
|
@@ -79,13 +77,13 @@ module BackgroundWorker
|
|
79
77
|
#
|
80
78
|
# It will just call your preferred method in the worker.
|
81
79
|
def perform(method_name, options = {})
|
82
|
-
BackgroundWorker.verify_active_connections!
|
80
|
+
BackgroundWorker.verify_active_connections! if BackgroundWorker.config.backgrounded
|
83
81
|
|
84
82
|
worker = new(options)
|
85
83
|
execution = WorkerExecution.new(worker, method_name, options)
|
86
84
|
execution.call
|
87
85
|
ensure
|
88
|
-
BackgroundWorker.release_connections!
|
86
|
+
BackgroundWorker.release_connections! if BackgroundWorker.config.backgrounded
|
89
87
|
end
|
90
88
|
end
|
91
89
|
end
|
@@ -2,7 +2,7 @@ require 'logger'
|
|
2
2
|
|
3
3
|
module BackgroundWorker
|
4
4
|
class Config
|
5
|
-
attr_reader :logger, :enqueue_with
|
5
|
+
attr_reader :logger, :enqueue_with, :backgrounded
|
6
6
|
|
7
7
|
# Configuration includes following options:
|
8
8
|
# logger: what logger to user throughout
|
@@ -19,6 +19,7 @@ module BackgroundWorker
|
|
19
19
|
@logger = attrs.fetch(:logger, ::Logger.new(STDOUT))
|
20
20
|
@enqueue_with = attrs.fetch(:enqueue_with, method(:foreground_enqueue))
|
21
21
|
@after_exception = attrs.fetch(:after_exception, method(:default_after_exception))
|
22
|
+
@backgrounded = attrs.fetch(:backgrounded, true)
|
22
23
|
end
|
23
24
|
|
24
25
|
# Callback fired when an exception occurs
|
data/lib/background_worker.rb
CHANGED
@@ -24,16 +24,14 @@ module BackgroundWorker
|
|
24
24
|
end
|
25
25
|
|
26
26
|
def self.verify_active_connections!
|
27
|
-
|
28
|
-
|
29
|
-
|
27
|
+
if defined?(Rails)
|
28
|
+
Rails.cache.reconnect if Rails.cache.respond_to?(:reconnect)
|
29
|
+
Rails.cache.redis.close if Rails.cache.respond_to?(:redis)
|
30
30
|
end
|
31
31
|
end
|
32
32
|
|
33
33
|
def self.release_connections!
|
34
|
-
|
35
|
-
ActiveRecord::Base.clear_all_connections!
|
36
|
-
end
|
34
|
+
ActiveRecord::Base.clear_all_connections!
|
37
35
|
end
|
38
36
|
|
39
37
|
def self.after_exception(e)
|
data/spec/base_spec.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
require 'active_support/core_ext/hash/keys' # Hash.symbolize_keys
|
4
|
+
require 'active_support/core_ext/numeric/time' # Numeric.hours
|
5
|
+
|
6
|
+
require 'active_record'
|
7
|
+
|
8
|
+
DB_FILE = 'tmp/test_db'
|
9
|
+
FileUtils.mkdir_p File.dirname(DB_FILE)
|
10
|
+
FileUtils.rm_f DB_FILE
|
11
|
+
|
12
|
+
ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => DB_FILE
|
13
|
+
|
14
|
+
load('spec/schema.rb')
|
15
|
+
|
16
|
+
describe BackgroundWorker::Base do
|
17
|
+
let(:cache) { double(write: nil, read: nil, reconnect: nil, store: nil) }
|
18
|
+
let(:model_class) { Model = Class.new(ActiveRecord::Base) }
|
19
|
+
let(:worker_class) {
|
20
|
+
Class.new(BackgroundWorker::Base) do
|
21
|
+
def store_in_cache(opts)
|
22
|
+
Rails.cache.store(opts[:value])
|
23
|
+
end
|
24
|
+
end
|
25
|
+
}
|
26
|
+
|
27
|
+
before do
|
28
|
+
stub_const 'Model', model_class
|
29
|
+
stub_const 'Rails', double(cache: cache, env: 'production')
|
30
|
+
BackgroundWorker.configure(backgrounded: false)
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'should perform action and handle transactions/connections appropriately' do
|
34
|
+
Model.transaction do
|
35
|
+
worker_class.perform_in_background(:store_in_cache, value: 42)
|
36
|
+
end
|
37
|
+
expect(cache).to have_received(:store).with(42)
|
38
|
+
end
|
39
|
+
end
|
data/spec/schema.rb
ADDED
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: background_worker
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael Noack
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date:
|
13
|
+
date: 2022-01-25 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: activesupport
|
@@ -83,7 +83,7 @@ dependencies:
|
|
83
83
|
- !ruby/object:Gem::Version
|
84
84
|
version: '0'
|
85
85
|
- !ruby/object:Gem::Dependency
|
86
|
-
name:
|
86
|
+
name: rubocop
|
87
87
|
requirement: !ruby/object:Gem::Requirement
|
88
88
|
requirements:
|
89
89
|
- - ">="
|
@@ -97,7 +97,7 @@ dependencies:
|
|
97
97
|
- !ruby/object:Gem::Version
|
98
98
|
version: '0'
|
99
99
|
- !ruby/object:Gem::Dependency
|
100
|
-
name:
|
100
|
+
name: activerecord
|
101
101
|
requirement: !ruby/object:Gem::Requirement
|
102
102
|
requirements:
|
103
103
|
- - ">="
|
@@ -111,7 +111,7 @@ dependencies:
|
|
111
111
|
- !ruby/object:Gem::Version
|
112
112
|
version: '0'
|
113
113
|
- !ruby/object:Gem::Dependency
|
114
|
-
name:
|
114
|
+
name: sqlite3
|
115
115
|
requirement: !ruby/object:Gem::Requirement
|
116
116
|
requirements:
|
117
117
|
- - ">="
|
@@ -133,15 +133,22 @@ executables: []
|
|
133
133
|
extensions: []
|
134
134
|
extra_rdoc_files: []
|
135
135
|
files:
|
136
|
+
- ".bundle/config"
|
137
|
+
- ".github/dependabot.yml"
|
138
|
+
- ".github/workflows/release.yml"
|
139
|
+
- ".github/workflows/ruby.yml"
|
136
140
|
- ".gitignore"
|
137
141
|
- ".rubocop.yml"
|
138
|
-
- ".
|
142
|
+
- ".ruby-version"
|
139
143
|
- CHANGELOG.md
|
140
144
|
- Gemfile
|
141
145
|
- LICENSE.txt
|
142
146
|
- README.md
|
143
147
|
- Rakefile
|
144
148
|
- background_worker.gemspec
|
149
|
+
- gemfiles/rails60.gemfile
|
150
|
+
- gemfiles/rails61.gemfile
|
151
|
+
- gemfiles/rails70.gemfile
|
145
152
|
- lib/background_worker.rb
|
146
153
|
- lib/background_worker/base.rb
|
147
154
|
- lib/background_worker/config.rb
|
@@ -149,6 +156,8 @@ files:
|
|
149
156
|
- lib/background_worker/uid.rb
|
150
157
|
- lib/background_worker/version.rb
|
151
158
|
- lib/background_worker/worker_execution.rb
|
159
|
+
- spec/base_spec.rb
|
160
|
+
- spec/schema.rb
|
152
161
|
- spec/spec_helper.rb
|
153
162
|
- spec/support/coverage_loader.rb
|
154
163
|
- spec/uid_spec.rb
|
@@ -164,19 +173,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
164
173
|
requirements:
|
165
174
|
- - ">="
|
166
175
|
- !ruby/object:Gem::Version
|
167
|
-
version: '
|
176
|
+
version: '2.7'
|
168
177
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
169
178
|
requirements:
|
170
179
|
- - ">="
|
171
180
|
- !ruby/object:Gem::Version
|
172
181
|
version: '0'
|
173
182
|
requirements: []
|
174
|
-
|
175
|
-
rubygems_version: 2.5.2
|
183
|
+
rubygems_version: 3.3.3
|
176
184
|
signing_key:
|
177
185
|
specification_version: 4
|
178
186
|
summary: Background worker abstraction with status updates
|
179
187
|
test_files:
|
188
|
+
- spec/base_spec.rb
|
189
|
+
- spec/schema.rb
|
180
190
|
- spec/spec_helper.rb
|
181
191
|
- spec/support/coverage_loader.rb
|
182
192
|
- spec/uid_spec.rb
|