blat 0.1.0b → 0.1.0c
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/blat.rb +1 -1
- data/lib/blat/queue.rb +45 -7
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: edc10068370f1a5d73ed06c92ef162dbecdefaa6
|
4
|
+
data.tar.gz: 69b155992407e5c2bf4facebb077a89d14272229
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 40688b033ed506bde40d2606d678fdcf0522c1b84060a516b0a2805e4816060c8eca618e38434f716eb21b826bcc1b511d699e45611565ae18378c3c9bec3197
|
7
|
+
data.tar.gz: 281605fdb68e87329f7009de13a27af4388c2f6700471a96de2a91ea9f906c3565ef6135fd155dde76cbc4624e58359b1af0206ac1bed81a4b2dba9abcf96d21
|
data/lib/blat.rb
CHANGED
data/lib/blat/queue.rb
CHANGED
@@ -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
|
-
#
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
173
|
+
perform do
|
136
174
|
while request_count < connections && new_link = list[item]
|
137
175
|
|
138
176
|
item += 1
|