diff --git a/lib/blue_green_process.rb b/lib/blue_green_process.rb index c7c40f8..8080dc2 100644 --- a/lib/blue_green_process.rb +++ b/lib/blue_green_process.rb @@ -75,5 +75,7 @@ def self.terminate_workers_immediately BlueGreenProcess.logger.warn "[BLUE_GREEN_PROCESS][#{$PROCESS_ID}] workerプロセスへTERMシグナルを送信しました" Process.waitall BlueGreenProcess.logger.warn "[BLUE_GREEN_PROCESS][#{$PROCESS_ID}] workerプロセスが終了しました" + ensure + FileUtils.rm_rf(BlueGreenProcess::PID_PATH) end end diff --git a/lib/blue_green_process/master_process.rb b/lib/blue_green_process/master_process.rb index edb474b..75245d1 100644 --- a/lib/blue_green_process/master_process.rb +++ b/lib/blue_green_process/master_process.rb @@ -115,6 +115,9 @@ def work def shutdown @processes.each(&:shutdown) Process.waitall + ensure + # 正常終了だとworkerから削除するケースはないんだけど、想定外のエラーが起きた時を考慮してworkerから削除する + FileUtils.rm_rf(BlueGreenProcess::PID_PATH) end private diff --git a/spec/blue_green_process_spec.rb b/spec/blue_green_process_spec.rb index 80ab1ef..e30a978 100644 --- a/spec/blue_green_process_spec.rb +++ b/spec/blue_green_process_spec.rb @@ -43,4 +43,23 @@ described_class.config.after_fork.call end end + + describe ".terminate_workers_immediately" do + let(:worker_instance) { worker_class.new } + let(:worker_class) do + Class.new(BlueGreenProcess::BaseWorker) do + def work(label) + puts "work #{label}" + end + end + end + + it 'workerプロセスを終了すること' do + process = BlueGreenProcess.new(worker_instance: worker_instance, max_work: 3) + process.work + BlueGreenProcess.terminate_workers_immediately + expect(Process.waitall).to eq([]) + expect(File.exist?(BlueGreenProcess::PID_PATH)).to eq(false) + end + end end diff --git a/spec/integration/after_fork_spec.rb b/spec/integration/after_fork_spec.rb index cbf64b2..82c4b16 100644 --- a/spec/integration/after_fork_spec.rb +++ b/spec/integration/after_fork_spec.rb @@ -56,6 +56,7 @@ def work(label) "green" * 2, "blue" * 2].join ) + expect(File.exist?(BlueGreenProcess::PID_PATH)).to eq(false) end end end