Skip to content

fix: close ghost rooms/users on startup and repair cron env#176

Merged
Uno-Takashi merged 1 commit into
mainfrom
fix/close-active-sessions-on-startup
Jun 24, 2026
Merged

fix: close ghost rooms/users on startup and repair cron env#176
Uno-Takashi merged 1 commit into
mainfrom
fix/close-active-sessions-on-startup

Conversation

@Uno-Takashi

Copy link
Copy Markdown
Member

概要

Django コンテナを作り直すと、その時接続中だった AnimeUser / AnimeRoomalive のまま残り、統計 API(/api/.../alive)に幽霊セッションとして計上され続ける問題を修正する。

合わせて、コンテナ内の cron で動いているはずの retention cleanup ジョブが起動以降 1 度も成功していなかった問題も修正する。

変更

  • 起動時クローズ: streamer/management/commands/close_active_sessions.py を追加し、AnimeUser / AnimeRoomalive を一括で論理削除する(冪等)。entrypoint.sh が runserver/gunicorn を起動する直前に呼ぶ。
  • cron 修復:
    • cron の最小 PATH には /opt/venv/bin が無く python: not found で毎分失敗していたため、venv の絶対パス /opt/venv/bin/python を使うようにした。
    • cron は親プロセスの env を継承しないため settings.pyos.environ["DEBUG"]KeyError になっていた。コンテナ起動時の env を /etc/cron.d/d-party-env.sh にスナップショットし、ジョブから source して解決。

動作確認

ローカル (docker compose) で再起動して確認:

django  | closed 90 alive user(s) and 101 alive room(s)
...
/var/log/cron.log:
  ran animeuser_auto_hard_delete
  ran animereaction_auto_hard_delete

- Add 'close_active_sessions' management command that logically deletes
  any AnimeRoom / AnimeUser still marked alive. WebSocket session state
  lives only in the Django process, so a container restart used to leave
  ghost sessions counted by the public /api/.../alive endpoints forever.
- entrypoint.sh now runs the command before starting runserver/gunicorn.
- Fix the cron job that drives 'manage.py cleanup':
  - Use the venv python absolute path (cron's PATH lacked /opt/venv/bin
    so the job was failing every minute with 'python: not found').
  - Snapshot container env to /etc/cron.d/d-party-env.sh and source it
    from the job, so settings.py can read DEBUG and other compose-only
    env vars that cron does not inherit.
@Uno-Takashi Uno-Takashi merged commit 4bccdc3 into main Jun 24, 2026
16 of 18 checks passed
@Uno-Takashi Uno-Takashi deleted the fix/close-active-sessions-on-startup branch June 24, 2026 10:38
冪等な操作なので、起動毎に無条件で実行してよい。
"""

from django.core.management.base import BaseCommand

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [mypy] reported by reviewdog 🐶
Cannot find implementation or library stub for module named "django.core.management.base" [import-not-found]

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