Running system commands with logging
require 'open3'
module Runnable
class RunnableError < Exception; end
extend ActiveSupport::Concern
def logger
Rails.logger
end
def run(command, options = {})
message = "executing `#{command}`\n"
stdout, stderr, status = Open3.capture3(command)
if status.success?
message << stdout
logger.info message
else
message << stderr
logger.error message
raise RunnableError.new(stderr)
end
end
end
Written by Malik Baktiyarov
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Related Tags
#ruby
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#