Class: Judges::Upload
Overview
The upload command, to send a durable to Zerocracy.
This class is instantiated by the bin/judges command line interface. You are not supposed to instantiate it yourself.
- Author
-
Yegor Bugayenko (yegor256@gmail.com)
- Copyright
-
Copyright © 2024-2026 Yegor Bugayenko
- License
-
MIT
Instance Method Summary collapse
-
#initialize(loog) ⇒ Upload
constructor
Initialize.
-
#run(opts, args) ⇒ Object
Run the upload command (called by the
bin/judgesscript).
Constructor Details
#initialize(loog) ⇒ Upload
Initialize.
23 24 25 |
# File 'lib/judges/commands/upload.rb', line 23 def initialize(loog) @loog = loog end |
Instance Method Details
#run(opts, args) ⇒ Object
Run the upload command (called by the bin/judges script). rubocop:disable Metrics/MethodLength
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/judges/commands/upload.rb', line 32 def run(opts, args) raise(ArgumentError, 'Exactly two arguments required') unless args.size == 2 jname = args[0] path = args[1] raise(StandardError, "File not found: #{path}") unless File.exist?(path) name = File.basename(path) baza = BazaRb.new( opts['host'], opts['port'].to_i, opts['token'], ssl: opts['ssl'], timeout: (opts['timeout'] || 30).to_i, loog: @loog, retries: (opts['retries'] || 3).to_i ) elapsed(@loog, level: Logger::INFO) do id = baza.durable_find(jname, name) if id.nil? || id.to_s.strip.empty? tmp = Dir.mktmpdir begin f = File.join(tmp, name) File.write(f, 'placeholder') id = baza.durable_place(jname, f) @loog.info("Placed a placeholder to new durable '#{name}' in '#{jname}' (ID: #{id})") ensure FileUtils.rm_rf(tmp, secure: true) end end size = File.size(path) id = id.to_i baza.durable_lock(id, opts['owner'] || 'default') begin baza.durable_save(id, path) throw(:"👍 Uploaded #{path} to existing durable '#{name}' in '#{jname}' (ID: #{id}, #{size} bytes)") ensure baza.durable_unlock(id, opts['owner'] || 'default') end end end |