Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dev ctrl_c fix #363

Merged
merged 3 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ gem_build_install:
gem_uninstall:
gem uninstall uffizzi-cli

gem_reinstall:
ls -la | grep uffizzi-cli- | rm -f
make gem_uninstall
make gem_build_install

brew_add_tap:
brew tap UffizziCloud/tap

Expand Down
14 changes: 13 additions & 1 deletion lib/uffizzi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,19 @@ def root
end

def process
@process ||= Process
Process
end

def signal
Signal
end

def thread
Thread
end

def at_exit(&block)
Kernel.at_exit(&block)
end
end
end
8 changes: 4 additions & 4 deletions lib/uffizzi/cli/dev.rb
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,8 @@ def parse_kubeconfig(kubeconfig)
def launch_demonise_skaffold(config_path)
Uffizzi.process.daemon(true)

at_exit do
DevService.delete_pid
Uffizzi.at_exit do
DevService.stop_process
end

DevService.save_pid
Expand All @@ -235,8 +235,8 @@ def launch_demonise_skaffold(config_path)
end

def launch_basic_skaffold(config_path)
at_exit do
DevService.delete_pid
Uffizzi.at_exit do
DevService.stop_process
end

DevService.save_pid
Expand Down
52 changes: 42 additions & 10 deletions lib/uffizzi/services/dev_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,27 @@ def stop_process
dev_pid = running_pid
skaffold_pid = running_skaffold_pid

Uffizzi.process.kill('INT', skaffold_pid)
Uffizzi.process.kill('INT', dev_pid)
begin
Uffizzi.process.kill('INT', skaffold_pid)
rescue Errno::ESRCH
end

wait_process_stop(skaffold_pid)
delete_pid

Uffizzi.process.kill('INT', dev_pid)
rescue Errno::ESRCH
delete_pid
end

def wait_process_stop(pid)
loop do
Uffizzi.process.kill(0, pid)
sleep(1)
end
rescue Errno::ESRCH
end

def process_running?
pid = running_pid
return false unless pid.positive?
Expand All @@ -50,9 +64,9 @@ def process_running?
end

def start_check_pid_file_existence
Thread.new do
Uffizzi.thread.new do
loop do
Uffizzi.process.kill('QUIT', Uffizzi.process.pid) unless File.exist?(pid_path)
stop_process unless File.exist?(pid_path)
sleep(1)
end
end
Expand All @@ -62,6 +76,8 @@ def start_basic_skaffold(config_path, options)
Uffizzi.ui.say('Start skaffold')
cmd = build_skaffold_dev_command(config_path, options)

Uffizzi.signal.trap('INT') {}

Uffizzi.ui.popen2e(cmd) do |_stdin, stdout_and_stderr, wait_thr|
pid = wait_thr.pid
skaffold_pid = find_skaffold_pid(pid)
Expand Down Expand Up @@ -198,13 +214,29 @@ def dev_environment
Uffizzi::ConfigHelper.dev_environment
end

def find_skaffold_pid(ppid)
ppid_regex = /\w*\s+\d+\s+#{ppid}.*\sskaffold dev/
pid_regex = /\w*\s+(\d+)\s+#{ppid}.*\sskaffold dev/

def find_skaffold_pid(pid)
pid_regex = /\w*#{pid}.*skaffold dev/
io = Uffizzi.ui.popen('ps -ef')
ps = io.readlines.detect { |l| l.match?(ppid_regex) }
ps.match(pid_regex)[1]
processes = io.readlines.select { |l| l.match?(pid_regex) }

if processes.count.zero?
raise StandardError.new('Can\'t find skaffold process pid')
end

# HACK: For MacOS
if processes.count == 1
current_pid = processes[0].gsub(/\s+/, ' ').lstrip.split[1]
return pid if current_pid.to_s == pid.to_s

raise StandardError.new('Can\'t find skaffold process pid')
end

# HACK: For Linux
parent_process = processes
.map { |ps| ps.gsub(/\s+/, ' ').lstrip.split }
.detect { |ps| ps[2].to_s == pid.to_s }

parent_process[1]
end
end
end
1 change: 0 additions & 1 deletion test/support/mocks/mock_prompt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ def promise_question_answer(question, answer)

def get_answer(question)
answer_index = @question_answers.index do |question_answer|
question_answer[:question] == question
case question_answer[:question]
when Regexp
question_answer[:question].match?(question)
Expand Down
7 changes: 7 additions & 0 deletions test/support/mocks/mock_signal.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class MockSignal
class << self
def trap(_sig); end
end
end
5 changes: 5 additions & 0 deletions test/support/mocks/mock_thread.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

class MockThread
def initialize; end
end
8 changes: 6 additions & 2 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Minitest::Test
TEST_CONFIG_DIR = 'tmp/config_default.json'
TEST_TOKEN_PATH = 'tmp/token_default.json'
TEST_PID_PATH = 'tmp/dev.pid'
TEST_PID_PATH = 'tmp/skaffold_dev.pid'
TEST_SKAFFOLD_PID_PATH = 'tmp/skaffold_dev.pid'
TEST_DEV_LOGS_PATH = 'tmp/dev-logs.txt'

def before_setup
Expand All @@ -49,11 +49,14 @@ def before_setup
Uffizzi.stubs(:ui).returns(@mock_shell)
Uffizzi.stubs(:prompt).returns(@mock_prompt)
Uffizzi.stubs(:process).returns(@mock_process)
Uffizzi.stubs(:signal).returns(MockSignal)
Uffizzi.stubs(:thread).returns(MockSignal)
Uffizzi.stubs(:at_exit).returns(nil)
Uffizzi::ConfigFile.stubs(:config_path).returns(TEST_CONFIG_PATH)
Uffizzi::Token.stubs(:token_path).returns(TEST_TOKEN_PATH)
Uffizzi::ConfigFile.stubs(:config_path).returns(TEST_CONFIG_PATH)
DevService.stubs(:pid_path).returns(TEST_PID_PATH)
DevService.stubs(:skaffold_pid_path).returns(TEST_PID_PATH)
DevService.stubs(:skaffold_pid_path).returns(TEST_SKAFFOLD_PID_PATH)
DevService.stubs(:logs_path).returns(TEST_DEV_LOGS_PATH)
end

Expand All @@ -70,6 +73,7 @@ def before_teardown
File.delete(TEST_CONFIG_PATH) if File.exist?(TEST_CONFIG_PATH)
File.delete(TEST_TOKEN_PATH) if File.exist?(TEST_TOKEN_PATH)
File.delete(TEST_PID_PATH) if File.exist?(TEST_PID_PATH)
File.delete(TEST_SKAFFOLD_PID_PATH) if File.exist?(TEST_SKAFFOLD_PID_PATH)
File.delete(TEST_DEV_LOGS_PATH) if File.exist?(TEST_DEV_LOGS_PATH)
end

Expand Down
Loading