Add support for starting procfile in tmux session

This commit is contained in:
Henry Hsu
2012-06-07 22:12:52 -07:00
parent a87a882e60
commit f745b16217
9 changed files with 142 additions and 1 deletions
+14 -1
View File
@@ -1,6 +1,7 @@
require "foreman"
require "foreman/helpers"
require "foreman/engine"
require "foreman/tmux_engine"
require "foreman/export"
require "shellwords"
require "thor"
@@ -10,6 +11,7 @@ class Foreman::CLI < Thor
include Foreman::Helpers
class_option :procfile, :type => :string, :aliases => "-f", :desc => "Default: Procfile"
class_option :tmux, :type => :boolean, :aliases => "-t", :desc => "Run in tmux session"
desc "start [PROCESS]", "Start the application (or a specific PROCESS)"
@@ -73,6 +75,17 @@ class Foreman::CLI < Thor
end
end
class << self
def new_engine(procfile, options)
@engine_class ||= options[:tmux] ? Foreman::TmuxEngine : Foreman::Engine
@engine_class.new(procfile, options)
end
def engine_class=(klass)
@engine_class = klass
end
end
private ######################################################################
def check_procfile!
@@ -80,7 +93,7 @@ private ######################################################################
end
def engine
@engine ||= Foreman::Engine.new(procfile, options)
@engine ||= self.class.new_engine(procfile, options)
end
def procfile
+35
View File
@@ -0,0 +1,35 @@
require "foreman"
require "foreman/engine"
class Foreman::TmuxEngine < Foreman::Engine
attr_reader :procfile
attr_reader :session
def initialize(procfile, options={})
@procfile = Foreman::Procfile.new(procfile)
@options = options.dup
@session = Time.now.to_i
end
def start
assign_colors
concurrency = Foreman::Utils.parse_concurrency(@options[:concurrency])
%x{tmux new-session -d -s #{session}}
procfile.entries.each_with_index do |entry, index|
name = "#{entry.name}.#{concurrency[entry.name]}"
if index == 0
%x{tmux rename-window -t #{session}:#{index} #{name}}
else
%x{tmux new-window -t #{session}:#{index} -n #{name}}
end
%x{tmux pipe-pane -o -t #{session}:#{index} "gawk '{ printf \\"%%s\\", \\"#{$stdout.color(colors[entry.name])}\\"; print strftime(\\"%%H:%%M:%%S\\"), \\"#{pad_process_name(name)} | #{$stdout.color(:reset)}\\", \\$0; fflush(); }' >> /tmp/foreman.#{session}.log"}
%x{tmux send-keys -t #{session}:#{index} "#{entry.command}" C-m}
end
last_index = procfile.entries.length
%x{tmux new-window -t #{session}:#{last_index} -n all}
%x{tmux send-keys -t #{session}:#{last_index} "tail -f /tmp/foreman.#{session}.log" C-m}
Kernel.exec("tmux attach-session -t #{session}")
end
end