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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f6ee64c99f7aa6a6254d2a98ffd0a49f33cb5c33992094afedf2d926905b7a24
4
- data.tar.gz: 76722a8fdafbc741e314213ae3912ff8e8c6d7368cbde7edbe3ab89c87448916
3
+ metadata.gz: ebcb57db3953278c8d402cc38ae954e982062cfc4498e301bc0495a7b3cba95a
4
+ data.tar.gz: 0a6acf7107e149f8515911542460266b95b825df586576713734648751f2de56
5
5
  SHA512:
6
- metadata.gz: 351ef08d92a9d637b51b163db95e26b8927abed703a51a1362e4fe51a10714c97afa7b16700bc348cfa4de168788929f5abb89c62e59a4ecf53ce2cd588b3cec
7
- data.tar.gz: 6560d77ff5939235f2db005c6151e3cf83ea0d8b40f4c0b7e84c6dde97caaa25daad25beb4cd3dbb936e837601783454b9eefad8bc0a56320d4a4c44d6e1b555
6
+ metadata.gz: a40b4c3655a48f0449cb0247f1dd1045344b95868330121b6ce462d874aec6117b1490d12819ceac9afc29217e0352d4d30d2c6ff78c1031761946bb3cf89654
7
+ data.tar.gz: 7a479de17acf3e82f60327fa7c131605bebf2a0ae2dbfd1fde80ac08a3bcfafd2d40b252ef4d23845de083867a5955244d7079ce5526574d94c1fb9814e62cf8
@@ -13,5 +13,5 @@
13
13
  # Copyright:: Copyright (c) 2024-2025 Yegor Bugayenko
14
14
  # License:: MIT
15
15
  class BazaRb
16
- VERSION = '0.7.0'
16
+ VERSION = '0.8.0'
17
17
  end
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
- elapsed(@loog) do
374
- ret =
375
- retry_it do
376
- checked(
377
- Typhoeus::Request.post(
378
- home.append('durables').append('place').to_s,
379
- body: {
380
- '_csrf' => csrf,
381
- 'jname' => jname,
382
- 'file' => File.basename(file),
383
- 'zip' => File.open(file, 'rb')
384
- },
385
- headers:,
386
- connecttimeout: @timeout,
387
- timeout: @timeout
388
- ),
389
- 302
390
- )
391
- end
392
- id = ret.headers['X-Zerocracy-DurableId'].to_i
393
- throw :"Durable ##{id} (#{file}) placed for job \"#{jname}\" at #{@host}"
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
- [200, 204, 206]
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' => "baza.rb #{BazaRb::VERSION}",
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 },
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: baza.rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko