lib/invoker/commander.rb in invoker-1.0.3 vs lib/invoker/commander.rb in invoker-1.0.4
- old
+ new
@@ -1,8 +1,9 @@
require "io/console"
require 'pty'
require "json"
+require "dotenv"
module Invoker
class Commander
MAX_PROCESS_COUNT = 10
LABEL_COLORS = [:green, :yellow, :blue, :magenta, :cyan]
@@ -65,10 +66,15 @@
# Start executing given command by their label name.
#
# @param command_label [String] Command label of process specified in config file.
def add_command_by_label(command_label)
+ if process_running?(command_label)
+ Invoker::Logger.puts "\nProcess '#{command_label}' is already running".color(:red)
+ return false
+ end
+
process_info = Invoker::CONFIG.process(command_label)
if process_info
add_command(process_info)
end
end
@@ -140,10 +146,20 @@
Process.kill("INT", powerup_id)
rescue Errno::ESRCH; end
}
end
+ def load_env(directory = nil)
+ directory ||= ENV['PWD']
+ default_env = File.join(directory, '.env')
+ if File.exist?(default_env)
+ Dotenv::Environment.new(default_env)
+ else
+ {}
+ end
+ end
+
private
def start_event_loop
loop do
reactor.watch_on_pipe()
run_runnables()
@@ -202,21 +218,23 @@
def run_command(process_info, write_pipe)
command_label = process_info.label
event_manager.schedule_event(command_label, :exit) { remove_worker(command_label) }
+ env_options = load_env(process_info.dir)
+
spawn_options = {
:chdir => process_info.dir || ENV['PWD'], :out => write_pipe, :err => write_pipe,
:pgroup => true, :close_others => true, :in => :close
}
if defined?(Bundler)
Bundler.with_clean_env do
- spawn(process_info.cmd, spawn_options)
+ spawn(env_options, process_info.cmd, spawn_options)
end
else
- spawn(process_info.cmd, spawn_options)
+ spawn(env_options, process_info.cmd, spawn_options)
end
end
def wait_on_pid(command_label,pid)
raise Invoker::Errors::ToomanyOpenConnections if @thread_group.enclosed?
@@ -264,8 +282,12 @@
rescue Errno::ESRCH
puts "Error killing #{key}"
end
}
@workers = {}
+ end
+
+ def process_running?(command_label)
+ !!workers[command_label]
end
end
end