services 4.3.0 → 5.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -2
- data/lib/services/asyncable.rb +15 -8
- data/lib/services/base.rb +3 -3
- data/lib/services/logger/file.rb +1 -1
- data/lib/services/logger/redis.rb +1 -1
- data/lib/services/modules/uniqueness_checker.rb +5 -5
- data/lib/services/version.rb +1 -1
- data/spec/services/modules/uniqueness_checker_spec.rb +2 -2
- data/spec/support/helpers.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7a54c2710e578f7af30ef239cd96b7f837630a02
|
4
|
+
data.tar.gz: a33159048724d2925ca48fb06acd92a67402833e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3c117a3e88a73ef1f712407d5fab186500759ec62f66f5617685dbeebed75b2a3a58adf0fd2f225b8b2dcfd6015864d0fe70e8729932abebddd07c93f65a2144
|
7
|
+
data.tar.gz: 8c661d04be05bf37b2916f79033b16f2a5208fb99978a03e31b05c5eddf3f2d39825fd946b95bef7e304b9799c7c6cf48c08cbf192b27e27051fc099119ff7ff
|
data/README.md
CHANGED
@@ -126,8 +126,8 @@ Services::Users::Delete.call [1, 2, 3] # with an array of user
|
|
126
126
|
|
127
127
|
# Execute asynchronously/in the background
|
128
128
|
|
129
|
-
Services::Users::Delete.
|
130
|
-
Services::Users::Delete.
|
129
|
+
Services::Users::Delete.call_async 1 # with a user ID
|
130
|
+
Services::Users::Delete.call_async [1, 2, 3] # with multiple user IDs
|
131
131
|
```
|
132
132
|
|
133
133
|
As you can see, you cannot use objects or a ActiveRecord::Relation as parameters when calling a service asynchronously since the arguments are serialized to Redis. This might change once Services works with [ActiveJob](https://github.com/rails/rails/tree/master/activejob) and [GlobalID](https://github.com/rails/globalid/).
|
data/lib/services/asyncable.rb
CHANGED
@@ -12,7 +12,9 @@ module Services
|
|
12
12
|
extend ActiveSupport::Concern
|
13
13
|
|
14
14
|
# The name of the parameter that is added to the parameter list when calling a method to be processed in the background.
|
15
|
-
TARGET_PARAM_NAME = :async_target_id
|
15
|
+
TARGET_PARAM_NAME = :async_target_id.freeze
|
16
|
+
|
17
|
+
ASYNC_METHOD_SUFFIXES = %i(async in at).freeze
|
16
18
|
|
17
19
|
included do
|
18
20
|
include Sidekiq::Worker
|
@@ -22,19 +24,24 @@ module Services
|
|
22
24
|
# Bulk enqueue items
|
23
25
|
# args can either be a one-dimensional or two-dimensional array,
|
24
26
|
# each item in args should be the arguments for one job.
|
25
|
-
def
|
26
|
-
# Convert args to two-dimensional array if it isn't already.
|
27
|
+
def bulk_call_async(args)
|
28
|
+
# Convert args to two-dimensional array if it isn't one already.
|
27
29
|
args = args.map { |arg| [arg] } if args.none? { |arg| arg.is_a?(Array) }
|
28
30
|
Sidekiq::Client.push_bulk 'class' => self, 'args' => args
|
29
31
|
end
|
32
|
+
|
33
|
+
ASYNC_METHOD_SUFFIXES.each do |async_method_suffix|
|
34
|
+
define_method "call_#{async_method_suffix}" do |*args|
|
35
|
+
self.public_send "perform_#{async_method_suffix}", *args
|
36
|
+
end
|
37
|
+
end
|
30
38
|
end
|
31
39
|
|
32
|
-
|
33
|
-
define_method
|
34
|
-
self.class.
|
40
|
+
ASYNC_METHOD_SUFFIXES.each do |async_method_suffix|
|
41
|
+
define_method "call_#{async_method_suffix}" do |*args|
|
42
|
+
self.class.public_send "perform_#{async_method_suffix}", *args, TARGET_PARAM_NAME => self.id
|
35
43
|
end
|
36
44
|
end
|
37
|
-
alias_method :perform_at, :perform_in
|
38
45
|
|
39
46
|
def perform(*args)
|
40
47
|
return self.call(*args) if self.is_a?(Services::Base)
|
@@ -45,7 +52,7 @@ module Services
|
|
45
52
|
self.class
|
46
53
|
end
|
47
54
|
|
48
|
-
target.
|
55
|
+
target.public_send *args
|
49
56
|
end
|
50
57
|
end
|
51
58
|
end
|
data/lib/services/base.rb
CHANGED
@@ -9,9 +9,9 @@ module Services
|
|
9
9
|
class << self
|
10
10
|
def inherited(subclass)
|
11
11
|
subclass.const_set :Error, Class.new(StandardError)
|
12
|
-
subclass.
|
13
|
-
subclass.
|
14
|
-
subclass.
|
12
|
+
subclass.public_send :include, Rails.application.routes.url_helpers if defined?(Rails)
|
13
|
+
subclass.public_send :include, Asyncable if defined?(Asyncable)
|
14
|
+
subclass.public_send :prepend, CallLogger, ExceptionWrapper, UniquenessChecker
|
15
15
|
end
|
16
16
|
|
17
17
|
delegate :call, to: :new
|
data/lib/services/logger/file.rb
CHANGED
@@ -4,17 +4,17 @@ module Services
|
|
4
4
|
KEY_PREFIX = %w(
|
5
5
|
services
|
6
6
|
uniqueness
|
7
|
-
).join(':')
|
7
|
+
).join(':').freeze
|
8
8
|
|
9
9
|
ON_ERROR = %i(
|
10
10
|
fail
|
11
11
|
ignore
|
12
12
|
reschedule
|
13
13
|
return
|
14
|
-
)
|
14
|
+
).freeze
|
15
15
|
|
16
|
-
MAX_RETRIES = 10
|
17
|
-
ONE_DAY = 60 * 60 * 24
|
16
|
+
MAX_RETRIES = 10.freeze
|
17
|
+
ONE_DAY = (60 * 60 * 24).freeze
|
18
18
|
|
19
19
|
def self.prepended(mod)
|
20
20
|
mod.const_set :NotUniqueError, Class.new(mod::Error)
|
@@ -95,7 +95,7 @@ module Services
|
|
95
95
|
convert_for_rescheduling arg
|
96
96
|
end
|
97
97
|
log "Rescheduling to be executed in #{retry_delay} seconds." if self.respond_to?(:log)
|
98
|
-
self.class.
|
98
|
+
self.class.call_in retry_delay, *reschedule_args
|
99
99
|
end
|
100
100
|
|
101
101
|
def error_count
|
data/lib/services/version.rb
CHANGED
@@ -57,7 +57,7 @@ shared_examples 'checking the uniqueness properly' do
|
|
57
57
|
fail_args.each do |fail_arg_group|
|
58
58
|
service = service_class.new
|
59
59
|
expect(service).to_not receive(:do_work)
|
60
|
-
expect(service_class).to receive(:
|
60
|
+
expect(service_class).to receive(:call_in).with(an_instance_of(Fixnum), *fail_arg_group, 'reschedule', false)
|
61
61
|
expect { service.call(*fail_arg_group, 'reschedule', false) }.to_not raise_error
|
62
62
|
end
|
63
63
|
end
|
@@ -68,7 +68,7 @@ shared_examples 'checking the uniqueness properly' do
|
|
68
68
|
pass_args.each do |pass_arg_group|
|
69
69
|
service = service_class.new
|
70
70
|
expect(service).to receive(:do_work)
|
71
|
-
expect(service_class).to_not receive(:
|
71
|
+
expect(service_class).to_not receive(:call_in)
|
72
72
|
expect { service.call(*pass_arg_group, 'reschedule', false) }.to_not raise_error
|
73
73
|
end
|
74
74
|
end
|
data/spec/support/helpers.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: services
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 5.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Manuel Meurer
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-11-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|