Skip to content

Commit

Permalink
#14 /pop and /finish
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Aug 30, 2024
1 parent 1bfee5b commit 0accbf0
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
67 changes: 67 additions & 0 deletions lib/baza-rb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
require 'retries'
require 'tago'
require 'typhoeus'
require 'fileutils'
require_relative 'baza-rb/version'

# Interface to the API of zerocracy.com.
Expand Down Expand Up @@ -436,6 +437,72 @@ def durable_unlock(id, owner)
end
end

# Pop job from the server.
# @param [String] owner Who is acting (could be any text)
# @param [String] zip The path to ZIP archive to take
# @return [Boolean] TRUE if job taken, otherwise false
def pop(owner, zip)
raise 'The "zip" of the job is nil' if zip.nil?
raise "The 'zip' file is absent: #{zip}" unless File.exist?(zip)
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) 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

# Submit a ZIP archvie to finish a job.
# @param [Integer] id The ID of the job on the server
# @param [String] zip The path to the ZIP file with the content of the archive
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) 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

private

def headers
Expand Down
17 changes: 17 additions & 0 deletions test/test_baza-rb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,23 @@ def test_simple_push
)
end

def test_simple_pop
WebMock.disable_net_connect!
stub_request(:get, 'https://example.org/pop?owner=me').to_return(status: 204)
Tempfile.open do |zip|
assert(!BazaRb.new('example.org', 443, '000').pop('me', zip.path))
assert(!File.exist?(zip.path))
end
end

def test_simple_finish
WebMock.disable_net_connect!
stub_request(:put, 'https://example.org/finish?id=42').to_return(status: 200)
Tempfile.open do |zip|
BazaRb.new('example.org', 443, '000').finish(42, zip.path)
end
end

def test_simple_recent_check
WebMock.disable_net_connect!
stub_request(:get, 'https://example.org/recent/simple.txt')
Expand Down

0 comments on commit 0accbf0

Please sign in to comment.