class Ruboty::ExecCommand::Command
Attributes
pid[R]
start_at[R]
Public Class Methods
all()
click to toggle source
# File lib/ruboty/exec_command/command.rb, line 30 def all files.map do |e| Command.new(absolute_path: e) end end
command?(path)
click to toggle source
# File lib/ruboty/exec_command/command.rb, line 20 def command?(path) File.executable?(path) and not File.directory?(path) end
command_root()
click to toggle source
# File lib/ruboty/exec_command/command.rb, line 12 def command_root "#{ruboty_root}/commands" end
files()
click to toggle source
# File lib/ruboty/exec_command/command.rb, line 24 def files Dir[command_root+'/**/*'].select do |path| command?(path) end end
new(args={})
click to toggle source
# File lib/ruboty/exec_command/command.rb, line 37 def initialize(args={}) args = { absolute_path: nil, command_args: nil }.merge(args) @absolute_path = args[:absolute_path] @command_args = args[:command_args].split if not args[:command_args].nil? @pid = nil @start_at = nil end
output_root()
click to toggle source
# File lib/ruboty/exec_command/command.rb, line 16 def output_root ENV['EXEC_COMMAND_OUTPUT_ROOT'] || "#{ruboty_root}/logs/exec_command" end
ruboty_root()
click to toggle source
# File lib/ruboty/exec_command/command.rb, line 8 def ruboty_root "#{ENV['RUBOTY_ROOT'] || Dir.pwd}" end
Public Instance Methods
__command2path(path, args)
click to toggle source
# File lib/ruboty/exec_command/command.rb, line 60 def __command2path(path, args) if self.class.command?(path) [path, args] else if args == [] # command not found return ["", ""] else __command2path("#{path}/#{args[0]}", args.slice(1, args.length)) end end end
absolute_path()
click to toggle source
# File lib/ruboty/exec_command/command.rb, line 48 def absolute_path @absolute_path ||= command2path[0] end
command2path()
click to toggle source
# File lib/ruboty/exec_command/command.rb, line 73 def command2path path = self.class.command_root __command2path "#{path}/#{@command_args[0]}", @command_args.slice(1, @command_args.length) end
command_name()
click to toggle source
# File lib/ruboty/exec_command/command.rb, line 56 def command_name @command_name ||= relative_path.gsub('/', ' ') end
help()
click to toggle source
# File lib/ruboty/exec_command/command.rb, line 144 def help run(args=['-h']).chomp end
opt_args()
click to toggle source
# File lib/ruboty/exec_command/command.rb, line 79 def opt_args @opt_args ||= command2path[1] end
output_dir()
click to toggle source
# File lib/ruboty/exec_command/command.rb, line 91 def output_dir d = ENV['EXEC_COMMAND_OUTPUT_DIR'] || "#{self.class.output_root}/#{this_month}" FileUtils.mkdir_p(d) if not Dir.exists?(d) d end
output_file_name()
click to toggle source
# File lib/ruboty/exec_command/command.rb, line 102 def output_file_name %Q(#{output_dir}/#{command_name.gsub(" ", "_")}-#{start_at}) end
output_files()
click to toggle source
return temporary output file name [stdout, stderr]
# File lib/ruboty/exec_command/command.rb, line 112 def output_files ["#{output_file_name}.out", "#{output_file_name}.err"] end
relative_path()
click to toggle source
# File lib/ruboty/exec_command/command.rb, line 52 def relative_path @relative_path ||= absolute_path.sub(/^#{self.class.command_root}\//,"") end
run(args=[])
click to toggle source
# File lib/ruboty/exec_command/command.rb, line 126 def run(args=[]) `#{absolute_path} #{args.join(" ")}` end
run_bg(args=[])
click to toggle source
# File lib/ruboty/exec_command/command.rb, line 130 def run_bg(args=[]) @start_at = this_time stdout, stderr = output_files stdout_link, stderr_link = symlink_files FileUtils.ln_sf(stdout, stdout_link) FileUtils.ln_sf(stderr, stderr_link) cmd = %Q(#{absolute_path} #{args.join(" ")}) with_clean_env do @pid = Process.spawn(cmd, pgroup: true, out: stdout, err: stderr) end Ruboty.logger.debug { "[EXEC_COMMAND] Invoked `#{cmd}`. PID: #{@pid}" } @pid end
stderr_log()
click to toggle source
return contents of stderr
# File lib/ruboty/exec_command/command.rb, line 122 def stderr_log File.open(output_files[1]).read end
stdout_log()
click to toggle source
return contents of stdout
# File lib/ruboty/exec_command/command.rb, line 117 def stdout_log File.open(output_files[0]).read end
symlink_file_name()
click to toggle source
symlink to output_file_name
so that we can easily tail -F
# File lib/ruboty/exec_command/command.rb, line 98 def symlink_file_name %Q(#{output_dir}/#{command_name.gsub(" ", "_")}) end
symlink_files()
click to toggle source
return symlink output file name [stdout, stderr]
# File lib/ruboty/exec_command/command.rb, line 107 def symlink_files ["#{symlink_file_name}.out", "#{symlink_file_name}.err"] end
this_month()
click to toggle source
# File lib/ruboty/exec_command/command.rb, line 83 def this_month Time.now.strftime "%Y-%m" end
this_time()
click to toggle source
# File lib/ruboty/exec_command/command.rb, line 87 def this_time Time.now.strftime "%Y-%m-%d_%H:%M:%S" end
with_clean_env() { || ... }
click to toggle source
# File lib/ruboty/exec_command/command.rb, line 148 def with_clean_env(&block) if defined?(::Bundler) ::Bundler.with_clean_env do yield end else yield end end