I've created a program that will list all hung print jobs on a specified server, from there it will load them and strip the jobs down to their Job ID
then cancel the jobs on that server only.
I would like a critique of my work please.
- How does this look as an OOP script?
- Is there anything I can do differently?
- Is calling the
Net::SSH.start
twice really necessary?
Source:
#!/usr/local/bin/ruby
require 'rubygems'
require 'net/ssh'
require 'etc'
class PrintJobs
HOST = ARGV[0]
USERNAME = Etc.getlogin
PASSWORD = nil
def scan_for_jobs
check_jobs = Net::SSH.start(HOST, USERNAME, :password => PASSWORD) do |ssh|
cmd = "prt_jobs"
info = ssh.exec!(cmd)
if info == nil
puts "No print jobs on server #{HOST}"
else
res = info.split("\n").reject {|line| line.match(/\s+2016\s+/)}.join("\n")
puts res
print "Process into kill que: "
input = STDIN.gets.chomp.upcase
if input == "YES"
kill_que(check_jobs, res)
else
exit 1
end
end
end
end
def kill_que(check_jobs, res)
puts "Loading jobs into kill que.."
column = 0
job_ids = res.lines.map { |job| job.split(/\s+/)[0] }.each do |task_id|
kill_jobs(task_id)
end
end
def kill_jobs(task_id)
execute = Net::SSH.start(HOST, USERNAME, :password => PASSWORD)
id_to_strip = task_id.gsub(/\-/," ")
column = 1
stripping_id = id_to_strip.lines.map { |task| task.split(/\s+/)[1] }.each do |id|
id.strip
puts "Cancelling job: #{id}"
#`sudo cancel #{id}`
execute.exec!("sudo cancel #{id}")
end
end
end
killer = PrintJobs.new
killer.scan_for_jobs
Usage example:
closing_2-254128 root 933888 Thu 03 Dec 2015 03:49:13 PM CST
closing_2-254129 root 933888 Thu 03 Dec 2015 03:50:16 PM CST
laser42-254144 root 192512 Thu 03 Dec 2015 04:24:02 PM CST
laser42-254145 root 171008 Thu 03 Dec 2015 04:25:10 PM CST
ship_audit-254143 root 141312 Thu 03 Dec 2015 04:23:13 PM CST
Process into kill que: yes
Loading jobs into kill que..
Cancelling job: 254128
Cancelling job: 254129
Cancelling job: 254144
Cancelling job: 254145
Cancelling job: 254143
No print jobs on server
Net::SSH
too instead of calling it twice, try passing it as an argument to the method, call it, and do anexec!
on it. \$\endgroup\$