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: 5, loog: Loog::NULL) ⇒ Baza

rubocop:disable Metrics/ParameterLists



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

def initialize(host, port, token, ssl: true, timeout: 5, loog: Loog::NULL)
  # rubocop:enable Metrics/ParameterLists
  @host = host
  @port = port
  @ssl = ssl
  @token = token
  @timeout = timeout
  @loog = loog
end

Instance Method Details

#finished?(id) ⇒ Boolean

The job with this ID is finished already?

Returns:

  • (Boolean)


89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/judges/baza.rb', line 89

def finished?(id)
  finished = false
  elapsed(@loog) do
    ret = Typhoeus::Request.get(
      home.append('finished').append(id).to_s,
      headers:
    )
    check_code(ret)
    finished = ret.body == 'yes'
    throw :"The job ##{id} is #{finished ? '' : 'not yet '}finished at #{@host}"
  end
  finished
end

#name_exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/judges/baza.rb', line 117

def name_exists?(name)
  exists = 0
  elapsed(@loog) do
    ret = Typhoeus::Request.get(
      home.append('exists').append(name).to_s,
      headers:
    )
    check_code(ret)
    exists = ret.body == 'yes'
    throw :"The name \"#{name}\" #{exists ? 'exists' : "doesn't exist"} at #{@host}"
  end
  exists
end

#pull(id) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/judges/baza.rb', line 64

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:,
          connecttimeout: @timeout,
          timeout: @timeout
        )
        request.on_body do |chunk|
          f.write(chunk)
        end
        request.run
        check_code(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) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/judges/baza.rb', line 45

def push(name, data)
  id = 0
  elapsed(@loog) do
    ret = Typhoeus::Request.put(
      home.append('push').append(name).to_s,
      body: data,
      headers: headers.merge(
        'Content-type' => 'application/x-www-form-urlencoded'
      ),
      connecttimeout: @timeout,
      timeout: @timeout
    )
    check_code(ret)
    id = ret.body.to_i
    throw :"Pushed #{data.size} bytes to #{@host}, job ID is ##{id}"
  end
  id
end

#recent(name) ⇒ Object



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

def recent(name)
  job = 0
  elapsed(@loog) do
    ret = Typhoeus::Request.get(
      home.append('recent').append("#{name}.txt").to_s,
      headers:
    )
    check_code(ret)
    job = ret.body.to_i
    throw :"The recent \"#{name}\" job's ID is ##{job} at #{@host}"
  end
  job
end