Skip to content

Instantly share code, notes, and snippets.

@wuputah
Last active December 19, 2015 01:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wuputah/5880052 to your computer and use it in GitHub Desktop.
Save wuputah/5880052 to your computer and use it in GitHub Desktop.
Running a command via Rendezvous and capturing the output.

This example captures the output of running a command and then prints that output to STDOUT along with an execution summary.

Usage

bundle install --path .bundle
./example.rb [APP] [COMMAND]
#!/usr/bin/env ruby
require 'bundler/setup'
require 'rendezvous'
require 'heroku-api'
require 'netrc'
require 'benchmark'
if ARGV.size < 2
puts "Usage: example.rb APP COMMAND"
exit
end
_, api_key = Netrc.read['api.heroku.com']
heroku = Heroku::API.new(api_key: api_key)
data = heroku.post_ps(
ARGV[0],
ARGV[1..-1].join(' '),
attach: true,
ps_env: {}
).body
in_rd, in_wr = IO.pipe
out_rd, out_wr = IO.pipe
in_wr.sync = true
in_wr.write(4.chr) # EOF
in_wr.flush
output = ""
thread = Thread.new do
begin
loop do
output << out_rd.readpartial(10000)
end
rescue EOFError
end
end
time = Benchmark.realtime do
Rendezvous.start(
url: data['rendezvous_url'],
input: in_rd,
output: out_wr
)
end
# send the thread an EOF and wait for it to complete
out_wr.close
thread.join
puts "-" * 40
puts "App: #{ARGV[0]}"
puts "Command: #{ARGV[1..-1].join(' ')}"
puts "Execution time: #{time}"
puts "-" * 40
puts "Output"
puts "-" * 40
puts output
puts "-" * 40
source 'https://rubygems.org/'
gem 'rendezvous'
gem 'heroku-api'
gem 'netrc'
GEM
remote: https://rubygems.org/
specs:
excon (0.25.0)
heroku-api (0.3.13)
excon (~> 0.25.0)
netrc (0.7.7)
rendezvous (0.0.2)
PLATFORMS
ruby
DEPENDENCIES
heroku-api
netrc
rendezvous
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment