Skip to content

Commit

Permalink
fix: some cops
Browse files Browse the repository at this point in the history
  • Loading branch information
rfdonnelly committed Nov 3, 2023
1 parent 6b7227b commit 6c3760a
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 27 deletions.
18 changes: 8 additions & 10 deletions lib/jobrnr/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,14 @@ def initialize(argv)
end

def run
begin
run_with_exceptions
rescue ::OptionParser::ParseError, Jobrnr::UsageError => e
Jobrnr::Log.error [e.message, "See `jobrnr --help`"].join("\n\n")
rescue Jobrnr::HelpException => e
puts e.message
exit 0
rescue Jobrnr::Error => e
Jobrnr::Log.error e.message
end
run_with_exceptions
rescue ::OptionParser::ParseError, Jobrnr::UsageError => e
Jobrnr::Log.error [e.message, "See `jobrnr --help`"].join("\n\n")
rescue Jobrnr::HelpException => e
puts e.message
exit 0
rescue Jobrnr::Error => e
Jobrnr::Log.error e.message
end

def run_with_exceptions
Expand Down
2 changes: 1 addition & 1 deletion lib/jobrnr/job/definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def evaluate_command
if command.respond_to?(:call)
command.call
else
eval('"' + command + '"')
eval(format('"%s"', command))
end
end

Expand Down
7 changes: 6 additions & 1 deletion lib/jobrnr/job/instance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,12 @@ def execute
begin
@pid = spawn([command, command], *argv, %i[out err] => log, :pgroup => true)
rescue StandardError => e
File.write(log, format("ERROR: failed to spawn command '%s' for job '%s': %s", @command, job.id, e.to_s))
File.write(log, format(
"ERROR: failed to spawn command '%<command>s' for job '%<job>s': %<cause>s",
command: @command,
job: job.id,
cause: e.to_s,
))
@exit_status = false
else
@pid, status = Process.waitpid2(pid)
Expand Down
6 changes: 4 additions & 2 deletions lib/jobrnr/table.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Jobrnr
class Table
def initialize(header:, rows:)
Expand All @@ -8,7 +10,7 @@ def initialize(header:, rows:)

def render
data = [@header, *@rows]
data.map! { |row| row.map! { |cell| cell.to_s } }
data.map! { |row| row.map!(&:to_s) }
widths = calculate_widths(data)
# NOTE: Previously used the Ruby %-<width>s string format specifier but
# it does not handle colors well so we roll our own formatting here.
Expand All @@ -28,7 +30,7 @@ def calculate_widths(data)
widths = row
.map { |cell| @pastel.strip(cell).size }
.zip(widths)
.map { |pair| pair.max }
.map(&:max)
end

widths
Expand Down
25 changes: 12 additions & 13 deletions lib/jobrnr/ui.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class UI

DEFAULT_TIME_SLICE_INTERVAL = 1

KEYS = Hash.new do |h, k|
KEYS = Hash.new do |_, k|
k.chr
end.merge({
3 => :ctrl_c,
Expand Down Expand Up @@ -96,18 +96,16 @@ def stop_submission?
end

def parse_integer(type_name, &block)
begin
n = Integer($stdin.gets)
block.call(n)
rescue ::ArgumentError
$stderr.puts "could not parse #{type_name}"
end
n = Integer($stdin.gets)
block.call(n)
rescue ::ArgumentError
warn "could not parse #{type_name}"
end

def instance_by_slot(slot, &block)
inst = @instances.find { |inst| inst.slot == slot }
if inst.nil?
$stderr.puts "invalid slot"
warn "invalid slot"
else
block.call(inst)
end
Expand All @@ -130,6 +128,7 @@ def restart_instance(inst)
def process_input
c = $stdin.getch(min: 0, time: @time_slice_interval)
return unless c

case KEYS[c.ord]
when :ctrl_c
sigint
Expand Down Expand Up @@ -157,17 +156,17 @@ def process_input
when "a"
insts = pool
.instances
.sort_by { |inst| inst.duration }
.sort_by(&:start_time)
.reverse
print_insts(insts, "active")
when "c"
insts = @passed
.chain(@failed)
.sort_by { |inst| inst.end_time }
.sort_by(&:end_time)
print_insts(insts, "completed")
when "f"
insts = @failed
.sort_by { |inst| inst.end_time }
.sort_by(&:end_time)
print_insts(insts, "failed")
when "j"
$stdout.write format("max-jobs (%d): ", slots.size)
Expand All @@ -193,7 +192,7 @@ def process_input
end
when "p"
insts = @passed
.sort_by { |inst| inst.end_time }
.sort_by(&:end_time)
print_insts(insts, "passed")
when "r"
$stdout.write "restart job (slot): "
Expand All @@ -219,7 +218,7 @@ def print_insts(insts, type = nil)
$stdout.puts ["No", type, "jobs present"].flatten.join(" ")
else
$stdout.puts Jobrnr::Table.new(
header: %w(Slot Status Duration Command),
header: %w[Slot Status Duration Command],
rows: data,
).render
end
Expand Down

0 comments on commit 6c3760a

Please sign in to comment.