baza.rb 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cec6d23dff978b5dcf080a6940a875ee1f9a67eb07b61ee5ee4b8a98348113c4
4
- data.tar.gz: 98eb41907e61acc9beeb0397da3b1ec0c9cbb8a1ebd7c8f57422f8be18947cf4
3
+ metadata.gz: a4854042d95be4cff78c5fa7d534c754025b0fea372586f6f782c755df332432
4
+ data.tar.gz: bffcee29566e8959085d7948365eebd8cfb399c248644f494b73e82de0c65734
5
5
  SHA512:
6
- metadata.gz: 1df697ec1d35721c3d23e6ef0b05d0d1c7e99b47c959b8150e413b18f5fd131efb9d3ca28bbdff4343ab4f012ce86217f4b1ffc8508c92eec2842633b6bef354
7
- data.tar.gz: 9f74b8782d6a5ad2bfa1f0d8450e1c033cf5c8ad22c2a083f20fb2d96f94a8abbdd572b612a9440edf74f1d40d3b09356c471e2befd85fdd8c020b69884f60f3
6
+ metadata.gz: d7e0c79a6f30febc5a408c7d63bab7606cdb9881c0bfc708fbd4ca7e5a60df14fe0d3677ec35c6fcf38210a23292fbe873bf51befd600040cfa42148170a8038
7
+ data.tar.gz: 027042ba55cd31a9ddea329efb085499deb342ba117a6a42782d1f328e74efb1164d12ae3329e7af0b4562705703f8af52b8057fa1012f0a8c2f9e488d3ad383
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)
@@ -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,11 +167,11 @@ 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)
@@ -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)
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.7'
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)
@@ -125,6 +139,7 @@ class BazaRb
125
139
  end
126
140
 
127
141
  # The job with this ID is finished already?
142
+ #
128
143
  # @param [Integer] id The ID of the job on the server
129
144
  # @return [Boolean] TRUE if the job is already finished
130
145
  def finished?(id)
@@ -148,6 +163,7 @@ class BazaRb
148
163
  end
149
164
 
150
165
  # Read and return the stdout of the job.
166
+ #
151
167
  # @param [Integer] id The ID of the job on the server
152
168
  # @return [String] The stdout, as a text
153
169
  def stdout(id)
@@ -171,6 +187,7 @@ class BazaRb
171
187
  end
172
188
 
173
189
  # Read and return the exit code of the job.
190
+ #
174
191
  # @param [Integer] id The ID of the job on the server
175
192
  # @return [Integer] The exit code
176
193
  def exit_code(id)
@@ -194,6 +211,7 @@ class BazaRb
194
211
  end
195
212
 
196
213
  # Read and return the verification verdict of the job.
214
+ #
197
215
  # @param [Integer] id The ID of the job on the server
198
216
  # @return [String] The verdict
199
217
  def verified(id)
@@ -217,6 +235,7 @@ class BazaRb
217
235
  end
218
236
 
219
237
  # Lock the name.
238
+ #
220
239
  # @param [String] name The name of the job on the server
221
240
  # @param [String] owner The owner of the lock (any string)
222
241
  def lock(name, owner)
@@ -238,6 +257,7 @@ class BazaRb
238
257
  end
239
258
 
240
259
  # Unlock the name.
260
+ #
241
261
  # @param [String] name The name of the job on the server
242
262
  # @param [String] owner The owner of the lock (any string)
243
263
  def unlock(name, owner)
@@ -259,6 +279,7 @@ class BazaRb
259
279
  end
260
280
 
261
281
  # Get the ID of the job by the name.
282
+ #
262
283
  # @param [String] name The name of the job on the server
263
284
  # @return [Integer] The ID of the job on the server
264
285
  def recent(name)
@@ -282,6 +303,7 @@ class BazaRb
282
303
  end
283
304
 
284
305
  # Check whether the name of the job exists on the server.
306
+ #
285
307
  # @param [String] name The name of the job on the server
286
308
  # @return [Boolean] TRUE if such name exists
287
309
  def name_exists?(name)
@@ -305,6 +327,7 @@ class BazaRb
305
327
  end
306
328
 
307
329
  # Place a single durable.
330
+ #
308
331
  # @param [String] jname The name of the job on the server
309
332
  # @param [String] file The file name
310
333
  def durable_place(jname, file)
@@ -338,6 +361,7 @@ class BazaRb
338
361
  end
339
362
 
340
363
  # Save a single durable from local file to server.
364
+ #
341
365
  # @param [Integer] id The ID of the durable
342
366
  # @param [String] file The file to upload
343
367
  def durable_save(id, file)
@@ -362,6 +386,7 @@ class BazaRb
362
386
  end
363
387
 
364
388
  # Load a single durable from server to local file.
389
+ #
365
390
  # @param [Integer] id The ID of the durable
366
391
  # @param [String] file The file to upload
367
392
  def durable_load(id, file)
@@ -393,6 +418,7 @@ class BazaRb
393
418
  end
394
419
 
395
420
  # Lock a single durable.
421
+ #
396
422
  # @param [Integer] id The ID of the durable
397
423
  # @param [String] owner The owner of the lock
398
424
  def durable_lock(id, owner)
@@ -415,6 +441,7 @@ class BazaRb
415
441
  end
416
442
 
417
443
  # Unlock a single durable.
444
+ #
418
445
  # @param [Integer] id The ID of the durable
419
446
  # @param [String] owner The owner of the lock
420
447
  def durable_unlock(id, owner)
@@ -436,6 +463,74 @@ class BazaRb
436
463
  end
437
464
  end
438
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
+
439
534
  private
440
535
 
441
536
  def headers
@@ -460,17 +555,6 @@ class BazaRb
460
555
  params.merge(body:, headers:)
461
556
  end
462
557
 
463
- def elapsed(loog)
464
- start = Time.now
465
- begin
466
- yield
467
- rescue UncaughtThrowError => e
468
- tag = e.tag
469
- throw e unless tag.is_a?(Symbol)
470
- loog.info("#{tag} in #{start.ago}")
471
- end
472
- end
473
-
474
558
  def gzip(data)
475
559
  ''.dup.tap do |result|
476
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')
@@ -208,7 +225,7 @@ class TestBazaRb < Minitest::Test
208
225
 
209
226
  def test_push_compression_disabled
210
227
  req =
211
- with_http_server(200, 'yes', compression: false) do |baza|
228
+ with_http_server(200, 'yes', compress: false) do |baza|
212
229
  baza.push('simple', 'hello, world!', %w[meta1 meta2 meta3])
213
230
  end
214
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.7
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-18 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