background_worker 0.6.0 → 0.8.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +12 -0
- data/README.md +4 -4
- data/lib/background_worker/base.rb +14 -6
- data/lib/background_worker/config.rb +2 -2
- data/lib/background_worker/uid.rb +4 -5
- data/lib/background_worker/version.rb +1 -1
- data/lib/background_worker/worker_execution.rb +2 -3
- data/lib/background_worker.rb +3 -3
- data/spec/base_spec.rb +9 -2
- data/spec/support/coverage_loader.rb +1 -1
- data/spec/uid_spec.rb +2 -3
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 061e6c0338c031523b85a90f084aaf996c00cbe9e668428931a98ae3b5a12f36
|
4
|
+
data.tar.gz: 36304e141265bc21d2d756d2af7aab6645fbfa9596d5ab5bfe2c115132f685a8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f799063ea369646eea2956427aa7bc0cd551229649e9e3cb1b324a8bd1edec162c7d0852dbf0c7ce724d0332c702208097c5a975827d050742c0df11e5e3709a
|
7
|
+
data.tar.gz: 6d4487e464251517e318fe59be864d87da48f324aabce3b6d8964971f55fa1a4a3b5dc7f66913fe6ee5654614a72d6fb978887a84829e0afe656313b6d693ca6
|
data/CHANGELOG.md
CHANGED
@@ -3,6 +3,18 @@ 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
|
+
## 0.8.1
|
7
|
+
|
8
|
+
- Fix version
|
9
|
+
|
10
|
+
## 0.8.0
|
11
|
+
|
12
|
+
- [PLAT-747] Remove the ability to pass through `method_name`
|
13
|
+
|
14
|
+
## 0.7.0
|
15
|
+
|
16
|
+
- [PLAT-670] Add queue_as method
|
17
|
+
|
6
18
|
## 0.6.0
|
7
19
|
|
8
20
|
- [PLAT-664] Align interface to ActiveJob
|
data/README.md
CHANGED
@@ -10,7 +10,7 @@ Start by making a worker class which extends from BackgroundWorker::Base
|
|
10
10
|
|
11
11
|
```ruby
|
12
12
|
class MyWorker < BackgroundWorker::Base
|
13
|
-
def
|
13
|
+
def perform(options={})
|
14
14
|
report_progress('Starting')
|
15
15
|
if options[:message].blank?
|
16
16
|
report_failed("No message provided")
|
@@ -27,7 +27,7 @@ Then, when you want to perform a task in the background, use
|
|
27
27
|
klass#perform_in_background which exists in Base:
|
28
28
|
|
29
29
|
```ruby
|
30
|
-
worker_id = MyWorker.
|
30
|
+
worker_id = MyWorker.perform_later(message: "hello!")
|
31
31
|
```
|
32
32
|
|
33
33
|
# Backgrounded
|
@@ -37,8 +37,8 @@ provide an #enqueue_with configuration like so:
|
|
37
37
|
|
38
38
|
```ruby
|
39
39
|
BackgroundWorker.configure(
|
40
|
-
enqueue_with: -> klass,
|
41
|
-
Resque.enqueue(klass,
|
40
|
+
enqueue_with: -> klass, options {
|
41
|
+
Resque.enqueue(klass, options)
|
42
42
|
}
|
43
43
|
)
|
44
44
|
```
|
@@ -15,6 +15,10 @@ module BackgroundWorker
|
|
15
15
|
log("Options are: #{options.inspect}")
|
16
16
|
end
|
17
17
|
|
18
|
+
def perform
|
19
|
+
raise AbstractError, 'Must be implemented in Job Class'
|
20
|
+
end
|
21
|
+
|
18
22
|
# Report progress...
|
19
23
|
def report_progress(message)
|
20
24
|
state.message = message
|
@@ -53,22 +57,22 @@ module BackgroundWorker
|
|
53
57
|
end
|
54
58
|
|
55
59
|
class << self
|
60
|
+
attr_reader :queue
|
56
61
|
def get_state_of(worker_id)
|
57
62
|
BackgroundWorker::PersistentState.get_state_of(worker_id)
|
58
63
|
end
|
59
64
|
|
60
65
|
# Public method to do in background...
|
61
|
-
def perform_later(
|
66
|
+
def perform_later(options = {})
|
62
67
|
opts = options.symbolize_keys
|
63
68
|
|
64
|
-
|
65
|
-
opts[:uid] ||= BackgroundWorker::Uid.new(to_s, method_name).generate
|
69
|
+
opts[:uid] ||= BackgroundWorker::Uid.new(to_s).generate
|
66
70
|
|
67
71
|
# Store into shared-cache before kicking job off
|
68
72
|
BackgroundWorker::PersistentState.new(opts[:uid], opts.except(:uid))
|
69
73
|
|
70
74
|
# Enqueue to the background queue
|
71
|
-
BackgroundWorker.enqueue(self,
|
75
|
+
BackgroundWorker.enqueue(self, opts)
|
72
76
|
|
73
77
|
opts[:uid]
|
74
78
|
end
|
@@ -76,15 +80,19 @@ module BackgroundWorker
|
|
76
80
|
# This method is called by the job runner
|
77
81
|
#
|
78
82
|
# It will just call your preferred method in the worker.
|
79
|
-
def perform_now(
|
83
|
+
def perform_now(options = {})
|
80
84
|
BackgroundWorker.verify_active_connections! if BackgroundWorker.config.backgrounded
|
81
85
|
|
82
86
|
worker = new(options)
|
83
|
-
execution = WorkerExecution.new(worker,
|
87
|
+
execution = WorkerExecution.new(worker, options)
|
84
88
|
execution.call
|
85
89
|
ensure
|
86
90
|
BackgroundWorker.release_connections! if BackgroundWorker.config.backgrounded
|
87
91
|
end
|
92
|
+
|
93
|
+
def queue_as(queue = nil)
|
94
|
+
@queue = queue&.to_sym || :default
|
95
|
+
end
|
88
96
|
end
|
89
97
|
end
|
90
98
|
end
|
@@ -27,8 +27,8 @@ module BackgroundWorker
|
|
27
27
|
@after_exception.call(e)
|
28
28
|
end
|
29
29
|
|
30
|
-
def foreground_enqueue(klass,
|
31
|
-
klass.perform_now(
|
30
|
+
def foreground_enqueue(klass, opts)
|
31
|
+
klass.perform_now(opts)
|
32
32
|
end
|
33
33
|
|
34
34
|
def default_after_exception(e)
|
@@ -1,11 +1,10 @@
|
|
1
1
|
# Generates a unique identifier for a particular job identified by class_name/method
|
2
2
|
module BackgroundWorker
|
3
3
|
class Uid
|
4
|
-
attr_reader :class_name
|
4
|
+
attr_reader :class_name
|
5
5
|
|
6
|
-
def initialize(class_name
|
6
|
+
def initialize(class_name)
|
7
7
|
@class_name = class_name
|
8
|
-
@method = method
|
9
8
|
end
|
10
9
|
|
11
10
|
def generate
|
@@ -15,11 +14,11 @@ module BackgroundWorker
|
|
15
14
|
private
|
16
15
|
|
17
16
|
def generate_uid_hash
|
18
|
-
::Digest::MD5.hexdigest("#{class_name}:#{
|
17
|
+
::Digest::MD5.hexdigest("#{class_name}:#{rand(1 << 64)}:#{Time.now}")
|
19
18
|
end
|
20
19
|
|
21
20
|
def generate_uid_name
|
22
|
-
"#{class_name.underscore}
|
21
|
+
"#{class_name.underscore}".split('/').join(':')
|
23
22
|
end
|
24
23
|
end
|
25
24
|
end
|
@@ -2,15 +2,14 @@ module BackgroundWorker
|
|
2
2
|
class WorkerExecution
|
3
3
|
attr_reader :worker, :method_name, :options
|
4
4
|
|
5
|
-
def initialize(worker,
|
5
|
+
def initialize(worker, options)
|
6
6
|
fail ArgumentError, "'uid' is required to identify worker" unless options[:uid].present?
|
7
7
|
@worker = worker
|
8
|
-
@method_name = method_name
|
9
8
|
@options = options
|
10
9
|
end
|
11
10
|
|
12
11
|
def call
|
13
|
-
worker.
|
12
|
+
worker.perform(options)
|
14
13
|
report_implicitly_successful unless completed?
|
15
14
|
|
16
15
|
rescue StandardError => e
|
data/lib/background_worker.rb
CHANGED
@@ -8,15 +8,15 @@ module BackgroundWorker
|
|
8
8
|
# eg:
|
9
9
|
# BackgroundWorker.configure(
|
10
10
|
# logger: Rails.logger,
|
11
|
-
# enqueue_with: -> klass,
|
11
|
+
# enqueue_with: -> klass, opts { Resque.enqueue(klass, opts) },
|
12
12
|
# after_exception: -> e { Airbrake.notify(e) }
|
13
13
|
# )
|
14
14
|
def self.configure(options)
|
15
15
|
@config = Config.new(options)
|
16
16
|
end
|
17
17
|
|
18
|
-
def self.enqueue(klass,
|
19
|
-
config.enqueue_with.call(klass,
|
18
|
+
def self.enqueue(klass, options)
|
19
|
+
config.enqueue_with.call(klass, options)
|
20
20
|
end
|
21
21
|
|
22
22
|
def self.logger
|
data/spec/base_spec.rb
CHANGED
@@ -18,7 +18,7 @@ describe BackgroundWorker::Base do
|
|
18
18
|
let(:model_class) { Model = Class.new(ActiveRecord::Base) }
|
19
19
|
let(:worker_class) {
|
20
20
|
Class.new(BackgroundWorker::Base) do
|
21
|
-
def
|
21
|
+
def perform(opts)
|
22
22
|
Rails.cache.store(opts[:value])
|
23
23
|
end
|
24
24
|
end
|
@@ -32,8 +32,15 @@ describe BackgroundWorker::Base do
|
|
32
32
|
|
33
33
|
it 'should perform action and handle transactions/connections appropriately' do
|
34
34
|
Model.transaction do
|
35
|
-
worker_class.perform_later(
|
35
|
+
worker_class.perform_later(value: 42)
|
36
36
|
end
|
37
37
|
expect(cache).to have_received(:store).with(42)
|
38
38
|
end
|
39
|
+
|
40
|
+
context '#queue_as' do
|
41
|
+
it 'should value queue correctly' do
|
42
|
+
worker_class.queue_as('low')
|
43
|
+
expect(worker_class.queue).to be :low
|
44
|
+
end
|
45
|
+
end
|
39
46
|
end
|
data/spec/uid_spec.rb
CHANGED
@@ -2,11 +2,10 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe BackgroundWorker::Uid do
|
4
4
|
let(:class_name) { 'CopyWorker' }
|
5
|
-
let(:
|
6
|
-
let(:uid_object) { BackgroundWorker::Uid.new(class_name, method) }
|
5
|
+
let(:uid_object) { BackgroundWorker::Uid.new(class_name) }
|
7
6
|
|
8
7
|
context '#generate' do
|
9
8
|
subject(:uid) { uid_object.generate }
|
10
|
-
it { is_expected.to match(/copy_worker:
|
9
|
+
it { is_expected.to match(/copy_worker:[0-9a-f]{16}/) }
|
11
10
|
end
|
12
11
|
end
|
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.8.1
|
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: 2022-09-
|
13
|
+
date: 2022-09-23 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: activesupport
|