Skip to content

Commit

Permalink
dev ctrl_c fix
Browse files Browse the repository at this point in the history
  • Loading branch information
zipofar committed Oct 24, 2023
1 parent 0d39ed7 commit bb941e7
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 12 deletions.
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:
rm uffizzi-cli-*.gem -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
24 changes: 20 additions & 4 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
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

0 comments on commit bb941e7

Please sign in to comment.