Class: BazaRb

Inherits:
Object
  • Object
show all
Defined in:
lib/baza-rb.rb,
lib/baza-rb/version.rb

Overview

Just a version.

We keep this file separate from the “baza-rb.rb” in order to have an ability to include it from the “.gemspec” script, without including all other packages (thus failing the build).

Author

Yegor Bugayenko ([email protected])

Copyright

Copyright © 2024 Yegor Bugayenko

License

MIT

Defined Under Namespace

Classes: BadResponse, ServerFailure, TimedOut

Constant Summary collapse

VERSION =
'0.0.9'

Instance Method Summary collapse

Constructor Details

#initialize(host, port, token, ssl: true, timeout: 30, retries: 3, loog: Loog::NULL, compress: true) ⇒ BazaRb

Ctor.

Parameters:

  • host (String)

    Host name

  • port (Integer)

    TCP port

  • token (String)

    Secret token of zerocracy.com

  • ssl (Boolean) (defaults to: true)

    Should we use SSL?

  • timeout (Float) (defaults to: 30)

    Connect timeout and session timeout, in seconds

  • retries (Integer) (defaults to: 3)

    How many times to retry on connection failure?

  • loog (Loog) (defaults to: Loog::NULL)

    The logging facility

  • compress (Boolean) (defaults to: true)

    Set to TRUE if need to use GZIP while pulling and sending



62
63
64
65
66
67
68
69
70
71
# File 'lib/baza-rb.rb', line 62

def initialize(host, port, token, ssl: true, timeout: 30, retries: 3, loog: Loog::NULL, compress: true)
  @host = host
  @port = port
  @ssl = ssl
  @token = token
  @timeout = timeout
  @loog = loog
  @retries = retries
  @compress = compress
end

Instance Method Details

#durable_load(id, file) ⇒ Object

Load a single durable from server to local file.

Parameters:

  • id (Integer)

    The ID of the durable

  • file (String)

    The file to upload



401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
# File 'lib/baza-rb.rb', line 401

def durable_load(id, file)
  raise 'The ID of the durable is nil' if id.nil?
  raise 'The ID of the durable must be a positive integer' unless id.positive?
  raise 'The "file" of the durable is nil' if file.nil?
  FileUtils.mkdir_p(File.dirname(file))
  elapsed(@loog) do
    File.open(file, 'wb') do |f|
      request = Typhoeus::Request.new(
        home.append('durables').append(id).to_s,
        method: :get,
        headers: headers.merge(
          'Accept' => 'application/octet-stream'
        ),
        connecttimeout: @timeout,
        timeout: @timeout
      )
      request.on_body do |chunk|
        f.write(chunk)
      end
      with_retries(max_tries: @retries, rescue: TimedOut) do
        request.run
      end
      checked(request.response)
    end
    throw :"Durable ##{id} loaded #{File.size(file)} bytes from #{@host}"
  end
end

#durable_lock(id, owner) ⇒ Object

Lock a single durable.

Parameters:

  • id (Integer)

    The ID of the durable

  • owner (String)

    The owner of the lock



433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
# File 'lib/baza-rb.rb', line 433

def durable_lock(id, owner)
  raise 'The ID of the durable is nil' if id.nil?
  raise 'The ID of the durable must be a positive integer' unless id.positive?
  raise 'The "owner" of the lock is nil' if owner.nil?
  raise 'The "owner" of the lock may not be empty' if owner.empty?
  elapsed(@loog) do
    with_retries(max_tries: @retries, rescue: TimedOut) do
      checked(
        Typhoeus::Request.get(
          home.append('durables').append(id).append('lock').add(owner:).to_s,
          headers:
        ),
        302
      )
    end
    throw :"Durable ##{id} locked at #{@host}"
  end
end

#durable_place(jname, file) ⇒ Object

Place a single durable.

Parameters:

  • jname (String)

    The name of the job on the server

  • file (String)

    The file name



342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# File 'lib/baza-rb.rb', line 342

def durable_place(jname, file)
  raise 'The "jname" of the durable is nil' if jname.nil?
  raise 'The "jname" of the durable may not be empty' if jname.empty?
  raise 'The "file" of the durable is nil' if file.nil?
  raise "The file '#{file}' is absent" unless File.exist?(file)
  id = nil
  elapsed(@loog) do
    ret =
      with_retries(max_tries: @retries, rescue: TimedOut) do
        checked(
          Typhoeus::Request.post(
            home.append('durables').append('place').to_s,
            body: {
              'jname' => jname,
              'file' => File.basename(file),
              'zip' => File.open(file, 'rb')
            },
            headers:,
            connecttimeout: @timeout,
            timeout: @timeout
          ),
          302
        )
      end
    id = ret.headers['X-Zerocracy-DurableId'].to_i
    throw :"Durable ##{id} (#{file}) placed for job \"#{jname}\" at #{@host}"
  end
  id
end

#durable_save(id, file) ⇒ Object

Save a single durable from local file to server.

Parameters:

  • id (Integer)

    The ID of the durable

  • file (String)

    The file to upload



376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
# File 'lib/baza-rb.rb', line 376

def durable_save(id, file)
  raise 'The ID of the durable is nil' if id.nil?
  raise 'The ID of the durable must be a positive integer' unless id.positive?
  raise 'The "file" of the durable is nil' if file.nil?
  raise "The file '#{file}' is absent" unless File.exist?(file)
  elapsed(@loog) do
    with_retries(max_tries: @retries, rescue: TimedOut) do
      checked(
        Typhoeus::Request.put(
          home.append('durables').append(id).to_s,
          body: File.binread(file),
          headers:,
          connecttimeout: @timeout,
          timeout: @timeout
        )
      )
    end
    throw :"Durable ##{id} saved #{File.size(file)} bytes to #{@host}"
  end
end

#durable_unlock(id, owner) ⇒ Object

Unlock a single durable.

Parameters:

  • id (Integer)

    The ID of the durable

  • owner (String)

    The owner of the lock



456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
# File 'lib/baza-rb.rb', line 456

def durable_unlock(id, owner)
  raise 'The ID of the durable is nil' if id.nil?
  raise 'The ID of the durable must be a positive integer' unless id.positive?
  raise 'The "owner" of the lock is nil' if owner.nil?
  raise 'The "owner" of the lock may not be empty' if owner.empty?
  elapsed(@loog) do
    with_retries(max_tries: @retries, rescue: TimedOut) do
      checked(
        Typhoeus::Request.get(
          home.append('durables').append(id).append('unlock').add(owner:).to_s,
          headers:
        ),
        302
      )
    end
    throw :"Durable ##{id} unlocked at #{@host}"
  end
end

#enter(name, badge, why, job) ⇒ String

Enter a valve.

Parameters:

  • name (String)

    Name of the job

  • badge (String)

    Unique badge of the valve

  • why (String)

    The reason

  • job (nil|Integer)

    The ID of the job

Returns:

  • (String)

    The result just calculated or retrieved



549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
# File 'lib/baza-rb.rb', line 549

def enter(name, badge, why, job)
  elapsed(@loog, intro: "Entered valve #{badge} to #{name}") do
    with_retries(max_tries: @retries, rescue: TimedOut) do
      ret = checked(
        Typhoeus::Request.get(
          home.append('valves').append('result').add(badge:).to_s,
          headers:
        ),
        [200, 204]
      )
      return ret.body if ret.code == 200
      r = yield
      uri = home.append('valves').append('add')
        .add(name:)
        .add(badge:)
        .add(why:)
        .add(result: r.to_s)
      uri = uri.add(job:) unless job.nil?
      checked(Typhoeus::Request.post(uri.to_s, headers:), 302)
      r
    end
  end
end

#exit_code(id) ⇒ Integer

Read and return the exit code of the job.

Parameters:

  • id (Integer)

    The ID of the job on the server

Returns:

  • (Integer)

    The exit code



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/baza-rb.rb', line 202

def exit_code(id)
  raise 'The ID of the job is nil' if id.nil?
  raise 'The ID of the job must be a positive integer' unless id.positive?
  code = 0
  elapsed(@loog) do
    ret =
      with_retries(max_tries: @retries, rescue: TimedOut) do
        checked(
          Typhoeus::Request.get(
            home.append('exit').append("#{id}.txt").to_s,
            headers:
          )
        )
      end
    code = ret.body.to_i
    throw :"The exit code of the job ##{id} is #{code}"
  end
  code
end

#finish(id, zip) ⇒ Object

Submit a ZIP archvie to finish a job.

Parameters:

  • id (Integer)

    The ID of the job on the server

  • zip (String)

    The path to the ZIP file with the content of the archive



518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
# File 'lib/baza-rb.rb', line 518

def finish(id, zip)
  raise 'The "id" of the job is nil' if id.nil?
  raise 'The "id" of the job must be an integer' unless id.is_a?(Integer)
  raise 'The "zip" of the job is nil' if zip.nil?
  raise "The 'zip' file is absent: #{zip}" unless File.exist?(zip)
  elapsed(@loog) do
    with_retries(max_tries: @retries, rescue: TimedOut) do
      checked(
        Typhoeus::Request.put(
          home.append('finish').add(id:).to_s,
          connecttimeout: @timeout,
          timeout: @timeout,
          body: File.binread(zip),
          headers: headers.merge(
            'Content-Type' => 'application/octet-stream',
            'Content-Length' => File.size(zip)
          )
        )
      )
    end
    throw :"Pushed #{File.size(zip)} bytes to #{@host}, finished job ##{id}"
  end
end

#finished?(id) ⇒ Boolean

The job with this ID is finished already?

Parameters:

  • id (Integer)

    The ID of the job on the server

Returns:

  • (Boolean)

    TRUE if the job is already finished



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/baza-rb.rb', line 154

def finished?(id)
  raise 'The ID of the job is nil' if id.nil?
  raise 'The ID of the job must be a positive integer' unless id.positive?
  finished = false
  elapsed(@loog) do
    ret =
      with_retries(max_tries: @retries, rescue: TimedOut) do
        checked(
          Typhoeus::Request.get(
            home.append('finished').append(id).to_s,
            headers:
          )
        )
      end
    finished = ret.body == 'yes'
    throw :"The job ##{id} is #{finished ? '' : 'not yet '}finished at #{@host}"
  end
  finished
end

#lock(name, owner) ⇒ Object

Lock the name.

Parameters:

  • name (String)

    The name of the job on the server

  • owner (String)

    The owner of the lock (any string)



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/baza-rb.rb', line 250

def lock(name, owner)
  raise 'The "name" of the job is nil' if name.nil?
  raise 'The "name" of the job may not be empty' if name.empty?
  raise 'The "owner" of the lock is nil' if owner.nil?
  elapsed(@loog) do
    with_retries(max_tries: @retries, rescue: TimedOut) do
      checked(
        Typhoeus::Request.get(
          home.append('lock').append(name).add(owner:).to_s,
          headers:
        ),
        302
      )
    end
    throw :"Job name '#{name}' locked at #{@host}"
  end
end

#name_exists?(name) ⇒ Boolean

Check whether the name of the job exists on the server.

Parameters:

  • name (String)

    The name of the job on the server

Returns:

  • (Boolean)

    TRUE if such name exists



318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/baza-rb.rb', line 318

def name_exists?(name)
  raise 'The "name" of the job is nil' if name.nil?
  raise 'The "name" of the job may not be empty' if name.empty?
  exists = 0
  elapsed(@loog) do
    ret =
      with_retries(max_tries: @retries, rescue: TimedOut) do
        checked(
          Typhoeus::Request.get(
            home.append('exists').append(name).to_s,
            headers:
          )
        )
      end
    exists = ret.body == 'yes'
    throw :"The name \"#{name}\" #{exists ? 'exists' : "doesn't exist"} at #{@host}"
  end
  exists
end

#pop(owner, zip) ⇒ Boolean

Pop job from the server.

Parameters:

  • owner (String)

    Who is acting (could be any text)

  • zip (String)

    The path to ZIP archive to take

Returns:

  • (Boolean)

    TRUE if job taken, otherwise false



480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
# File 'lib/baza-rb.rb', line 480

def pop(owner, zip)
  raise 'The "zip" of the job is nil' if zip.nil?
  success = false
  FileUtils.rm_f(zip)
  elapsed(@loog) do
    File.open(zip, 'wb') do |f|
      request = Typhoeus::Request.new(
        home.append('pop').add(owner:).to_s,
        method: :get,
        headers: headers.merge(
          'Accept' => 'application/octet-stream'
        ),
        connecttimeout: @timeout,
        timeout: @timeout
      )
      request.on_body do |chunk|
        f.write(chunk)
      end
      with_retries(max_tries: @retries, rescue: TimedOut) do
        request.run
      end
      ret = request.response
      checked(ret, [200, 204])
      success = ret.code == 200
    end
    unless success
      FileUtils.rm_f(zip)
      throw :"Nothing to pop at #{@host}"
    end
    throw :"Popped #{File.size(zip)} bytes in ZIP archive at #{@host}"
  end
  success
end

#pull(id) ⇒ Bytes

Pull factbase from the server.

Parameters:

  • id (Integer)

    The ID of the job on the server

Returns:

  • (Bytes)

    Binary data pulled



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/baza-rb.rb', line 118

def pull(id)
  raise 'The ID of the job is nil' if id.nil?
  raise 'The ID of the job must be a positive integer' unless id.positive?
  data = 0
  elapsed(@loog) do
    Tempfile.open do |file|
      File.open(file, 'wb') do |f|
        request = Typhoeus::Request.new(
          home.append('pull').append("#{id}.fb").to_s,
          method: :get,
          headers: headers.merge(
            'Accept' => 'application/zip, application/factbase'
          ),
          accept_encoding: 'gzip',
          connecttimeout: @timeout,
          timeout: @timeout
        )
        request.on_body do |chunk|
          f.write(chunk)
        end
        with_retries(max_tries: @retries, rescue: TimedOut) do
          request.run
        end
        checked(request.response)
      end
      data = File.binread(file)
      throw :"Pulled #{data.size} bytes of job ##{id} factbase at #{@host}"
    end
  end
  data
end

#push(name, data, meta) ⇒ Integer

Push factbase to the server.

Parameters:

  • name (String)

    The name of the job on the server

  • data (Bytes)

    The data to push to the server (binary)

  • meta (Array<String>)

    List of metas, possibly empty

Returns:

  • (Integer)

    Job ID on the server



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/baza-rb.rb', line 79

def push(name, data, meta)
  raise 'The "name" of the job is nil' if name.nil?
  raise 'The "name" of the job may not be empty' if name.empty?
  raise 'The "data" of the job is nil' if data.nil?
  raise 'The "meta" of the job is nil' if meta.nil?
  id = 0
  hdrs = headers.merge(
    'Content-Type' => 'application/octet-stream',
    'Content-Length' => data.size
  )
  unless meta.empty?
    hdrs = hdrs.merge('X-Zerocracy-Meta' => meta.map { |v| Base64.encode64(v).gsub("\n", '') }.join(' '))
  end
  params = {
    connecttimeout: @timeout,
    timeout: @timeout,
    body: data,
    headers: hdrs
  }
  elapsed(@loog) do
    ret =
      with_retries(max_tries: @retries, rescue: TimedOut) do
        checked(
          Typhoeus::Request.put(
            home.append('push').append(name).to_s,
            @compress ? zipped(params) : params
          )
        )
      end
    id = ret.body.to_i
    throw :"Pushed #{data.size} bytes to #{@host}, job ID is ##{id}"
  end
  id
end

#recent(name) ⇒ Integer

Get the ID of the job by the name.

Parameters:

  • name (String)

    The name of the job on the server

Returns:

  • (Integer)

    The ID of the job on the server



294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/baza-rb.rb', line 294

def recent(name)
  raise 'The "name" of the job is nil' if name.nil?
  raise 'The "name" of the job may not be empty' if name.empty?
  job = 0
  elapsed(@loog) do
    ret =
      with_retries(max_tries: @retries, rescue: TimedOut) do
        checked(
          Typhoeus::Request.get(
            home.append('recent').append("#{name}.txt").to_s,
            headers:
          )
        )
      end
    job = ret.body.to_i
    throw :"The recent \"#{name}\" job's ID is ##{job} at #{@host}"
  end
  job
end

#stdout(id) ⇒ String

Read and return the stdout of the job.

Parameters:

  • id (Integer)

    The ID of the job on the server

Returns:

  • (String)

    The stdout, as a text



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/baza-rb.rb', line 178

def stdout(id)
  raise 'The ID of the job is nil' if id.nil?
  raise 'The ID of the job must be a positive integer' unless id.positive?
  stdout = ''
  elapsed(@loog) do
    ret =
      with_retries(max_tries: @retries, rescue: TimedOut) do
        checked(
          Typhoeus::Request.get(
            home.append('stdout').append("#{id}.txt").to_s,
            headers:
          )
        )
      end
    stdout = ret.body
    throw :"The stdout of the job ##{id} has #{stdout.split("\n").count} lines"
  end
  stdout
end

#unlock(name, owner) ⇒ Object

Unlock the name.

Parameters:

  • name (String)

    The name of the job on the server

  • owner (String)

    The owner of the lock (any string)



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/baza-rb.rb', line 272

def unlock(name, owner)
  raise 'The "name" of the job is nil' if name.nil?
  raise 'The "name" of the job may not be empty' if name.empty?
  raise 'The "owner" of the lock is nil' if owner.nil?
  elapsed(@loog) do
    with_retries(max_tries: @retries, rescue: TimedOut) do
      checked(
        Typhoeus::Request.get(
          home.append('unlock').append(name).add(owner:).to_s,
          headers:
        ),
        302
      )
    end
    throw :"Job name '#{name}' unlocked at #{@host}"
  end
end

#verified(id) ⇒ String

Read and return the verification verdict of the job.

Parameters:

  • id (Integer)

    The ID of the job on the server

Returns:

  • (String)

    The verdict



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/baza-rb.rb', line 226

def verified(id)
  raise 'The ID of the job is nil' if id.nil?
  raise 'The ID of the job must be a positive integer' unless id.positive?
  verdict = 0
  elapsed(@loog) do
    ret =
      with_retries(max_tries: @retries, rescue: TimedOut) do
        checked(
          Typhoeus::Request.get(
            home.append('jobs').append(id).append('verified.txt').to_s,
            headers:
          )
        )
      end
    verdict = ret.body
    throw :"The verdict of the job ##{id} is #{verdict.inspect}"
  end
  verdict
end