Skip to content

無駄実装の削除とパフォーマンス改善(waste audit 第2弾)#29

Merged
dendencat merged 3 commits into
mainfrom
chore/waste-audit-and-performance
Jul 18, 2026
Merged

無駄実装の削除とパフォーマンス改善(waste audit 第2弾)#29
dendencat merged 3 commits into
mainfrom
chore/waste-audit-and-performance

Conversation

@dendencat

Copy link
Copy Markdown
Owner

概要

CMS 全体を「無駄な実装」「処理速度を落としている実装」の観点で監査し(#27/#28 の waste audit の続き)、検出した問題をすべて修正しました。実装は Codex CLI、指揮・レビューは Claude が担当。-754 行 / +450 行

無駄の削除

  • 孤立した重複 Django プロジェクト app/ ツリーを削除(どの Docker 構成からも未参照)
  • ゴミファイル削除: techblog_cms/import os.py(不正なファイル名の死んだテスト)、未使用の index.htmlstatic/js/articles.js、重複テストデータスクリプト 2 本
  • 壊れていた Django admin ルーティングを完全撤去(HIDE_ADMIN_URL ハードコードで到達不能、フラグ切替時は無限リダイレクト)。/admin/ は常に 404
  • 未使用依存 6 件を requirements.txt から削除(whitenoise / django-compressor / django-cors-headers / django-csp / sentry-sdk / django-extensions)
  • 未使用 Docker 資材を削除: ビルド不能な DockerfileDockerfile.nginx.staticnginx/Dockerfile、孤立 nginx.conf、nginx と二重配信だった compose static サービス
  • どこからも読み込まれていなかった settings_production.py を base に統合して削除

パフォーマンス改善

項目 内容
DB 接続 CONN_MAX_AGE=60(従来は毎リクエスト再接続)
N+1 解消 select_related('category') / annotate(Count)記事 15 件で /articles/ が 2 クエリ(実測)
ページネーション 記事・カテゴリ詳細・タグ詳細を 10 件/頁に
Markdown レンダリング結果を Redis に 24h キャッシュ(updated_at キーで自動無効化)、Pygments guess_lang 無効化
サイドバー context processor + 300 秒キャッシュ + シグナル無効化
nginx gzip 有効化、proxy_buffering on(sync worker の占有解消)
gunicorn reload=Falseloglevel=info・worker 数を環境変数化
インデックス (published, -created_at) 複合インデックス追加
migrations 欠落していた migrations を整備(initial + index)、entrypoint を --fake-initial
画像 アップロード時に長辺 1920px へリサイズ + EXIF 回転補正(GIF は素通し)
テンプレート 非 DEBUG 時に cached.Loader

E2E 検証で発見した既存バグの修正

  • Dockerfile.nginx266aeab で削除済みの static/ を COPY していてビルド不能 → 冗長 COPY を削除
  • DEBUG=False 時に SSL リダイレクトがコンテナ内部 healthcheck まで HTTPS に飛ばし恒常的に unhealthy → SECURE_REDIRECT_EXEMPT を追加
  • redis が --requirepass 起動なのに CACHES に認証情報が渡らずキャッシュ・セッションが無効化 → REDIS_PASSWORD を CACHES に注入

検証

  • pytest: 23 passed(ページネーション・画像リサイズ・キャッシュのテストを追加)
  • 実機(docker compose): 全 migration 適用成功、django コンテナ healthy、公開ページ全 200、/admin/ 404、Redis 上にキャッシュキー生成を確認
  • 既知の制約: 検証環境が rootless Docker のため nginx のポート 80 起動と SSL は未検証(構文チェックのみ)。マージ後、デプロイ先の .envDATABASE_URLPOSTGRES_* と整合する値に要修正(詳細はレビューコメント参照)

🤖 Generated with Claude Code

dendencat and others added 3 commits July 18, 2026 23:26
Waste removal:
- Delete orphaned duplicate Django project tree (app/)
- Delete broken/unused files: "import os.py", index.html, articles.js,
  duplicate test-data scripts, unbuildable Dockerfile, Dockerfile.nginx.static,
  nginx/Dockerfile, orphaned root nginx.conf, redundant compose static service
- Remove 6 unused dependencies (whitenoise, compressor, cors-headers, csp,
  sentry-sdk, django-extensions)
- Remove broken Django admin routing entirely (/admin/ now always 404)
- Merge never-loaded settings_production.py into settings.py

Performance:
- Add CONN_MAX_AGE=60 (was reconnecting to PostgreSQL every request)
- Fix N+1 queries with select_related/annotate (article list: 2 queries flat)
- Paginate article/category/tag list views (10 per page)
- Cache rendered article Markdown in Redis keyed by updated_at;
  disable Pygments guess_lang
- Cache sidebar categories/tags via context processor with signal invalidation
- Enable nginx gzip, turn proxy_buffering on
- Production gunicorn config (no reload, info log, fixed workers)
- Add missing migrations (initial + composite index on published/created_at)
  and use migrate --fake-initial in entrypoint
- Resize uploaded images to 1920px max with EXIF orientation fix
- Use cached template loader when DEBUG=False

Operational fixes found during E2E verification:
- Dockerfile.nginx copied a static/ dir deleted in 266aeab (build was broken)
- Exempt /health/ and /ready/ from SSL redirect so container healthcheck
  passes with DEBUG=False
- Inject REDIS_PASSWORD into CACHES so caching/sessions work against
  password-protected Redis

Implementation by Codex CLI under Claude orchestration.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…y scan on PRs

The build job relied on the repo-root Dockerfile, which was removed in this
PR (it had no FROM instruction and could never build). Build the real app
image from Dockerfile.django instead. Load the image into the local Docker
store on pull_request events (where push is disabled) and scan a single
sha-tagged reference so the Trivy step has a valid target. Bump checkout
and setup-python to current majors to clear Node 20 deprecation warnings.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Resolves the 22 CRITICAL/HIGH vulnerabilities Trivy found in the image:
Django 4.2.30 (CVE-2024-42005 SQL injection and later LTS fixes),
gunicorn 23 (CVE-2024-1135), Pillow >=12.1.1 (CVE-2026-25990), and
upgraded pip/setuptools/wheel in the image (wheel CVE-2026-24049,
jaraco.context CVE-2026-23949). black/flake8/isort are unused in this
repo (per the waste audit) and black 23.12 carried CVE-2026-32274, so
they are removed from the image entirely.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@dendencat

Copy link
Copy Markdown
Owner Author

@claude この PR の CI 結果(test / build / Trivy スキャン)を確認して、直近 3 コミットで何が修正されたかを日本語で簡潔に要約してください。

@claude

claude Bot commented Jul 18, 2026

Copy link
Copy Markdown

Claude finished @dendencat's task in 37s —— View job


CI 結果と直近3コミットの要約

  • CI 結果(test / build / Trivy スキャン)を確認
  • 直近3コミットの差分を確認
  • 日本語で要約してコメント更新

CI 結果(最新実行: run 29648820817)

ワークフロー 結果
CI/CD Pipeline test ✅ success
CI/CD Pipeline build(Trivy スキャンを含む) ✅ success
Claude Code Review ✅ success

2 ワークフローとも成功、失敗ステップなし。Trivy スキャンは build ジョブ内で実行されており問題なくパスしています。

直近3コミットの内容

  1. 8fa339f Remove dead code and fix performance bottlenecks from waste audit
    PR 本文の無駄削除・パフォーマンス改善一式(孤立 app/ ツリー削除、未使用依存6件削除、admin ルーティング撤去、N+1解消、ページネーション、Markdownキャッシュ、nginx/gunicorn設定最適化、複合インデックス追加、画像リサイズ等)をまとめて実施した本体コミット。E2E検証で見つかった SSL リダイレクト・Redis 認証・Dockerfile.nginx の壊れた COPY も同時に修正。

  2. 9ffc734 Fix CI build: point docker build at Dockerfile.django and repair Trivy scan on PRs
    直前のコミットでリポジトリ直下の Dockerfile(FROM 命令すらなくビルド不能)を削除したため CI の build ジョブが壊れていたのを修正。ビルド対象を実際に使われている Dockerfile.django に変更し、pull_request イベント(push 無効)ではイメージをローカル Docker ストアにロードして単一の sha タグ付き参照を Trivy スキャンできるようにした。あわせて actions/checkoutactions/setup-python を最新メジャーへ更新し Node 20 の非推奨警告を解消。

  3. 63e8e1a Fix Trivy findings: bump Django/gunicorn/Pillow, drop unused lint tools
    Trivy が検出した CRITICAL/HIGH 脆弱性22件を解消。Django 4.2.30(CVE-2024-42005 SQLインジェクション等)、gunicorn 23(CVE-2024-1135)、Pillow >=12.1.1(CVE-2026-25990)へバージョンアップ。加えて pip/setuptools/wheel も更新(wheel CVE-2026-24049、jaraco.context CVE-2026-23949)。black/flake8/isort はリポジトリ内で未使用(waste audit で判明)かつ black 23.12 に CVE-2026-32274 があったため、イメージから完全に削除。

まとめ: 8fa339f で waste audit 第2弾の本体変更を実施 → その結果 CI の build が壊れていたので 9ffc734 で修正 → 最後に 63e8e1a で Trivy が検出したセキュリティ脆弱性を解消、という流れで CI を green にしています。現時点で test / build(Trivy含む) はすべて success です。

@dendencat
dendencat merged commit a5bac3c into main Jul 18, 2026
3 checks passed
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