baza.rb 0.7.0 → 0.8.0
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 +4 -4
- data/lib/baza-rb/version.rb +1 -1
- data/lib/baza-rb.rb +74 -23
- data/test/test_baza-rb.rb +13 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ebcb57db3953278c8d402cc38ae954e982062cfc4498e301bc0495a7b3cba95a
|
4
|
+
data.tar.gz: 0a6acf7107e149f8515911542460266b95b825df586576713734648751f2de56
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a40b4c3655a48f0449cb0247f1dd1045344b95868330121b6ce462d874aec6117b1490d12819ceac9afc29217e0352d4d30d2c6ff78c1031761946bb3cf89654
|
7
|
+
data.tar.gz: 7a479de17acf3e82f60327fa7c131605bebf2a0ae2dbfd1fde80ac08a3bcfafd2d40b252ef4d23845de083867a5955244d7079ce5526574d94c1fb9814e62cf8
|
data/lib/baza-rb/version.rb
CHANGED
data/lib/baza-rb.rb
CHANGED
@@ -370,28 +370,34 @@ class BazaRb
|
|
370
370
|
raise 'The "file" of the durable is nil' if file.nil?
|
371
371
|
raise "The file '#{file}' is absent" unless File.exist?(file)
|
372
372
|
id = nil
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
'
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
373
|
+
Tempfile.open do |f|
|
374
|
+
File.write(f.path, 'placeholder')
|
375
|
+
elapsed(@loog) do
|
376
|
+
ret =
|
377
|
+
retry_it do
|
378
|
+
checked(
|
379
|
+
Typhoeus::Request.post(
|
380
|
+
home.append('durables').append('place').to_s,
|
381
|
+
body: {
|
382
|
+
'_csrf' => csrf,
|
383
|
+
'jname' => jname,
|
384
|
+
'file' => File.basename(file),
|
385
|
+
'zip' => File.open(f, 'rb')
|
386
|
+
},
|
387
|
+
headers:,
|
388
|
+
connecttimeout: @timeout,
|
389
|
+
timeout: @timeout
|
390
|
+
),
|
391
|
+
302
|
392
|
+
)
|
393
|
+
end
|
394
|
+
id = ret.headers['X-Zerocracy-DurableId'].to_i
|
395
|
+
throw :"Durable ##{id} (#{file}) placed for job \"#{jname}\" at #{@host}"
|
396
|
+
end
|
394
397
|
end
|
398
|
+
durable_lock(id, user_agent)
|
399
|
+
durable_save(id, file)
|
400
|
+
durable_unlock(id, user_agent)
|
395
401
|
id
|
396
402
|
end
|
397
403
|
|
@@ -613,7 +619,7 @@ class BazaRb
|
|
613
619
|
uri.to_s,
|
614
620
|
headers:
|
615
621
|
),
|
616
|
-
[
|
622
|
+
[204, 302]
|
617
623
|
)
|
618
624
|
end
|
619
625
|
if ret.code == 204
|
@@ -731,9 +737,13 @@ class BazaRb
|
|
731
737
|
|
732
738
|
private
|
733
739
|
|
740
|
+
def user_agent
|
741
|
+
"baza.rb #{BazaRb::VERSION}"
|
742
|
+
end
|
743
|
+
|
734
744
|
def headers
|
735
745
|
{
|
736
|
-
'User-Agent' =>
|
746
|
+
'User-Agent' => user_agent,
|
737
747
|
'Connection' => 'close',
|
738
748
|
'X-Zerocracy-Token' => @token
|
739
749
|
}
|
@@ -865,4 +875,45 @@ class BazaRb
|
|
865
875
|
throw :"Downloaded #{File.size(file)} bytes from #{uri}"
|
866
876
|
end
|
867
877
|
end
|
878
|
+
|
879
|
+
# Upload file via PUT, in ranges.
|
880
|
+
# @param [String] uri The URI
|
881
|
+
# @param [String] file The path to save to
|
882
|
+
# @param [Hash] headers Hash of HTTP headers
|
883
|
+
def upload(uri, file, headers = {})
|
884
|
+
raise 'The "file" is nil' if file.nil?
|
885
|
+
raise 'The "file" does not exist' unless File.exist?(file)
|
886
|
+
params = {
|
887
|
+
connecttimeout: @timeout,
|
888
|
+
timeout: @timeout,
|
889
|
+
body: data,
|
890
|
+
headers: headers.merge(
|
891
|
+
'Content-Type' => 'application/octet-stream'
|
892
|
+
)
|
893
|
+
}
|
894
|
+
max = 1_000_000
|
895
|
+
chunk = 0
|
896
|
+
elapsed(@loog) do
|
897
|
+
params[:headers]['X-Zerocracy-Chunk'] = chunk.to_s
|
898
|
+
loop do
|
899
|
+
File.open(file, 'rb') do |f|
|
900
|
+
f.seek(max * chunk)
|
901
|
+
data = f.read(max)
|
902
|
+
data = '' if data.nil?
|
903
|
+
params[:headers]['Content-Length'] = data.bytesize
|
904
|
+
retry_it do
|
905
|
+
checked(
|
906
|
+
Typhoeus::Request.put(
|
907
|
+
uri.to_s,
|
908
|
+
@compress ? zipped(params) : params
|
909
|
+
)
|
910
|
+
)
|
911
|
+
end
|
912
|
+
break if data.empty?
|
913
|
+
end
|
914
|
+
chunk += 1
|
915
|
+
end
|
916
|
+
throw :"Uploaded #{File.size(file)} bytes to #{uri} in #{chunk + 1} chunk(s)"
|
917
|
+
end
|
918
|
+
end
|
868
919
|
end
|
data/test/test_baza-rb.rb
CHANGED
@@ -94,7 +94,7 @@ class TestBazaRb < Minitest::Test
|
|
94
94
|
skip('We are offline') unless we_are_online
|
95
95
|
Dir.mktmpdir do |dir|
|
96
96
|
file = File.join(dir, "#{fake_name}.bin")
|
97
|
-
File.binwrite(file, 'hello')
|
97
|
+
File.binwrite(file, 'hello, world!' * 100_000)
|
98
98
|
jname = fake_name
|
99
99
|
refute(LIVE.durable_find(jname, File.basename(file)))
|
100
100
|
id = LIVE.durable_place(jname, file)
|
@@ -179,6 +179,11 @@ class TestBazaRb < Minitest::Test
|
|
179
179
|
stub_request(:post, 'https://example.org/durables/place').to_return(
|
180
180
|
status: 302, headers: { 'X-Zerocracy-DurableId' => '42' }
|
181
181
|
)
|
182
|
+
stub_request(:get, %r{https://example\.org/durables/42/lock})
|
183
|
+
.to_return(status: 302)
|
184
|
+
stub_request(:get, %r{https://example\.org/durables/42/unlock})
|
185
|
+
.to_return(status: 302)
|
186
|
+
stub_request(:put, 'https://example.org/durables/42').to_return(status: 200)
|
182
187
|
Dir.mktmpdir do |dir|
|
183
188
|
file = File.join(dir, 'test.bin')
|
184
189
|
File.binwrite(file, 'hello')
|
@@ -212,6 +217,13 @@ class TestBazaRb < Minitest::Test
|
|
212
217
|
job = 4242
|
213
218
|
stub_request(:get, 'https://example.org/pop')
|
214
219
|
.with(query: { owner: })
|
220
|
+
.to_return(
|
221
|
+
status: 302,
|
222
|
+
headers: { 'X-Zerocracy-JobId' => job },
|
223
|
+
body: ''
|
224
|
+
)
|
225
|
+
stub_request(:get, 'https://example.org/pop')
|
226
|
+
.with(query: { job: })
|
215
227
|
.to_return(
|
216
228
|
status: 206,
|
217
229
|
headers: { 'Content-Range' => 'bytes 0-0/*', 'X-Zerocracy-JobId' => job, 'Content-Length' => 0 },
|