blat 0.1.0b → 0.1.0c

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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/blat.rb +1 -1
  3. data/lib/blat/queue.rb +45 -7
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a18d2f9b96d5a2d9b6e7e8ae085e1bfed1855d32
4
- data.tar.gz: df5cc403ee46326005387f73432d73edb0fb69bf
3
+ metadata.gz: edc10068370f1a5d73ed06c92ef162dbecdefaa6
4
+ data.tar.gz: 69b155992407e5c2bf4facebb077a89d14272229
5
5
  SHA512:
6
- metadata.gz: f324fa27a4c93dd576a2663b87a047c3f0c74cf2387ad8fdf362eb4cf884b156a09232421101a7ca6cd947c03ad3d7fe5870ff6a1d7a95897a107fd31ca66fa7
7
- data.tar.gz: cf89d6d6a43935fbd03dd811a770ff53ba091318cb89ee88037b9017efaed91e178dd1dcd8387624383b0d476b35f9bf9f5dc22d8fe3046876b7e07362d4c51b
6
+ metadata.gz: 40688b033ed506bde40d2606d678fdcf0522c1b84060a516b0a2805e4816060c8eca618e38434f716eb21b826bcc1b511d699e45611565ae18378c3c9bec3197
7
+ data.tar.gz: 281605fdb68e87329f7009de13a27af4388c2f6700471a96de2a91ea9f906c3565ef6135fd155dde76cbc4624e58359b1af0206ac1bed81a4b2dba9abcf96d21
@@ -6,6 +6,6 @@ require 'blat/queue'
6
6
  # aggressively as possible.
7
7
  module Blat
8
8
 
9
- VERSION = '0.1.0b'
9
+ VERSION = '0.1.0c'
10
10
 
11
11
  end
@@ -27,6 +27,10 @@ module Blat
27
27
  @pipeline = (pipeline == true)
28
28
  @multi.max_connects = @max_connects
29
29
  @multi.pipeline = @pipeline
30
+
31
+ # Keep track of activity
32
+ @active = false
33
+ @activity_mx = Mutex.new
30
34
  end
31
35
 
32
36
  # Add a URL or a Curl::Easy object to the queue.
@@ -50,12 +54,19 @@ module Blat
50
54
  return curl
51
55
  end
52
56
 
57
+ # Cancel all requests currently queued or downloading
58
+ def cancel
59
+ @multi.cancel!
60
+ end
61
+
62
+ alias_method :cancel!, :cancel
63
+
53
64
  # Returns the number of active requests
54
65
  def request_count
55
66
  requests.length
56
67
  end
57
68
 
58
- # Returns a list of active requests
69
+ # Returns a list of active requests (Curl::Easy objects)
59
70
  def requests
60
71
  @multi.requests
61
72
  end
@@ -67,16 +78,43 @@ module Blat
67
78
  @multi.remove(curl)
68
79
  end
69
80
 
70
- # Wait for all requests to finish (blocking).
81
+ # Run the queue, waiting for requests to finish (blocking).
71
82
  #
72
- # If a block is given it is executed repeatedly whilst waiting.
73
- def wait(&block)
83
+ # If a block is given it is executed repeatedly whilst waiting, e.g.
84
+ #
85
+ # q.perform {
86
+ # puts "Active downloads: #{q.request_count}"
87
+ # }
88
+ #
89
+ def perform(&block)
90
+ raise 'Already actively performing requests' if active?
91
+
92
+ @activity_mx.synchronize { @active = true }
74
93
  @multi.perform do
75
94
  yield if block_given?
76
95
  end
96
+ ensure
97
+ @activity_mx.synchronize { @active = false }
98
+ end
99
+
100
+ # Perform downloads in a nonblocking manner
101
+ #
102
+ # Optionally run the block given, as with regular #perform
103
+ def perform_nonblock(&block)
104
+ raise 'Currently active' if @thread
105
+
106
+ me = self
107
+ @thread = Thread.new() do
108
+ me.perform { yield if block_given? }
109
+ end
110
+ @thread.abort_on_exception = true
111
+
77
112
  end
78
113
 
79
- alias_method :perform, :wait
114
+ # Is this object currently actively downloading data?
115
+ def active?
116
+ @activity_mx.synchronize { @active }
117
+ end
80
118
 
81
119
  # Is the queue idle?
82
120
  def idle?
@@ -113,7 +151,7 @@ module Blat
113
151
  # end
114
152
  #
115
153
  def consume(connections = @max_connects, &block)
116
- @multi.perform do
154
+ perform do
117
155
  while request_count < connections && new_link = yield
118
156
  add(new_link) if new_link
119
157
  end
@@ -132,7 +170,7 @@ module Blat
132
170
  item = 0 # Start at item 0
133
171
  list = list.to_a # Ensure we can address with []
134
172
 
135
- @multi.perform do
173
+ perform do
136
174
  while request_count < connections && new_link = list[item]
137
175
 
138
176
  item += 1
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: blat
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0b
4
+ version: 0.1.0c
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stephen Wattam