Skip to content

Commit

Permalink
処理を停止するときにpidを書いたファイルも削除する
Browse files Browse the repository at this point in the history
  • Loading branch information
jiikko committed Jan 8, 2024
1 parent 9d15dd5 commit 62ee982
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/blue_green_process.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions lib/blue_green_process/master_process.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ def work
def shutdown
@processes.each(&:shutdown)
Process.waitall
ensure
# 正常終了だとworkerから削除するケースはないんだけど、想定外のエラーが起きた時を考慮してworkerから削除する
FileUtils.rm_rf(BlueGreenProcess::PID_PATH)
end

private
Expand Down
19 changes: 19 additions & 0 deletions spec/blue_green_process_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions spec/integration/after_fork_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def work(label)
"green" * 2,
"blue" * 2].join
)
expect(File.exist?(BlueGreenProcess::PID_PATH)).to eq(false)
end
end
end
Expand Down

0 comments on commit 62ee982

Please sign in to comment.