saviour 0.5.0 → 0.5.1

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: '04095eaf0410d74e00e023a53cc473e8b5f2898f49673a329127d446a87f0080'
4
- data.tar.gz: a3cba3ccb7d59f0f0e219475f003f21f102205132d3bc2cfb9ccc2ca6b0b5c54
3
+ metadata.gz: d30fa955590994c40acee59df17db7840c20d772afd8532e772813f53cdf9588
4
+ data.tar.gz: 37f60dce478723b85d08ba25e971497b9a20829fab51c0d6f46e2422661cfa5e
5
5
  SHA512:
6
- metadata.gz: 83428891e963c4a96bffe2865380bbd586af945029e23d50c86c8b6aacac99f2ee7b928d89383bba0e1845175396e0b6f0c972d18e2df03390579cbf197518f0
7
- data.tar.gz: 3ba8709c6d680f74eb933c3336b3954cd284b8bd52445db2cab72e2c58e1534152667a482b5cab2b5c8cf49a95a63e22f2a0a33e4cebf1becd03f3e1dddc65a6
6
+ metadata.gz: 0055e48b2d02bc40270c32ddf797089c0ef4c7fcfc1d29ecae04b2abd6a72ce952b2faf319bed50619b5497eab337001e950edb2c4b0a06a478dce7fefbc8941
7
+ data.tar.gz: bb9bd3648caaa9fc1c379f374f7c97f5e131916ba3ffa600f691db28ae90853afed59432b86f359b0f1d93064d3efb683f2fe4c7f0f58dc8d118992569d764c6
data/lib/saviour.rb CHANGED
@@ -18,6 +18,8 @@ require 'tempfile'
18
18
  require 'fileutils'
19
19
  require 'concurrent/future'
20
20
 
21
+ require 'active_support/dependencies'
22
+
21
23
  module Saviour
22
24
  NoActiveRecordDetected = Class.new(StandardError)
23
25
  FileNotPresent = Class.new(StandardError)
@@ -115,17 +115,27 @@ module Saviour
115
115
  end.compact
116
116
 
117
117
  pool = Concurrent::FixedThreadPool.new(Saviour::Config.concurrent_workers)
118
- futures = uploaders.map { |uploader| Concurrent::Future.execute(executor: pool) { uploader.upload } }
118
+ futures = uploaders.map { |uploader|
119
+ Concurrent::Future.execute(executor: pool) {
120
+ if defined?(Rails)
121
+ Rails.application.executor.wrap { uploader.upload }
122
+ else
123
+ uploader.upload
124
+ end
125
+ }
126
+ }
127
+
128
+ result = ActiveSupport::Dependencies.interlock.permit_concurrent_loads do
129
+ futures.map do |x|
130
+ x.value.tap do
131
+ raise(x.reason) if x.rejected?
132
+ end
133
+ end.compact
134
+ end
119
135
 
120
136
  pool.shutdown
121
137
  pool.wait_for_termination
122
138
 
123
- result = futures.map do |x|
124
- x.value.tap do
125
- raise(x.reason) if x.rejected?
126
- end
127
- end.compact
128
-
129
139
  attrs = result.to_h
130
140
 
131
141
  uploaders.map(&:uploader).select { |x| x.class.after_upload_hooks.any? }.each do |uploader|
@@ -1,3 +1,3 @@
1
1
  module Saviour
2
- VERSION = "0.5.0"
2
+ VERSION = "0.5.1"
3
3
  end
@@ -10,15 +10,29 @@ describe "concurrent processors" do
10
10
  store_dir { "/store/dir" }
11
11
 
12
12
  process_with_file do |file, filename|
13
+ stash(attached_as => Time.now.to_f)
13
14
  sleep WAIT_TIME
14
15
 
15
16
  [file, filename]
16
17
  end
18
+
19
+ after_upload do |stash|
20
+ model.times = model.times.merge(stash)
21
+ end
17
22
  }
18
23
  }
19
24
 
20
25
  let(:klass) {
21
- klass = Class.new(Test) { include Saviour::Model }
26
+ klass = Class.new(Test) {
27
+ include Saviour::Model
28
+
29
+ attr_accessor :times
30
+
31
+ def initialize(*)
32
+ super
33
+ @times = {}
34
+ end
35
+ }
22
36
  klass.attach_file :file, uploader
23
37
  klass.attach_file :file_thumb, uploader
24
38
  klass.attach_file :file_thumb_2, uploader
@@ -32,15 +46,13 @@ describe "concurrent processors" do
32
46
 
33
47
  Saviour::Config.concurrent_workers = 4
34
48
 
35
- t0 = Time.now
36
49
  a.update_attributes! file: Saviour::StringSource.new("contents", "file.txt"),
37
50
  file_thumb: Saviour::StringSource.new("contents", "file_2.txt"),
38
51
  file_thumb_2: Saviour::StringSource.new("contents", "file_3.txt"),
39
52
  file_thumb_3: Saviour::StringSource.new("contents", "file_4.txt")
40
53
 
41
- diff = Time.now - t0
42
- expect(diff).to be_within(0.05).of(WAIT_TIME)
43
- expect(diff).to be < WAIT_TIME * 4
54
+ start_times = a.times.values.sort
55
+ expect((start_times[0] - start_times[-1]).abs).to be_within(0.1).of(0)
44
56
  end
45
57
 
46
58
  it 'works in serial with 1 worker' do
@@ -48,13 +60,13 @@ describe "concurrent processors" do
48
60
 
49
61
  Saviour::Config.concurrent_workers = 1
50
62
 
51
- t0 = Time.now
52
63
  a.update_attributes! file: Saviour::StringSource.new("contents", "file.txt"),
53
64
  file_thumb: Saviour::StringSource.new("contents", "file_2.txt"),
54
65
  file_thumb_2: Saviour::StringSource.new("contents", "file_3.txt"),
55
66
  file_thumb_3: Saviour::StringSource.new("contents", "file_4.txt")
56
67
 
57
- expect(Time.now - t0).to be >= WAIT_TIME * 4
68
+ start_times = a.times.values.sort
69
+ expect((start_times[0] - start_times[-1]).abs).to be > WAIT_TIME * 3
58
70
  end
59
71
 
60
72
  it 'concurrency can be adjusted' do
@@ -62,15 +74,15 @@ describe "concurrent processors" do
62
74
 
63
75
  Saviour::Config.concurrent_workers = 2
64
76
 
65
- t0 = Time.now
66
77
  a.update_attributes! file: Saviour::StringSource.new("contents", "file.txt"),
67
78
  file_thumb: Saviour::StringSource.new("contents", "file_2.txt"),
68
79
  file_thumb_2: Saviour::StringSource.new("contents", "file_3.txt"),
69
80
  file_thumb_3: Saviour::StringSource.new("contents", "file_4.txt")
70
81
 
71
- diff = Time.now - t0
72
- expect(diff).to be_within(0.05).of(WAIT_TIME * 2)
73
- expect(diff).to be < WAIT_TIME * 4
82
+ start_times = a.times.values.sort
83
+ expect((start_times[0] - start_times[1]).abs).to be_within(0.1).of(0)
84
+ expect((start_times[2] - start_times[3]).abs).to be_within(0.1).of(0)
85
+ expect((start_times[0] - start_times[-1]).abs).to be > WAIT_TIME
74
86
  end
75
87
  end
76
88
 
@@ -78,41 +90,39 @@ describe "concurrent processors" do
78
90
  it 'works concurrently with 4 workers' do
79
91
  Saviour::Config.concurrent_workers = 4
80
92
 
81
- t0 = Time.now
82
- klass.create! file: Saviour::StringSource.new("contents", "file.txt"),
83
- file_thumb: Saviour::StringSource.new("contents", "file_2.txt"),
84
- file_thumb_2: Saviour::StringSource.new("contents", "file_3.txt"),
85
- file_thumb_3: Saviour::StringSource.new("contents", "file_4.txt")
93
+ a = klass.create! file: Saviour::StringSource.new("contents", "file.txt"),
94
+ file_thumb: Saviour::StringSource.new("contents", "file_2.txt"),
95
+ file_thumb_2: Saviour::StringSource.new("contents", "file_3.txt"),
96
+ file_thumb_3: Saviour::StringSource.new("contents", "file_4.txt")
86
97
 
87
- diff = Time.now - t0
88
- expect(diff).to be_within(0.05).of(WAIT_TIME)
89
- expect(diff).to be < WAIT_TIME * 4
98
+ start_times = a.times.values.sort
99
+ expect((start_times[0] - start_times[-1]).abs).to be_within(0.1).of(0)
90
100
  end
91
101
 
92
102
  it 'works in serial with 1 worker' do
93
103
  Saviour::Config.concurrent_workers = 1
94
104
 
95
- t0 = Time.now
96
- klass.create! file: Saviour::StringSource.new("contents", "file.txt"),
97
- file_thumb: Saviour::StringSource.new("contents", "file_2.txt"),
98
- file_thumb_2: Saviour::StringSource.new("contents", "file_3.txt"),
99
- file_thumb_3: Saviour::StringSource.new("contents", "file_4.txt")
105
+ a = klass.create! file: Saviour::StringSource.new("contents", "file.txt"),
106
+ file_thumb: Saviour::StringSource.new("contents", "file_2.txt"),
107
+ file_thumb_2: Saviour::StringSource.new("contents", "file_3.txt"),
108
+ file_thumb_3: Saviour::StringSource.new("contents", "file_4.txt")
100
109
 
101
- expect(Time.now - t0).to be >= WAIT_TIME * 4
110
+ start_times = a.times.values.sort
111
+ expect((start_times[0] - start_times[-1]).abs).to be > WAIT_TIME * 3
102
112
  end
103
113
 
104
114
  it 'concurrency can be adjusted' do
105
115
  Saviour::Config.concurrent_workers = 2
106
116
 
107
- t0 = Time.now
108
- klass.create! file: Saviour::StringSource.new("contents", "file.txt"),
109
- file_thumb: Saviour::StringSource.new("contents", "file_2.txt"),
110
- file_thumb_2: Saviour::StringSource.new("contents", "file_3.txt"),
111
- file_thumb_3: Saviour::StringSource.new("contents", "file_4.txt")
117
+ a = klass.create! file: Saviour::StringSource.new("contents", "file.txt"),
118
+ file_thumb: Saviour::StringSource.new("contents", "file_2.txt"),
119
+ file_thumb_2: Saviour::StringSource.new("contents", "file_3.txt"),
120
+ file_thumb_3: Saviour::StringSource.new("contents", "file_4.txt")
112
121
 
113
- diff = Time.now - t0
114
- expect(diff).to be_within(0.05).of(WAIT_TIME * 2)
115
- expect(diff).to be < WAIT_TIME * 4
122
+ start_times = a.times.values.sort
123
+ expect((start_times[0] - start_times[1]).abs).to be_within(0.1).of(0)
124
+ expect((start_times[2] - start_times[3]).abs).to be_within(0.1).of(0)
125
+ expect((start_times[0] - start_times[-1]).abs).to be > WAIT_TIME
116
126
  end
117
127
  end
118
128
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: saviour
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Roger Campos
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-03-07 00:00:00.000000000 Z
11
+ date: 2018-03-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord