baza.rb 0.0.6 → 0.0.8

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: 1e5d99494e1d40acfbb4e3528aa78da5c6f3db987d2a5adeef6e3b549dd7d39a
4
- data.tar.gz: 935c8d8a3c2b91c75a9a65ff842661e21f2e6deeaa91349645a28970ad589011
3
+ metadata.gz: a4854042d95be4cff78c5fa7d534c754025b0fea372586f6f782c755df332432
4
+ data.tar.gz: bffcee29566e8959085d7948365eebd8cfb399c248644f494b73e82de0c65734
5
5
  SHA512:
6
- metadata.gz: a173d77d88af5c4c5c8d82ad6ec4a19d169a9759d1fe6c5a7932d2e3a222ca496c37b83a8158175f5f7a562b94c12c3572f22e9d66e65eb822b6ab75899ab9c5
7
- data.tar.gz: 2ca69ab0a36e5fa0a52592a7873fe115ea079df503797b2d2cba64e4bd402952a5ac3a9ea7a4ffca5edc70d40cbcfd80c518d6261a70a2ce36506d49e28d1b60
6
+ metadata.gz: d7e0c79a6f30febc5a408c7d63bab7606cdb9881c0bfc708fbd4ca7e5a60df14fe0d3677ec35c6fcf38210a23292fbe873bf51befd600040cfa42148170a8038
7
+ data.tar.gz: 027042ba55cd31a9ddea329efb085499deb342ba117a6a42782d1f328e74efb1164d12ae3329e7af0b4562705703f8af52b8057fa1012f0a8c2f9e488d3ad383
data/Gemfile CHANGED
@@ -24,12 +24,12 @@ source 'https://rubygems.org'
24
24
  gemspec
25
25
 
26
26
  gem 'factbase', '>0', require: false
27
- gem 'minitest', '5.24.1', require: false
27
+ gem 'minitest', '5.25.1', require: false
28
28
  gem 'minitest-reporters', '1.7.1', require: false
29
29
  gem 'net-ping', '2.0.8', require: false
30
30
  gem 'rake', '13.2.1', require: false
31
31
  gem 'random-port', '>0', require: false
32
- gem 'rspec-rails', '6.1.3', require: false
32
+ gem 'rspec-rails', '6.1.4', require: false
33
33
  gem 'rubocop', '1.65.1', require: false
34
34
  gem 'rubocop-performance', '1.21.1', require: false
35
35
  gem 'rubocop-rspec', '3.0.4', require: false
data/Gemfile.lock CHANGED
@@ -3,6 +3,7 @@ PATH
3
3
  specs:
4
4
  baza.rb (0.0.0)
5
5
  backtrace (> 0)
6
+ elapsed (> 0)
6
7
  faraday (> 0)
7
8
  faraday-http-cache (> 0)
8
9
  faraday-multipart (> 0)
@@ -16,9 +17,9 @@ PATH
16
17
  GEM
17
18
  remote: https://rubygems.org/
18
19
  specs:
19
- actionpack (7.2.0)
20
- actionview (= 7.2.0)
21
- activesupport (= 7.2.0)
20
+ actionpack (7.2.1)
21
+ actionview (= 7.2.1)
22
+ activesupport (= 7.2.1)
22
23
  nokogiri (>= 1.8.5)
23
24
  racc
24
25
  rack (>= 2.2.4, < 3.2)
@@ -27,13 +28,13 @@ GEM
27
28
  rails-dom-testing (~> 2.2)
28
29
  rails-html-sanitizer (~> 1.6)
29
30
  useragent (~> 0.16)
30
- actionview (7.2.0)
31
- activesupport (= 7.2.0)
31
+ actionview (7.2.1)
32
+ activesupport (= 7.2.1)
32
33
  builder (~> 3.1)
33
34
  erubi (~> 1.11)
34
35
  rails-dom-testing (~> 2.2)
35
36
  rails-html-sanitizer (~> 1.6)
36
- activesupport (7.2.0)
37
+ activesupport (7.2.1)
37
38
  base64
38
39
  bigdecimal
39
40
  concurrent-ruby (~> 1.0, >= 1.3.1)
@@ -62,6 +63,9 @@ GEM
62
63
  diff-lcs (1.5.1)
63
64
  docile (1.4.1)
64
65
  drb (2.2.1)
66
+ elapsed (0.0.1)
67
+ loog (> 0)
68
+ tago (> 0)
65
69
  erubi (1.13.0)
66
70
  ethon (0.16.0)
67
71
  ffi (>= 1.15.0)
@@ -74,14 +78,14 @@ GEM
74
78
  others (> 0)
75
79
  tago (> 0)
76
80
  yaml (~> 0.3)
77
- faraday (2.10.1)
78
- faraday-net_http (>= 2.0, < 3.2)
81
+ faraday (2.11.0)
82
+ faraday-net_http (>= 2.0, < 3.4)
79
83
  logger
80
84
  faraday-http-cache (2.5.1)
81
85
  faraday (>= 0.8)
82
86
  faraday-multipart (1.0.4)
83
87
  multipart-post (~> 2)
84
- faraday-net_http (3.1.1)
88
+ faraday-net_http (3.3.0)
85
89
  net-http
86
90
  faraday-retry (2.2.1)
87
91
  faraday (~> 2.0)
@@ -104,7 +108,7 @@ GEM
104
108
  crass (~> 1.0.2)
105
109
  nokogiri (>= 1.12.0)
106
110
  loog (0.6.0)
107
- minitest (5.24.1)
111
+ minitest (5.25.1)
108
112
  minitest-reporters (1.7.1)
109
113
  ansi
110
114
  builder
@@ -123,7 +127,7 @@ GEM
123
127
  nokogiri (1.16.7-x86_64-linux)
124
128
  racc (~> 1.4)
125
129
  others (0.0.3)
126
- parallel (1.26.2)
130
+ parallel (1.26.3)
127
131
  parser (3.3.4.2)
128
132
  ast (~> 2.4.1)
129
133
  racc
@@ -146,9 +150,9 @@ GEM
146
150
  rails-html-sanitizer (1.6.0)
147
151
  loofah (~> 2.21)
148
152
  nokogiri (~> 1.14)
149
- railties (7.2.0)
150
- actionpack (= 7.2.0)
151
- activesupport (= 7.2.0)
153
+ railties (7.2.1)
154
+ actionpack (= 7.2.1)
155
+ activesupport (= 7.2.1)
152
156
  irb (~> 1.13)
153
157
  rackup (>= 1.0.0)
154
158
  rake (>= 12.2)
@@ -163,17 +167,17 @@ GEM
163
167
  reline (0.5.9)
164
168
  io-console (~> 0.5)
165
169
  retries (0.0.5)
166
- rexml (3.3.5)
170
+ rexml (3.3.6)
167
171
  strscan
168
172
  rspec-core (3.13.0)
169
173
  rspec-support (~> 3.13.0)
170
- rspec-expectations (3.13.1)
174
+ rspec-expectations (3.13.2)
171
175
  diff-lcs (>= 1.2.0, < 2.0)
172
176
  rspec-support (~> 3.13.0)
173
177
  rspec-mocks (3.13.1)
174
178
  diff-lcs (>= 1.2.0, < 2.0)
175
179
  rspec-support (~> 3.13.0)
176
- rspec-rails (6.1.3)
180
+ rspec-rails (6.1.4)
177
181
  actionpack (>= 6.1)
178
182
  activesupport (>= 6.1)
179
183
  railties (>= 6.1)
@@ -193,7 +197,7 @@ GEM
193
197
  rubocop-ast (>= 1.31.1, < 2.0)
194
198
  ruby-progressbar (~> 1.7)
195
199
  unicode-display_width (>= 2.4.0, < 3.0)
196
- rubocop-ast (1.32.0)
200
+ rubocop-ast (1.32.1)
197
201
  parser (>= 3.3.1.0)
198
202
  rubocop-performance (1.21.1)
199
203
  rubocop (>= 1.48.1, < 2.0)
@@ -214,13 +218,13 @@ GEM
214
218
  stringio (3.1.1)
215
219
  strscan (3.1.0)
216
220
  tago (0.0.2)
217
- thor (1.3.1)
221
+ thor (1.3.2)
218
222
  typhoeus (1.4.1)
219
223
  ethon (>= 0.9.0)
220
224
  tzinfo (2.0.6)
221
225
  concurrent-ruby (~> 1.0)
222
226
  unicode-display_width (2.5.0)
223
- uri (0.13.0)
227
+ uri (0.13.1)
224
228
  useragent (0.16.10)
225
229
  wait_for (0.1.1)
226
230
  webmock (3.23.1)
@@ -242,12 +246,12 @@ PLATFORMS
242
246
  DEPENDENCIES
243
247
  baza.rb!
244
248
  factbase (> 0)
245
- minitest (= 5.24.1)
249
+ minitest (= 5.25.1)
246
250
  minitest-reporters (= 1.7.1)
247
251
  net-ping (= 2.0.8)
248
252
  rake (= 13.2.1)
249
253
  random-port (> 0)
250
- rspec-rails (= 6.1.3)
254
+ rspec-rails (= 6.1.4)
251
255
  rubocop (= 1.65.1)
252
256
  rubocop-performance (= 1.21.1)
253
257
  rubocop-rspec (= 3.0.4)
data/baza.rb.gemspec CHANGED
@@ -41,6 +41,7 @@ Gem::Specification.new do |s|
41
41
  s.rdoc_options = ['--charset=UTF-8']
42
42
  s.extra_rdoc_files = ['README.md', 'LICENSE.txt']
43
43
  s.add_dependency 'backtrace', '>0'
44
+ s.add_dependency 'elapsed', '>0'
44
45
  s.add_dependency 'faraday', '>0'
45
46
  s.add_dependency 'faraday-http-cache', '>0'
46
47
  s.add_dependency 'faraday-multipart', '>0'
@@ -22,9 +22,13 @@
22
22
 
23
23
  # Just a version.
24
24
  #
25
+ # We keep this file separate from the "baza-rb.rb" in order to have an
26
+ # ability to include it from the ".gemspec" script, without including all
27
+ # other packages (thus failing the build).
28
+ #
25
29
  # Author:: Yegor Bugayenko (yegor256@gmail.com)
26
30
  # Copyright:: Copyright (c) 2024 Yegor Bugayenko
27
31
  # License:: MIT
28
32
  class BazaRb
29
- VERSION = '0.0.6'
33
+ VERSION = '0.0.8'
30
34
  end
data/lib/baza-rb.rb CHANGED
@@ -21,6 +21,8 @@
21
21
  # SOFTWARE.
22
22
 
23
23
  require 'base64'
24
+ require 'elapsed'
25
+ require 'fileutils'
24
26
  require 'iri'
25
27
  require 'loog'
26
28
  require 'retries'
@@ -38,7 +40,17 @@ require_relative 'baza-rb/version'
38
40
  # Copyright:: Copyright (c) 2024 Yegor Bugayenko
39
41
  # License:: MIT
40
42
  class BazaRb
41
- def initialize(host, port, token, ssl: true, timeout: 30, retries: 3, loog: Loog::NULL, compression: true)
43
+ # Ctor.
44
+ #
45
+ # @param [String] host Host name
46
+ # @param [Integer] port TCP port
47
+ # @param [String] token Secret token of zerocracy.com
48
+ # @param [Boolean] ssl Should we use SSL?
49
+ # @param [Float] timeout Connect timeout and session timeout, in seconds
50
+ # @param [Integer] retries How many times to retry on connection failure?
51
+ # @param [Loog] loog The logging facility
52
+ # @param [Boolean] compress Set to TRUE if need to use GZIP while pulling and sending
53
+ def initialize(host, port, token, ssl: true, timeout: 30, retries: 3, loog: Loog::NULL, compress: true)
42
54
  @host = host
43
55
  @port = port
44
56
  @ssl = ssl
@@ -46,10 +58,11 @@ class BazaRb
46
58
  @timeout = timeout
47
59
  @loog = loog
48
60
  @retries = retries
49
- @compression = compression
61
+ @compress = compress
50
62
  end
51
63
 
52
64
  # Push factbase to the server.
65
+ #
53
66
  # @param [String] name The name of the job on the server
54
67
  # @param [Bytes] data The data to push to the server (binary)
55
68
  # @param [Array<String>] meta List of metas, possibly empty
@@ -79,7 +92,7 @@ class BazaRb
79
92
  checked(
80
93
  Typhoeus::Request.put(
81
94
  home.append('push').append(name).to_s,
82
- @compression ? zipped(params) : params
95
+ @compress ? zipped(params) : params
83
96
  )
84
97
  )
85
98
  end
@@ -90,6 +103,7 @@ class BazaRb
90
103
  end
91
104
 
92
105
  # Pull factbase from the server.
106
+ #
93
107
  # @param [Integer] id The ID of the job on the server
94
108
  # @return [Bytes] Binary data pulled
95
109
  def pull(id)
@@ -103,8 +117,9 @@ class BazaRb
103
117
  home.append('pull').append("#{id}.fb").to_s,
104
118
  method: :get,
105
119
  headers: headers.merge(
106
- 'Accept' => 'application/octet-stream'
120
+ 'Accept' => 'application/zip, application/factbase'
107
121
  ),
122
+ accept_encoding: 'gzip',
108
123
  connecttimeout: @timeout,
109
124
  timeout: @timeout
110
125
  )
@@ -124,6 +139,7 @@ class BazaRb
124
139
  end
125
140
 
126
141
  # The job with this ID is finished already?
142
+ #
127
143
  # @param [Integer] id The ID of the job on the server
128
144
  # @return [Boolean] TRUE if the job is already finished
129
145
  def finished?(id)
@@ -147,6 +163,7 @@ class BazaRb
147
163
  end
148
164
 
149
165
  # Read and return the stdout of the job.
166
+ #
150
167
  # @param [Integer] id The ID of the job on the server
151
168
  # @return [String] The stdout, as a text
152
169
  def stdout(id)
@@ -170,6 +187,7 @@ class BazaRb
170
187
  end
171
188
 
172
189
  # Read and return the exit code of the job.
190
+ #
173
191
  # @param [Integer] id The ID of the job on the server
174
192
  # @return [Integer] The exit code
175
193
  def exit_code(id)
@@ -193,6 +211,7 @@ class BazaRb
193
211
  end
194
212
 
195
213
  # Read and return the verification verdict of the job.
214
+ #
196
215
  # @param [Integer] id The ID of the job on the server
197
216
  # @return [String] The verdict
198
217
  def verified(id)
@@ -216,6 +235,7 @@ class BazaRb
216
235
  end
217
236
 
218
237
  # Lock the name.
238
+ #
219
239
  # @param [String] name The name of the job on the server
220
240
  # @param [String] owner The owner of the lock (any string)
221
241
  def lock(name, owner)
@@ -237,6 +257,7 @@ class BazaRb
237
257
  end
238
258
 
239
259
  # Unlock the name.
260
+ #
240
261
  # @param [String] name The name of the job on the server
241
262
  # @param [String] owner The owner of the lock (any string)
242
263
  def unlock(name, owner)
@@ -258,6 +279,7 @@ class BazaRb
258
279
  end
259
280
 
260
281
  # Get the ID of the job by the name.
282
+ #
261
283
  # @param [String] name The name of the job on the server
262
284
  # @return [Integer] The ID of the job on the server
263
285
  def recent(name)
@@ -281,6 +303,7 @@ class BazaRb
281
303
  end
282
304
 
283
305
  # Check whether the name of the job exists on the server.
306
+ #
284
307
  # @param [String] name The name of the job on the server
285
308
  # @return [Boolean] TRUE if such name exists
286
309
  def name_exists?(name)
@@ -304,6 +327,7 @@ class BazaRb
304
327
  end
305
328
 
306
329
  # Place a single durable.
330
+ #
307
331
  # @param [String] jname The name of the job on the server
308
332
  # @param [String] file The file name
309
333
  def durable_place(jname, file)
@@ -337,6 +361,7 @@ class BazaRb
337
361
  end
338
362
 
339
363
  # Save a single durable from local file to server.
364
+ #
340
365
  # @param [Integer] id The ID of the durable
341
366
  # @param [String] file The file to upload
342
367
  def durable_save(id, file)
@@ -361,6 +386,7 @@ class BazaRb
361
386
  end
362
387
 
363
388
  # Load a single durable from server to local file.
389
+ #
364
390
  # @param [Integer] id The ID of the durable
365
391
  # @param [String] file The file to upload
366
392
  def durable_load(id, file)
@@ -392,6 +418,7 @@ class BazaRb
392
418
  end
393
419
 
394
420
  # Lock a single durable.
421
+ #
395
422
  # @param [Integer] id The ID of the durable
396
423
  # @param [String] owner The owner of the lock
397
424
  def durable_lock(id, owner)
@@ -414,6 +441,7 @@ class BazaRb
414
441
  end
415
442
 
416
443
  # Unlock a single durable.
444
+ #
417
445
  # @param [Integer] id The ID of the durable
418
446
  # @param [String] owner The owner of the lock
419
447
  def durable_unlock(id, owner)
@@ -435,6 +463,74 @@ class BazaRb
435
463
  end
436
464
  end
437
465
 
466
+ # Pop job from the server.
467
+ #
468
+ # @param [String] owner Who is acting (could be any text)
469
+ # @param [String] zip The path to ZIP archive to take
470
+ # @return [Boolean] TRUE if job taken, otherwise false
471
+ def pop(owner, zip)
472
+ raise 'The "zip" of the job is nil' if zip.nil?
473
+ raise "The 'zip' file is absent: #{zip}" unless File.exist?(zip)
474
+ success = false
475
+ FileUtils.rm_f(zip)
476
+ elapsed(@loog) do
477
+ File.open(zip, 'wb') do |f|
478
+ request = Typhoeus::Request.new(
479
+ home.append('pop').add(owner:).to_s,
480
+ method: :get,
481
+ headers: headers.merge(
482
+ 'Accept' => 'application/octet-stream'
483
+ ),
484
+ connecttimeout: @timeout,
485
+ timeout: @timeout
486
+ )
487
+ request.on_body do |chunk|
488
+ f.write(chunk)
489
+ end
490
+ with_retries(max_tries: @retries) do
491
+ request.run
492
+ end
493
+ ret = request.response
494
+ checked(ret, [200, 204])
495
+ success = ret.code == 200
496
+ end
497
+ unless success
498
+ FileUtils.rm_f(zip)
499
+ throw :"Nothing to pop at #{@host}"
500
+ end
501
+ throw :"Popped #{File.size(zip)} bytes in ZIP archive at #{@host}"
502
+ end
503
+ success
504
+ end
505
+
506
+ # Submit a ZIP archvie to finish a job.
507
+ #
508
+ # @param [Integer] id The ID of the job on the server
509
+ # @param [String] zip The path to the ZIP file with the content of the archive
510
+ def finish(id, zip)
511
+ raise 'The "id" of the job is nil' if id.nil?
512
+ raise 'The "id" of the job must be an integer' unless id.is_a?(Integer)
513
+ raise 'The "zip" of the job is nil' if zip.nil?
514
+ raise "The 'zip' file is absent: #{zip}" unless File.exist?(zip)
515
+ elapsed(@loog) do
516
+ with_retries(max_tries: @retries) do
517
+ checked(
518
+ Typhoeus::Request.put(
519
+ home.append('finish').add(id:).to_s,
520
+ connecttimeout: @timeout,
521
+ timeout: @timeout,
522
+ body: File.binread(zip),
523
+ headers: headers.merge(
524
+ 'Content-Type' => 'application/octet-stream',
525
+ 'Content-Length' => File.size(zip)
526
+ )
527
+ )
528
+ )
529
+ end
530
+ throw :"Pushed #{File.size(zip)} bytes to #{@host}, finished job ##{id}"
531
+ end
532
+ end
533
+
438
534
  private
439
535
 
440
536
  def headers
@@ -459,17 +555,6 @@ class BazaRb
459
555
  params.merge(body:, headers:)
460
556
  end
461
557
 
462
- def elapsed(loog)
463
- start = Time.now
464
- begin
465
- yield
466
- rescue UncaughtThrowError => e
467
- tag = e.tag
468
- throw e unless tag.is_a?(Symbol)
469
- loog.info("#{tag} in #{start.ago}")
470
- end
471
- end
472
-
473
558
  def gzip(data)
474
559
  ''.dup.tap do |result|
475
560
  io = StringIO.new(result)
data/test/test_baza-rb.rb CHANGED
@@ -70,7 +70,7 @@ class TestBazaRb < Minitest::Test
70
70
  fb = Factbase.new
71
71
  fb.insert.foo = 'test-' * 10_000
72
72
  fb.insert
73
- baza = BazaRb.new(HOST, PORT, TOKEN, compression: false)
73
+ baza = BazaRb.new(HOST, PORT, TOKEN, compress: false)
74
74
  assert(baza.push(fake_name, fb.export, []).positive?)
75
75
  end
76
76
 
@@ -112,6 +112,23 @@ class TestBazaRb < Minitest::Test
112
112
  )
113
113
  end
114
114
 
115
+ def test_simple_pop
116
+ WebMock.disable_net_connect!
117
+ stub_request(:get, 'https://example.org/pop?owner=me').to_return(status: 204)
118
+ Tempfile.open do |zip|
119
+ assert(!BazaRb.new('example.org', 443, '000').pop('me', zip.path))
120
+ assert(!File.exist?(zip.path))
121
+ end
122
+ end
123
+
124
+ def test_simple_finish
125
+ WebMock.disable_net_connect!
126
+ stub_request(:put, 'https://example.org/finish?id=42').to_return(status: 200)
127
+ Tempfile.open do |zip|
128
+ BazaRb.new('example.org', 443, '000').finish(42, zip.path)
129
+ end
130
+ end
131
+
115
132
  def test_simple_recent_check
116
133
  WebMock.disable_net_connect!
117
134
  stub_request(:get, 'https://example.org/recent/simple.txt')
@@ -196,7 +213,6 @@ class TestBazaRb < Minitest::Test
196
213
  end
197
214
 
198
215
  def test_push_compressed_content
199
- skip # this test is not stable, see https://github.com/yegor256/judges/issues/105
200
216
  req =
201
217
  with_http_server(200, 'yes') do |baza|
202
218
  baza.push('simple', 'hello, world!', %w[meta1 meta2 meta3])
@@ -209,7 +225,7 @@ class TestBazaRb < Minitest::Test
209
225
 
210
226
  def test_push_compression_disabled
211
227
  req =
212
- with_http_server(200, 'yes', compression: false) do |baza|
228
+ with_http_server(200, 'yes', compress: false) do |baza|
213
229
  baza.push('simple', 'hello, world!', %w[meta1 meta2 meta3])
214
230
  end
215
231
  assert_equal('application/octet-stream', req.content_type)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: baza.rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-08-13 00:00:00.000000000 Z
11
+ date: 2024-08-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: backtrace
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: elapsed
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">"
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">"
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: faraday
29
43
  requirement: !ruby/object:Gem::Requirement