attache 1.1.3 → 1.1.4

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
  SHA1:
3
- metadata.gz: 142d8bbaf242702c8f4ffc3dd138981bf5e36515
4
- data.tar.gz: 489c6110a4ccf9d646537d0e29ae3be1febd9d71
3
+ metadata.gz: 637b0e3db30b5d851946c433e8df383d38708d39
4
+ data.tar.gz: 9103c50678a077092a9c43a10bab435e425e86ac
5
5
  SHA512:
6
- metadata.gz: 42ca943075fad7ef4c8897f617bfec5c758687d45b31c50ee6d841459b6bc53351bb47c3a62d5614041de7a85158b07e9723de3196ea06b46fac10dae5a61ef6
7
- data.tar.gz: bdc7479ebc633564b8ab06208f4bdafa74ca4cd6631e83f9d08daeb5061f147c0ea34c864048fa95d557b151c1f0a2818153e5564cde31c62e789190c5ab4f97
6
+ metadata.gz: ca9c7794c0dac8a080ec267d849c3d0a786fb1b60c3041b80548c37115ac2af33d57963d29f2c4634aa23647252ce5eaa9561b4a63b52b068033f82200a41eb1
7
+ data.tar.gz: 980b910c931c32d814d7c757fe7e74a0b1ee376a4e657c988ab531866f52e2029279b0961fe24c6b08423b43ef915d912aff9666048cbf78dd1f7894c7500d7b
data/README.md CHANGED
@@ -25,6 +25,7 @@ You can checkout the source code and run it like a regular [a Procfile-based app
25
25
  ```
26
26
  git clone https://github.com/choonkeat/attache.git
27
27
  cd attache
28
+ bundle install
28
29
  foreman start -c web=1 -p 9292
29
30
  ```
30
31
 
@@ -27,19 +27,18 @@ class Attache::Download < Attache::Base
27
27
  file = begin
28
28
  cachekey = File.join(request_hostname(env), relpath)
29
29
  Attache.cache.fetch(cachekey) do
30
- get_first_result_async(vhosts.inject({}) {|sum,(k,v)|
31
- if v
32
- sum.merge("#{k} #{relpath}" => lambda {
33
- begin
34
- v.storage_get(relpath: relpath)
35
- rescue Exception
36
- Attache.logger.info "[POOL] not found #{k} #{relpath}"
37
- nil
30
+ name_with_vhost_pairs = vhosts.inject({}) { |sum,(k,v)| (v ? sum.merge(k => v) : sum) }
31
+ get_first_result_present_async(name_with_vhost_pairs.collect {|name, vhost|
32
+ lambda { Thread.handle_interrupt(BasicObject => :on_blocking) {
33
+ begin
34
+ vhost.storage_get(relpath: relpath).tap do |v|
35
+ Attache.logger.info "[POOL] found #{name} #{relpath} = #{v.inspect}"
38
36
  end
39
- })
40
- else
41
- sum
42
- end
37
+ rescue Exception
38
+ Attache.logger.info "[POOL] not found #{name} #{relpath}"
39
+ nil
40
+ end
41
+ } }
43
42
  })
44
43
  end
45
44
  rescue Exception # Errno::ECONNREFUSED, OpenURI::HTTPError, Excon::Errors, Fog::Errors::Error
@@ -93,24 +92,16 @@ class Attache::Download < Attache::Base
93
92
  end
94
93
  end
95
94
 
96
- def get_first_result_async(name_code_pairs)
97
- result = nil
98
- threads = name_code_pairs.collect {|name, code|
99
- Thread.new do
100
- Thread.handle_interrupt(BasicObject => :on_blocking) { # if killed
101
- if result
102
- # war over
103
- elsif current_result = code.call
104
- result = current_result
105
- (threads - [Thread.current]).each(&:kill) # kill siblings
106
- Attache.logger.info "[POOL] found #{name.inspect}"
107
- else
108
- # no contribution
109
- end
110
- }
111
- end
112
- }
113
- threads.each(&:join)
95
+ # Ref https://gist.github.com/sferik/39831f34eb87686b639c#gistcomment-1652888
96
+ # a bit more complicated because we *want* to ignore falsey result
97
+ def get_first_result_present_async(lambdas)
98
+ return if lambdas.empty? # queue.pop will never happen
99
+ queue = Queue.new
100
+ threads = lambdas.collect { |code| Thread.new { queue << code.call } }
101
+ until result = queue.pop do
102
+ break unless threads.any?(&:alive?) || queue.size > 0
103
+ end
104
+ threads.each(&:kill)
114
105
  result
115
106
  end
116
107
  end
@@ -1,3 +1,3 @@
1
1
  module Attache
2
- VERSION = "1.1.3"
2
+ VERSION = "1.1.4"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: attache
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.3
4
+ version: 1.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - choonkeat