concurrent_rails 0.1.4 → 0.2.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
  SHA256:
3
- metadata.gz: 9c9f6c5865a512a981fb3be8f87dadffbc2751c0bb8937b4929af57b3fa691a7
4
- data.tar.gz: 40e7a30066f9cb04a4093fef697e0e36733d6ca9c7302cbe4b3af4ad7d7e29e7
3
+ metadata.gz: 14fd5aa53c8cb75c74999acd7e1c43410a9564ba4122d83142f0e3ed8565d429
4
+ data.tar.gz: 71e60b4e355052fcc328ada6b881b9e41330360aaaa582eff9afb90cab19866f
5
5
  SHA512:
6
- metadata.gz: 31ba76250826eab804ac7941e6bd33bf9887d367bcf4dcd1b7dca306b3c44287ca1d5c9c75396e7ed8b88168675251ca8df597b4b175c1aa5cee1287fbbb9181
7
- data.tar.gz: 3beebfc9b8dc305b555dca571570737247c0409dd67976d0a526d6f37ae64531c2fedafb4de62ef3edb90e867db3ace8131abfa152998b1ff047534c62d7d251
6
+ metadata.gz: 020b3dee7781090195212145f6faf7f19c311cdee6b619e0601dde476fd3dd2dbe811269da5362794d5903f56ca95eaf56e615dcadb55db454d2a705b6599837
7
+ data.tar.gz: 9abb0931cb4283b4c9947d7498784c3d0ca0c32e70fb0fbaff9aa29ad28cde0c97ba48bde5f2d07837902cfcb912f987f449b9825658f47f93a7ef1f48bacf32
data/README.md CHANGED
@@ -9,12 +9,11 @@ The goal of this gem is to provide a simple library that allows the developer to
9
9
 
10
10
  ## Usage
11
11
 
12
- This library provides three classes that will help you run tasks in parallel: `ConcurrentRails::Promises`, `ConcurrentRails::Future` and `ConcurrentRails::Multi`
12
+ This library provides three classes that will help you run tasks in parallel: `ConcurrentRails::Promises`, `ConcurrentRails::Future` ([in process of being deprecated by concurrent-ruby](https://github.com/ruby-concurrency/concurrent-ruby#deprecated)) and `ConcurrentRails::Multi`
13
13
 
14
14
  ### Promises
15
15
 
16
- `Promises` is the recommended way from `concurrent-ruby` to create `Future`s as `Concurrent::Future` will be deprecated at some point.
17
- Similar to other classes, all you have to do is call `.future` helper and pass a block:
16
+ `Promises` is the recommended way from `concurrent-ruby` to create `Future`s as `Concurrent::Future` will be deprecated at some point. All you have to do is call `#future` and pass a block to be executed asynchronously:
18
17
 
19
18
  ```ruby
20
19
  irb(main):001:0> future = ConcurrentRails::Promises.future(5) { |v| sleep(v); 42 }
@@ -40,7 +39,50 @@ irb(main):002:0> future.value
40
39
  => 84
41
40
  ```
42
41
 
43
- ### Future
42
+ ### Delayed futures
43
+
44
+ Delayed future is a future that is enqueued but not run until `#touch` or any other method that requires a resolution is called.
45
+
46
+ ```ruby
47
+ irb(main):002:0> delay = ConcurrentRails::Promises.delay { 42 }
48
+ => #<ConcurrentRails::Promises:0x00007f8b55333d48 @executor=:io, @instan...
49
+
50
+ irb(main):003:0> delay.state
51
+ => :pending
52
+
53
+ irb(main):004:0> delay.touch
54
+ => #<Concurrent::Promises::Future:0x00007f8b553325b0 pending>
55
+
56
+ irb(main):005:0> delay.state
57
+ => :fulfilled
58
+
59
+ irb(main):006:0> delay.value
60
+ => 42
61
+ ```
62
+
63
+ Three methods will trigger a resolution: `#touch`, `#value` and `#wait`: `#touch` will simply trigger the execution but won't block the main thread, while `#wait` and `#value` will block the main thread until a resolution is given.
64
+
65
+ ### Callbacks
66
+
67
+ Delayed and regular futures can set a callback to be executed after the resolution of the future. There are three different callbacks:
68
+
69
+ * `on_resolution`: runs after the future is resolved and yields three parameters to the callback in the following order: `true/false` for future's fulfillment, `value` as the result of the future execution, and `reason`, that will be `nil` if the future fulfilled or the error that the future triggered.
70
+
71
+ * `on_fulfillment`: runs after the future is fulfilled and yields `value` to the callback
72
+
73
+ * `on_rejection`: runs after the future is rejected and yields the `error` to the callback
74
+
75
+ ```ruby
76
+ delay = ConcurrentRails::Promises.delay { complex_find_user_query }.
77
+ on_fulfillment { |user| user.update!(name: 'John Doe') }.
78
+ on_rejection { |reason| log_error(reason) }
79
+
80
+ delay.touch
81
+ ```
82
+
83
+ All of these callbacks have a bang version (e.g. `on_fulfillment!`). The bang version will execute the callback on the same thread pool that was initially set up and the version without bang will run asynchronously on a different executor.
84
+
85
+ ### (Deprecated) Future
44
86
 
45
87
  `ConcurrentRails::Future` will execute your code in a separated thread and you can check the progress of it whenever you need it. When the task is ready, you can access the result with `#result` function:
46
88
 
@@ -81,7 +123,7 @@ irb(main):005:0> future.reason
81
123
  => #<ZeroDivisionError: divided by 0>
82
124
  ```
83
125
 
84
- ### Multi
126
+ ### (Deprecated) Multi
85
127
 
86
128
  `ConcurrentRails::Multi` will let you execute multiple tasks in parallel and aggregate the results of each task when they are done. `Multi` accepts an undefined number of `Proc`s.
87
129
 
@@ -143,7 +185,7 @@ For more information on how Futures work and how Rails handle multithread check
143
185
  Add this line to your application's Gemfile:
144
186
 
145
187
  ```ruby
146
- gem 'concurrent_rails', '~> 0.1.3'
188
+ gem 'concurrent_rails', '~> 0.2.0'
147
189
  ```
148
190
 
149
191
  And then execute:
@@ -1,10 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'concurrent_rails/version'
4
- require 'concurrent_rails/railtie'
5
3
  require 'concurrent_rails/future'
6
4
  require 'concurrent_rails/multi'
7
5
  require 'concurrent_rails/promises'
8
-
9
- module ConcurrentRails
10
- end
6
+ require 'concurrent_rails/railtie'
7
+ require 'concurrent_rails/version'
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConcurrentRails::Adapters
4
+ module Delay
5
+ extend ActiveSupport::Concern
6
+
7
+ class_methods do
8
+ def delay(*args, &task)
9
+ delay_on(:io, *args, &task)
10
+ end
11
+
12
+ def delay_on(executor, *args, &task)
13
+ new(executor).delay_on_rails(*args, &task)
14
+ end
15
+ end
16
+
17
+ def delay_on_rails(*args, &task)
18
+ @instance = rails_wrapped { delay_on(executor, *args, &task) }
19
+
20
+ self
21
+ end
22
+
23
+ delegate :touch, to: :instance
24
+ end
25
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConcurrentRails::Adapters
4
+ module Future
5
+ extend ActiveSupport::Concern
6
+
7
+ class_methods do
8
+ def future(*args, &task)
9
+ future_on(:io, *args, &task)
10
+ end
11
+
12
+ def future_on(executor, *args, &task)
13
+ new(executor).future_on_rails(*args, &task)
14
+ end
15
+ end
16
+
17
+ def future_on_rails(*args, &task)
18
+ @instance = rails_wrapped { future_on(executor, *args, &task) }
19
+
20
+ self
21
+ end
22
+ end
23
+ end
@@ -2,11 +2,10 @@
2
2
 
3
3
  module ConcurrentRails
4
4
  class Future
5
- extend Forwardable
6
-
7
5
  def initialize(executor: :io, &block)
8
6
  @executor = executor
9
7
  @future = run_on_rails(block)
8
+ # ActiveSupport::Deprecation.warn('Concurrent::Future is deprecated')
10
9
  end
11
10
 
12
11
  def execute
@@ -15,30 +14,32 @@ module ConcurrentRails
15
14
  self
16
15
  end
17
16
 
18
- def value
19
- Rails.application.executor.wrap do
20
- result = nil
21
-
22
- ActiveSupport::Dependencies.interlock.permit_concurrent_loads do
23
- result = future.value
17
+ %i[value value!].each do |method_name|
18
+ define_method method_name do
19
+ rails_wrapped do
20
+ ActiveSupport::Dependencies.interlock.permit_concurrent_loads do
21
+ future.__send__(method_name)
22
+ end
24
23
  end
25
-
26
- result
27
24
  end
28
25
  end
29
26
 
30
- def_delegators :@future, :state, :reason, :rejected?, :complete?, :add_observer
27
+ delegate :state, :reason, :rejected?, :complete?, :add_observer, to: :future
31
28
 
32
29
  private
33
30
 
34
31
  def run_on_rails(block)
35
- @future = Rails.application.executor.wrap do
32
+ @future = rails_wrapped do
36
33
  Concurrent::Future.new(executor: executor) do
37
- Rails.application.executor.wrap(&block)
34
+ rails_wrapped(&block)
38
35
  end
39
36
  end
40
37
  end
41
38
 
39
+ def rails_wrapped(&block)
40
+ Rails.application.executor.wrap(&block)
41
+ end
42
+
42
43
  attr_reader :executor, :future
43
44
  end
44
45
  end
@@ -30,6 +30,10 @@ module ConcurrentRails
30
30
  futures.map(&:value)
31
31
  end
32
32
 
33
+ def compute!
34
+ futures.map(&:value!)
35
+ end
36
+
33
37
  def complete?
34
38
  futures.all?(&:complete?)
35
39
  end
@@ -1,48 +1,70 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'concurrent_rails/adapters/future'
4
+ require 'concurrent_rails/adapters/delay'
5
+
3
6
  module ConcurrentRails
4
7
  class Promises
5
8
  include Concurrent::Promises::FactoryMethods
6
- extend Forwardable
9
+ include ConcurrentRails::Adapters::Delay
10
+ include ConcurrentRails::Adapters::Future
7
11
 
8
- def self.future(*args, &task)
9
- new.run_on_rails(*args, &task)
12
+ def initialize(executor)
13
+ @executor = executor
10
14
  end
11
15
 
12
- def then(*args, &task)
13
- @future_instance = Rails.application.executor.wrap do
14
- future_instance.then(*args, &task)
16
+ %i[value value!].each do |method_name|
17
+ define_method(method_name) do |timeout = nil, timeout_value = nil|
18
+ permit_concurrent_loads do
19
+ instance.__send__(method_name, timeout, timeout_value)
20
+ end
15
21
  end
16
-
17
- self
18
22
  end
19
23
 
20
- %i[value value!].each do |method_name|
21
- define_method method_name do
22
- Rails.application.executor.wrap do
23
- result = nil
24
-
25
- ActiveSupport::Dependencies.interlock.permit_concurrent_loads do
26
- result = future_instance.__send__(method_name)
27
- end
28
-
29
- result
24
+ %i[then chain].each do |chainable|
25
+ define_method(chainable) do |*args, &task|
26
+ method = "#{chainable}_on"
27
+ @instance = rails_wrapped do
28
+ instance.__send__(method, executor, *args, &task)
30
29
  end
30
+
31
+ self
31
32
  end
32
33
  end
33
34
 
34
- def run_on_rails(*args, &task)
35
- @future_instance = Rails.application.executor.wrap do
36
- future_on(default_executor, *args, &task)
35
+ %i[on_fulfillment on_rejection on_resolution].each do |method|
36
+ define_method(method) do |*args, &callback_task|
37
+ rails_wrapped do
38
+ @instance = instance.__send__("#{method}_using", executor, *args, &callback_task)
39
+ end
40
+
41
+ self
37
42
  end
38
43
 
39
- self
44
+ define_method("#{method}!") do |*args, &callback_task|
45
+ rails_wrapped do
46
+ @instance = instance.__send__(:add_callback, "callback_#{method}", args, callback_task)
47
+ end
48
+
49
+ self
50
+ end
40
51
  end
41
52
 
42
- def_delegators :@future_instance, :state, :reason, :rejected?, :complete?
53
+ delegate :state, :reason, :rejected?, :resolved?, :fulfilled?, :wait,
54
+ to: :instance
43
55
 
44
56
  private
45
57
 
46
- attr_reader :future_instance
58
+ def rails_wrapped(&block)
59
+ Rails.application.executor.wrap(&block)
60
+ end
61
+
62
+ def permit_concurrent_loads(&block)
63
+ rails_wrapped do
64
+ ActiveSupport::Dependencies.interlock.permit_concurrent_loads(&block)
65
+ end
66
+ end
67
+
68
+ attr_reader :executor, :instance
47
69
  end
48
70
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ConcurrentRails
4
- VERSION = '0.1.4'
4
+ VERSION = '0.2.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: concurrent_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Luiz Eduardo Kowalski
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-04-20 00:00:00.000000000 Z
11
+ date: 2021-05-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '1.12'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rubocop-minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0.12'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0.12'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: rubocop-performance
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -52,6 +66,20 @@ dependencies:
52
66
  - - ">="
53
67
  - !ruby/object:Gem::Version
54
68
  version: '1.10'
69
+ - !ruby/object:Gem::Dependency
70
+ name: sqlite3
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.4'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.4'
55
83
  description: Small library to make concurrent-ruby and Rails play nice together
56
84
  email:
57
85
  - luizeduardokowalski@gmail.com
@@ -63,19 +91,20 @@ files:
63
91
  - README.md
64
92
  - Rakefile
65
93
  - lib/concurrent_rails.rb
94
+ - lib/concurrent_rails/adapters/delay.rb
95
+ - lib/concurrent_rails/adapters/future.rb
66
96
  - lib/concurrent_rails/future.rb
67
97
  - lib/concurrent_rails/multi.rb
68
98
  - lib/concurrent_rails/promises.rb
69
99
  - lib/concurrent_rails/railtie.rb
70
100
  - lib/concurrent_rails/version.rb
71
- - lib/tasks/concurrent_rails_tasks.rake
72
101
  homepage: https://github.com/luizkowalski/concurrent_rails
73
102
  licenses:
74
103
  - MIT
75
104
  metadata:
76
105
  homepage_uri: https://github.com/luizkowalski/concurrent_rails
77
106
  source_code_uri: https://github.com/luizkowalski/concurrent_rails
78
- changelog_uri: https://github.com/luizkowalski/concurrent_rails/CHANGELOG.md
107
+ changelog_uri: https://github.com/luizkowalski/concurrent_rails/blob/master/CHANGELOG.md
79
108
  post_install_message:
80
109
  rdoc_options: []
81
110
  require_paths:
@@ -91,7 +120,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
91
120
  - !ruby/object:Gem::Version
92
121
  version: '0'
93
122
  requirements: []
94
- rubygems_version: 3.1.6
123
+ rubygems_version: 3.2.17
95
124
  signing_key:
96
125
  specification_version: 4
97
126
  summary: Multithread is hard
@@ -1,5 +0,0 @@
1
- # frozen_string_literal: true
2
- # desc "Explaining what the task does"
3
- # task :concurrent_rails do
4
- # # Task goes here
5
- # end