baza.rb 0.6.0 → 0.7.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: 055d08eba666af0bcadcea162c8b47ccbdd7b9ffc1647830999112a503ad8907
4
- data.tar.gz: 966991989ab8c9fc2dba9f2c2498a38278a8d70195742266d452c34af33328fb
3
+ metadata.gz: f6ee64c99f7aa6a6254d2a98ffd0a49f33cb5c33992094afedf2d926905b7a24
4
+ data.tar.gz: 76722a8fdafbc741e314213ae3912ff8e8c6d7368cbde7edbe3ab89c87448916
5
5
  SHA512:
6
- metadata.gz: e699824b88910aa69f738e3329b62b9aece6b296f1e7aec9917b44044b32e14845f1700d774fb4386977fe64a03fcb5bd5e39c15ea4c40053d814a632b95d1ea
7
- data.tar.gz: f462b2fcada287d6f6af5f6d11bad0651cfbad83b079957a43fcac92cf8947c479be776c73c8346281e82c78803782c44919d8c19ef38670db344435365b15b8
6
+ metadata.gz: 351ef08d92a9d637b51b163db95e26b8927abed703a51a1362e4fe51a10714c97afa7b16700bc348cfa4de168788929f5abb89c62e59a4ecf53ce2cd588b3cec
7
+ data.tar.gz: 6560d77ff5939235f2db005c6151e3cf83ea0d8b40f4c0b7e84c6dde97caaa25daad25beb4cd3dbb936e837601783454b9eefad8bc0a56320d4a4c44d6e1b555
data/Gemfile.lock CHANGED
@@ -107,7 +107,7 @@ GEM
107
107
  regexp_parser (2.10.0)
108
108
  retries (0.0.5)
109
109
  rexml (3.4.1)
110
- rubocop (1.76.0)
110
+ rubocop (1.76.1)
111
111
  json (~> 2.3)
112
112
  language_server-protocol (~> 3.17.0.2)
113
113
  lint_roller (~> 1.1.0)
@@ -118,7 +118,7 @@ GEM
118
118
  rubocop-ast (>= 1.45.0, < 2.0)
119
119
  ruby-progressbar (~> 1.7)
120
120
  unicode-display_width (>= 2.4.0, < 4.0)
121
- rubocop-ast (1.45.0)
121
+ rubocop-ast (1.45.1)
122
122
  parser (>= 3.3.7.2)
123
123
  prism (~> 1.4)
124
124
  rubocop-minitest (0.38.1)
data/lib/baza-rb/fake.rb CHANGED
@@ -123,6 +123,17 @@ class BazaRb::Fake
123
123
  true
124
124
  end
125
125
 
126
+ # Find a single durable.
127
+ #
128
+ # @param [String] jname The name of the job on the server
129
+ # @param [String] file The path to the file to upload
130
+ # @return [Integer] Always returns 42 as the fake durable ID
131
+ def durable_find(jname, file)
132
+ assert_name(jname)
133
+ assert_file(file)
134
+ 42
135
+ end
136
+
126
137
  # Place a single durable file on the server.
127
138
  #
128
139
  # @param [String] jname The name of the job on the server
@@ -13,5 +13,5 @@
13
13
  # Copyright:: Copyright (c) 2024-2025 Yegor Bugayenko
14
14
  # License:: MIT
15
15
  class BazaRb
16
- VERSION = '0.6.0'
16
+ VERSION = '0.7.0'
17
17
  end
data/lib/baza-rb.rb CHANGED
@@ -158,25 +158,7 @@ class BazaRb
158
158
  data = ''
159
159
  elapsed(@loog) do
160
160
  Tempfile.open do |file|
161
- File.open(file, 'wb') do |f|
162
- request = Typhoeus::Request.new(
163
- home.append('pull').append("#{id}.fb").to_s,
164
- method: :get,
165
- headers: headers.merge(
166
- 'Accept' => 'application/zip, application/factbase'
167
- ),
168
- accept_encoding: 'gzip',
169
- connecttimeout: @timeout,
170
- timeout: @timeout
171
- )
172
- request.on_body do |chunk|
173
- f.write(chunk)
174
- end
175
- retry_it do
176
- request.run
177
- end
178
- checked(request.response)
179
- end
161
+ download(home.append('pull').append("#{id}.fb").to_s, file.path)
180
162
  data = File.binread(file)
181
163
  throw :"Pulled #{data.bytesize} bytes of job ##{id} factbase at #{@host}"
182
164
  end
@@ -446,26 +428,8 @@ class BazaRb
446
428
  raise 'The ID of the durable is nil' if id.nil?
447
429
  raise 'The ID of the durable must be a positive integer' unless id.positive?
448
430
  raise 'The "file" of the durable is nil' if file.nil?
449
- FileUtils.mkdir_p(File.dirname(file))
450
431
  elapsed(@loog) do
451
- File.open(file, 'wb') do |f|
452
- request = Typhoeus::Request.new(
453
- home.append('durables').append(id).to_s,
454
- method: :get,
455
- headers: headers.merge(
456
- 'Accept' => 'application/octet-stream'
457
- ),
458
- connecttimeout: @timeout,
459
- timeout: @timeout
460
- )
461
- request.on_body do |chunk|
462
- f.write(chunk)
463
- end
464
- retry_it do
465
- request.run
466
- end
467
- checked(request.response)
468
- end
432
+ download(home.append('durables').append(id).to_s, file)
469
433
  throw :"Durable ##{id} loaded #{File.size(file)} bytes from #{@host}"
470
434
  end
471
435
  end
@@ -639,55 +603,28 @@ class BazaRb
639
603
  # @return [Boolean] TRUE if a job was successfully popped, FALSE if queue is empty
640
604
  # @raise [ServerFailure] If the pop operation fails
641
605
  def pop(owner, zip)
642
- raise 'The "zip" of the job is nil' if zip.nil?
643
606
  success = false
644
- FileUtils.rm_f(zip)
645
- job = nil
646
607
  elapsed(@loog) do
647
- File.open(zip, 'wb+') do |f|
648
- loop do
649
- uri = home.append('pop').add(owner:)
650
- uri = uri.add(job:) if job
651
- request = Typhoeus::Request.new(
652
- uri.to_s,
653
- method: :get,
654
- headers: headers.merge(
655
- 'Accept' => 'application/octet-stream',
656
- 'Range' => "bytes=#{f.size}-"
608
+ uri = home.append('pop').add(owner:)
609
+ ret =
610
+ retry_it do
611
+ checked(
612
+ Typhoeus::Request.get(
613
+ uri.to_s,
614
+ headers:
657
615
  ),
658
- connecttimeout: @timeout,
659
- timeout: @timeout
616
+ [200, 204, 206]
660
617
  )
661
- request.on_body do |chunk|
662
- f.write(chunk)
663
- end
664
- retry_it do
665
- request.run
666
- end
667
- ret = request.response
668
- checked(ret, [200, 204, 206])
669
- success = ret.code != 204
670
- break unless ret.code == 206
671
- job = ret.headers['X-Zerocracy-JobId']
672
- raise 'Job ID is not returned in X-Zerocracy-JobId' if job.nil?
673
- raise "Job ID returned in X-Zerocracy-JobId is not valid (#{job.inspect})" unless job.match?(/^[0-9]+$/)
674
- _, v = ret.headers['Content-Range'].split
675
- range, total = v.split('/')
676
- raise "Total size is not valid (#{total.inspect})" unless total.match?(/^\*|[0-9]+$/)
677
- b, e = range.split('-')
678
- raise "Range is not valid (#{range.inspect})" unless e.match?(/^[0-9]+$/)
679
- len = ret.headers['Content-Length'].to_i
680
- unless len.zero?
681
- raise "Range size (#{range.inspect}) is not equal to Content-Length" unless len - 1 == e.to_i - b.to_i
682
- raise "Range end (#{range.inspect}) is not equal to #{f.size}" if e.to_i != f.size - 1
683
- end
684
- break if e.to_i == total.to_i - 1
685
618
  end
686
- end
687
- unless success
619
+ if ret.code == 204
688
620
  FileUtils.rm_f(zip)
689
- throw :"Nothing to pop at #{@host}"
621
+ throw :"Nothing to pop at #{uri}"
690
622
  end
623
+ job = ret.headers['X-Zerocracy-JobId']
624
+ raise 'Job ID is not returned in X-Zerocracy-JobId' if job.nil?
625
+ raise "Job ID returned in X-Zerocracy-JobId is not valid (#{job.inspect})" unless job.match?(/^[0-9]+$/)
626
+ download(uri.add(job:), zip)
627
+ success = true
691
628
  throw :"Popped #{File.size(zip)} bytes in ZIP archive at #{@host}"
692
629
  end
693
630
  success
@@ -883,4 +820,49 @@ class BazaRb
883
820
  @loog.error(msg)
884
821
  raise ServerFailure, msg
885
822
  end
823
+
824
+ # Download file via GET, in ranges.
825
+ # @param [String] uri The URI
826
+ # @param [String] file The path to save to
827
+ def download(uri, file)
828
+ raise 'The "file" is nil' if file.nil?
829
+ FileUtils.mkdir_p(File.dirname(file))
830
+ elapsed(@loog) do
831
+ File.open(file, 'wb+') do |f|
832
+ loop do
833
+ request = Typhoeus::Request.new(
834
+ uri.to_s,
835
+ method: :get,
836
+ headers: headers.merge(
837
+ 'Accept' => 'application/octet-stream',
838
+ 'Range' => "bytes=#{f.size}-"
839
+ ),
840
+ connecttimeout: @timeout,
841
+ timeout: @timeout
842
+ )
843
+ request.on_body do |chunk|
844
+ f.write(chunk)
845
+ end
846
+ retry_it do
847
+ request.run
848
+ end
849
+ ret = request.response
850
+ checked(ret, [200, 206])
851
+ break if ret.code == 200
852
+ _, v = ret.headers['Content-Range'].split
853
+ range, total = v.split('/')
854
+ raise "Total size is not valid (#{total.inspect})" unless total.match?(/^\*|[0-9]+$/)
855
+ b, e = range.split('-')
856
+ raise "Range is not valid (#{range.inspect})" unless e.match?(/^[0-9]+$/)
857
+ len = ret.headers['Content-Length'].to_i
858
+ unless len.zero?
859
+ raise "Range size (#{range.inspect}) is not equal to Content-Length" unless len - 1 == e.to_i - b.to_i
860
+ raise "Range end (#{range.inspect}) is not equal to #{f.size}" if e.to_i != f.size - 1
861
+ end
862
+ break if e.to_i == total.to_i - 1
863
+ end
864
+ end
865
+ throw :"Downloaded #{File.size(file)} bytes from #{uri}"
866
+ end
867
+ end
886
868
  end
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.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko