Skip to content

DB削除前にCloud Runサービスを停止して接続を切断#9357

Merged
komagata merged 1 commit intomainfrom
fix/stop-cloudrun-before-db-delete
Dec 11, 2025
Merged

DB削除前にCloud Runサービスを停止して接続を切断#9357
komagata merged 1 commit intomainfrom
fix/stop-cloudrun-before-db-delete

Conversation

@komagata
Copy link
Copy Markdown
Member

@komagata komagata commented Dec 11, 2025

Summary

  • Cloud Runサービスを--max-instances=0で停止することで、DBへの接続を確実に切断
  • Cloud SQLでは管理者権限がないためpg_terminate_backendが使えない問題を解決
  • 30秒の待機時間で接続のドレインを確保

変更内容

  1. StopCloudRunステップを追加 - Push後、DB操作前にCloud Runサービスを停止
  2. TerminateConnectionsWaitForProxyに名称変更・簡略化 - pg_terminate_backendのロジックを削除
  3. SqlProxyの開始をStopCloudRun完了後に変更
  4. 依存関係を適切に更新

背景

誰かがステージング環境にアクセスしているとDB削除ができずにデプロイが失敗する問題が長年ありました。Cloud SQLでは管理者権限を持つユーザーになることができないため、pg_terminate_backendで接続を切断することができませんでした。

解決策

Cloud Runサービス自体を停止することで、アプリケーションからのDB接続を確実に切断します。

Test plan

  • ステージング環境へのデプロイが成功することを確認
  • 誰かがアクセス中でもデプロイが成功することを確認

🤖 Generated with Claude Code

Summary by CodeRabbit

リリースノート

  • Chores
    • データベース接続管理とサービス停止のシーケンスを最適化しました。
    • クラウドインフラストラクチャのデプロイメントワークフローを改善し、信頼性と接続処理の堅牢性を向上させました。

✏️ Tip: You can customize this high-level summary in your review settings.

Cloud SQLでは管理者権限がないためpg_terminate_backendが使えない。
代わりにCloud Runサービスの--max-instances=0を設定することで、
アクティブな接続を確実に切断してからDBを削除する。

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Dec 11, 2025

Caution

Review failed

The pull request is closed.

ウォークスルー

Cloud Build ステージング パイプラインの接続管理フローを再構成します。Cloud Run サービスを停止するステップを追加し、直接的なデータベース接続の終了ロジックをアクティブな SQL Proxy 準備確認に置き換え、パイプライン ステップの依存関係を再編成します。

変更内容

コホート / ファイル 変更の要約
Cloud Build ステージング パイプラインの再構成
​.cloudbuild/cloudbuild-staging.yaml
StopCloudRun ステップを新規追加してサービスのデータベース接続を切断。TerminateConnections ステップを削除し、Rails ベースの接続終了ロジックを廃止。WaitForProxy ステップを追加して Cloud SQL Proxy の準備状況をポーリング。SqlProxyStopCloudRun の完了を待つよう設定。DeleteDBTerminateConnections から WaitForProxy への依存に変更。

推定コードレビュー工数

🎯 3 (標準的) | ⏱️ 約20分

  • Cloud Run サービス停止ロジックと Proxy 準備確認フローの動作確認
  • パイプライン ステップ間の依存関係と実行順序の妥当性検証
  • タイムアウト値(30秒)とポーリング間隔の適切性確認

関連する可能性のあるプルリクエスト

推奨ラベル

雑事

推奨レビュアー

  • okuramasafumi

🐰 クラウドの流れを整えて
ステップの順序 優雅に
サーバー停止して繋がり切れ
Proxy 待ちて安らかに
データベース削除 静かに

✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/stop-cloudrun-before-db-delete

📜 Recent review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7cd98dd and 918f900.

📒 Files selected for processing (1)
  • .cloudbuild/cloudbuild-staging.yaml (3 hunks)

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant