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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5bd1a27e2051a3dfce7845c6d24396c007f17c51
4
- data.tar.gz: 55072f152a1356ce1f6be21d3b60ec92ba45ac78
3
+ metadata.gz: 7a54c2710e578f7af30ef239cd96b7f837630a02
4
+ data.tar.gz: a33159048724d2925ca48fb06acd92a67402833e
5
5
  SHA512:
6
- metadata.gz: 5fba150903e4924d507e1f13526578e47f6b0164d170d89d6c6f7f96564d657ab2bc1dc5901d7a2e1bbbd9146ddc4de4d9e8d1a5c3f228ca9ca7aed2e864c1e7
7
- data.tar.gz: 4ae154068e6e42f69b79f4b20555f77e73e5499c5e2e072a3e8c77ecb1e6049404b7883486c712351ee534277b68d941de9132f7d5c1e6a001ead12e553dcba6
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.perform_async 1 # with a user ID
130
- Services::Users::Delete.perform_async [1, 2, 3] # with multiple user IDs
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/).
@@ -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 bulk_perform_async(args)
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
- %w(perform_async perform_in).each do |method_name|
33
- define_method method_name do |*args|
34
- self.class.send method_name, *args, TARGET_PARAM_NAME => self.id
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.send *args
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.send :include, Rails.application.routes.url_helpers if defined?(Rails)
13
- subclass.send :include, Asyncable if defined?(Asyncable)
14
- subclass.send :prepend, CallLogger, ExceptionWrapper, UniquenessChecker
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
@@ -14,7 +14,7 @@ module Services
14
14
  [k, v].join('=')
15
15
  end
16
16
  @logger.tagged Time.now, severity.upcase, *tags do
17
- @logger.send severity, message
17
+ @logger.public_send severity, message
18
18
  end
19
19
  end
20
20
  end
@@ -8,7 +8,7 @@ module Services
8
8
  Symbol,
9
9
  String,
10
10
  Numeric
11
- ]
11
+ ].freeze
12
12
 
13
13
  InvalidMetaError = Class.new(StandardError)
14
14
  EmptyResponseFromRedisMultiError = Class.new(StandardError)
@@ -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.perform_in retry_delay, *reschedule_args
98
+ self.class.call_in retry_delay, *reschedule_args
99
99
  end
100
100
 
101
101
  def error_count
@@ -1,3 +1,3 @@
1
1
  module Services
2
- VERSION = '4.3.0'
2
+ VERSION = '5.0.0'.freeze
3
3
  end
@@ -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(:perform_in).with(an_instance_of(Fixnum), *fail_arg_group, 'reschedule', false)
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(:perform_in)
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
@@ -21,7 +21,7 @@ def wait_for_all_jobs_to_finish
21
21
  end
22
22
 
23
23
  def wait_for_job_to_run(job_class, *args, &block)
24
- job_class.perform_async(*args).tap do |jid|
24
+ job_class.call_async(*args).tap do |jid|
25
25
  wait_for { worker_with_jid(jid) }
26
26
  block.call if block_given?
27
27
  end
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.3.0
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-08-13 00:00:00.000000000 Z
11
+ date: 2016-11-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake