concurrent_rails 0.1.3 → 0.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: afa4c6f568f01ea4ada14fabc78e2e47b7c9a8c3f7ddaa3f3cbc8d3c39dad949
4
- data.tar.gz: cc4165e43a1a6a48fc0dd2bb5b6154257af0ab4d8c32a3620c61b6425e3f32c1
3
+ metadata.gz: 257bcdcbd99076d7b73c193297ce5bc410c1fd5962bb823b965b5abbdf21197c
4
+ data.tar.gz: 5ae2230ba4416193fac70bcd9500673e7d96081491786717eb8d6ce95c938f92
5
5
  SHA512:
6
- metadata.gz: f68e51f1d5fdc986e4fe70a16beac361f77334727da261ae545dc725f2d9424c9d9b0df7755e7b1e8fb22b010d03db253e1c5206217e13640b246c60c9330177
7
- data.tar.gz: d20462ad8cd6a4b72c86e36654441b53b614e0c98684f4c087d5b7fff251e7dbe8f2e4c1627df623d2e332da8c6017b941d0d922160f78c27023ed942f3adf4f
6
+ metadata.gz: df4405c6f8e8efe155dea24db8a3799955b59c4d3a93118153b557711941c66428027caea64d245df4437be49d3d0482ffb2461bad863b38877e152f57eb6970
7
+ data.tar.gz: 8c11ffcd0afbeedf533a8da19ca19f79a1e2566e326946f55e699337f45c179a2f245f5b4fd46f62b694a9803ac317cf0bfb6dec95c38285cf3487c5631a4457
data/README.md CHANGED
@@ -9,7 +9,7 @@ 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
 
@@ -17,26 +17,26 @@ This library provides three classes that will help you run tasks in parallel: `C
17
17
  Similar to other classes, all you have to do is call `.future` helper and pass a block:
18
18
 
19
19
  ```ruby
20
- irb(main):001:0> future = ConcurrentRails::Promises.future { sleep(5); 42 }
21
- => #<ConcurrentRails::Promises:0x00007fe92ea9ff28 @future_instance=#<Con...
20
+ irb(main):001:0> future = ConcurrentRails::Promises.future(5) { |v| sleep(v); 42 }
21
+ => #<ConcurrentRails::Promises:0x00007fed68db66b0 @future_instance=#<Concurrent::Promises::Future
22
22
 
23
- irb(main):003:0> future.state
23
+ irb(main):002:0> future.state
24
24
  => :pending
25
25
 
26
- # After future is processed
27
- irb(main):004:0> future.state
26
+ # After the process slept for 5 seconds
27
+ irb(main):003:0> future.state
28
28
  => :fulfilled
29
29
 
30
- irb(main):005:0> future.value
30
+ irb(main):004:0> future.value
31
31
  => 42
32
32
  ```
33
33
 
34
34
  The benefit of `Promises` over a pure `Future` class is that you can chain futures without blocking the main thread.
35
35
 
36
36
  ```ruby
37
- irb(main):006:0> future = ConcurrentRails::Promises.future { 42 }.then { |v| v * 2 }
37
+ irb(main):001:0> future = ConcurrentRails::Promises.future { 42 }.then { |v| v * 2 }
38
38
  => #<ConcurrentRails::Promises:0x00007fe92eba3460 @future_instance=#...
39
- irb(main):007:0> future.value
39
+ irb(main):002:0> future.value
40
40
  => 84
41
41
  ```
42
42
 
@@ -143,7 +143,7 @@ For more information on how Futures work and how Rails handle multithread check
143
143
  Add this line to your application's Gemfile:
144
144
 
145
145
  ```ruby
146
- gem 'concurrent_rails', '~> 0.1.3'
146
+ gem 'concurrent_rails', '~> 0.1.8'
147
147
  ```
148
148
 
149
149
  And then execute:
@@ -1,10 +1,10 @@
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'
6
+ require 'concurrent_rails/railtie'
7
+ require 'concurrent_rails/version'
8
8
 
9
9
  module ConcurrentRails
10
10
  end
@@ -4,8 +4,7 @@ module ConcurrentRails
4
4
  class Future
5
5
  extend Forwardable
6
6
 
7
- def initialize(executor: :io, &block)
8
- @task = block
7
+ def initialize(executor: :fast, &block)
9
8
  @executor = executor
10
9
  @future = run_on_rails(block)
11
10
  end
@@ -16,15 +15,13 @@ module ConcurrentRails
16
15
  self
17
16
  end
18
17
 
19
- def value
20
- Rails.application.executor.wrap do
21
- result = nil
22
-
23
- ActiveSupport::Dependencies.interlock.permit_concurrent_loads do
24
- result = future.value
18
+ %i[value value!].each do |method_name|
19
+ define_method method_name do
20
+ rails_wrapped do
21
+ ActiveSupport::Dependencies.interlock.permit_concurrent_loads do
22
+ future.__send__(method_name)
23
+ end
25
24
  end
26
-
27
- result
28
25
  end
29
26
  end
30
27
 
@@ -33,13 +30,17 @@ module ConcurrentRails
33
30
  private
34
31
 
35
32
  def run_on_rails(block)
36
- @future = Rails.application.executor.wrap do
33
+ @future = rails_wrapped do
37
34
  Concurrent::Future.new(executor: executor) do
38
- Rails.application.executor.wrap(&block)
35
+ rails_wrapped(&block)
39
36
  end
40
37
  end
41
38
  end
42
39
 
43
- attr_reader :executor, :task, :future
40
+ def rails_wrapped(&block)
41
+ Rails.application.executor.wrap(&block)
42
+ end
43
+
44
+ attr_reader :executor, :future
44
45
  end
45
46
  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
@@ -2,47 +2,60 @@
2
2
 
3
3
  module ConcurrentRails
4
4
  class Promises
5
- include Concurrent::Promises::FactoryMethods
6
5
  extend Forwardable
6
+ include Concurrent::Promises::FactoryMethods
7
7
 
8
- def self.future(*args, &block)
9
- new.run_on_rails(*args, &block)
10
- end
8
+ class << self
9
+ def future(*args, &task)
10
+ future_on(:fast, *args, &task)
11
+ end
11
12
 
12
- def then(*args, &block)
13
- @future_instance = Rails.application.executor.wrap do
14
- future_instance.then(*args, &block)
13
+ def future_on(executor, *args, &task)
14
+ new(executor).run_on_rails(*args, &task)
15
15
  end
16
+ end
16
17
 
17
- self
18
+ def initialize(executor)
19
+ @executor = executor
18
20
  end
19
21
 
20
- %i[value value!].each do |method_name|
21
- define_method method_name do
22
- Rails.application.executor.wrap do
23
- result = nil
22
+ def run_on_rails(*args, &task)
23
+ @future_instance = rails_wrapped { future_on(executor, *args, &task) }
24
24
 
25
- ActiveSupport::Dependencies.interlock.permit_concurrent_loads do
26
- result = future_instance.__send__(method_name)
27
- end
25
+ self
26
+ end
28
27
 
29
- result
28
+ %i[then chain].each do |chainable|
29
+ define_method(chainable) do |*args, &task|
30
+ method = "#{chainable}_on"
31
+ @future_instance = rails_wrapped do
32
+ future_instance.__send__(method, executor, *args, &task)
30
33
  end
34
+
35
+ self
31
36
  end
32
37
  end
33
38
 
34
- def run_on_rails(*args, &block)
35
- @future_instance = Rails.application.executor.wrap do
36
- future(args, &block)
39
+ %i[value value!].each do |method_name|
40
+ define_method(method_name) do |timeout = nil, timeout_value = nil|
41
+ rails_wrapped do
42
+ ActiveSupport::Dependencies.interlock.permit_concurrent_loads do
43
+ future_instance.__send__(method_name, timeout, timeout_value)
44
+ end
45
+ end
37
46
  end
38
-
39
- self
40
47
  end
41
48
 
42
- def_delegators :@future_instance, :state, :reason, :rejected?, :complete?
49
+ %i[state reason rejected? resolved? fulfilled?].each do |delegatable|
50
+ def_delegator :@future_instance, delegatable
51
+ end
43
52
 
44
53
  private
45
54
 
46
- attr_reader :task, :future_instance
55
+ def rails_wrapped(&block)
56
+ Rails.application.executor.wrap(&block)
57
+ end
58
+
59
+ attr_reader :future_instance, :executor
47
60
  end
48
61
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ConcurrentRails
4
- VERSION = '0.1.3'
4
+ VERSION = '0.1.8'
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.3
4
+ version: 0.1.8
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-19 00:00:00.000000000 Z
11
+ date: 2021-05-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -68,14 +68,13 @@ files:
68
68
  - lib/concurrent_rails/promises.rb
69
69
  - lib/concurrent_rails/railtie.rb
70
70
  - lib/concurrent_rails/version.rb
71
- - lib/tasks/concurrent_rails_tasks.rake
72
71
  homepage: https://github.com/luizkowalski/concurrent_rails
73
72
  licenses:
74
73
  - MIT
75
74
  metadata:
76
75
  homepage_uri: https://github.com/luizkowalski/concurrent_rails
77
76
  source_code_uri: https://github.com/luizkowalski/concurrent_rails
78
- changelog_uri: https://github.com/luizkowalski/concurrent_rails/CHANGELOG.md
77
+ changelog_uri: https://github.com/luizkowalski/concurrent_rails/blob/master/CHANGELOG.md
79
78
  post_install_message:
80
79
  rdoc_options: []
81
80
  require_paths:
@@ -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