Skip to content

Commit

Permalink
Add Huey and task to clear db sessions.
Browse files Browse the repository at this point in the history
  • Loading branch information
mblayman committed Oct 6, 2024
1 parent c2c1168 commit dfe1386
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ coverage.xml
# Django stuff:
*.log
local_settings.py
db.sqlite3*
*.sqlite3*

# Flask stuff:
instance/
Expand Down
1 change: 1 addition & 0 deletions Procfile.dev
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
web: gunicorn project.wsgi --workers 2 --log-file - --bind 0.0.0.0:8000
worker: ./manage.py run_huey
frontend: npm run watch
2 changes: 1 addition & 1 deletion config/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ servers:
- 192.168.0.1
worker:
hosts:
- 45.55.106.163
- 192.168.0.1
cmd:
python /app/manage.py run_huey

Expand Down
12 changes: 12 additions & 0 deletions homeschool/core/tasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from importlib import import_module

from django.conf import settings
from huey import crontab
from huey.contrib.djhuey import db_periodic_task


@db_periodic_task(crontab(minute="0", hour="0"))
def clear_db_sessions():
print("Clear expired sessions")
engine = import_module(settings.SESSION_ENGINE)
engine.SessionStore.clear_expired()
8 changes: 8 additions & 0 deletions homeschool/core/tests/test_tasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from homeschool.core.tasks import clear_db_sessions
from homeschool.test import TestCase


class TestClearDBSessions(TestCase):
def test_run(self):
"""The task can run."""
clear_db_sessions()
8 changes: 8 additions & 0 deletions project/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"djstripe",
"hijack",
"hijack.contrib.admin",
"huey.contrib.djhuey",
"ordered_model",
"simple_history",
"tz_detect",
Expand Down Expand Up @@ -288,6 +289,13 @@
if DJSTRIPE_WEBHOOK_VALIDATION is None:
SILENCED_SYSTEM_CHECKS.append("djstripe.W004")

# Huey
HUEY = {
"huey_class": "huey.SqliteHuey",
"filename": env.path("DB_DIR", BASE_DIR) / "huey.sqlite3",
"immediate": False,
}

# Sentry
SENTRY_ENABLED = env.bool("SENTRY_ENABLED", True)
SENTRY_DSN = env.str("SENTRY_DSN")
Expand Down
6 changes: 6 additions & 0 deletions project/testing_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
},
}

HUEY = {
"huey_class": "huey.SqliteHuey",
"filename": ":memory:",
"immediate": True,
}

# Make sure that tests are never sending real emails.
EMAIL_BACKEND = "django.core.mail.backends.locmem.EmailBackend"

Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ dependencies = [
"django==5.1",
"environs>=11.0.0",
"gunicorn==23.0.0",
"huey>=2.5.2",
"python-dateutil==2.9.0.post0",
"requests==2.32.3",
"sentry-sdk==2.13.0",
Expand Down
8 changes: 8 additions & 0 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit dfe1386

Please sign in to comment.