Class: Judges::Baza

Inherits:
Object show all
Defined in:
lib/judges/baza.rb

Overview

Baza.

Author

Yegor Bugayenko (yegor256@gmail.com)

Copyright

Copyright © 2024 Yegor Bugayenko

License

MIT

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Baza.



36
37
38
39
40
41
42
43
44
# File 'lib/judges/baza.rb', line 36

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

Instance Method Details

#finished?(id) ⇒ Boolean

The job with this ID is finished already?

Returns:

  • (Boolean)


102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/judges/baza.rb', line 102

def finished?(id)
  finished = false
  elapsed(@loog) do
    ret = with_retries(max_tries: @retries) 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.



120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/judges/baza.rb', line 120

def lock(name, owner)
  elapsed(@loog) do
    with_retries(max_tries: @retries) do
      checked(
        Typhoeus::Request.get(
          home.append('lock').append(name).add(owner:).to_s,
          headers:
        ),
        302
      )
    end
  end
end

#name_exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/judges/baza.rb', line 166

def name_exists?(name)
  exists = 0
  elapsed(@loog) do
    ret = with_retries(max_tries: @retries) 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

#pull(id) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/judges/baza.rb', line 73

def pull(id)
  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,
          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
        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) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/judges/baza.rb', line 46

def push(name, data, meta)
  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
  elapsed(@loog) do
    ret = with_retries(max_tries: @retries) do
      checked(
        Typhoeus::Request.put(
          home.append('push').append(name).to_s,
          body: data,
          headers: hdrs,
          connecttimeout: @timeout,
          timeout: @timeout
        )
      )
    end
    id = ret.body.to_i
    throw :"Pushed #{data.size} bytes to #{@host}, job ID is ##{id}"
  end
  id
end

#recent(name) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/judges/baza.rb', line 149

def recent(name)
  job = 0
  elapsed(@loog) do
    ret = with_retries(max_tries: @retries) 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

#unlock(name, owner) ⇒ Object

Unlock the name.



135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/judges/baza.rb', line 135

def unlock(name, owner)
  elapsed(@loog) do
    with_retries(max_tries: @retries) do
      checked(
        Typhoeus::Request.get(
          home.append('unlock').append(name).add(owner:).to_s,
          headers:
        ),
        302
      )
    end
  end
end