From dfe1386e36b2d58662abc1b28ca0f49bb1660688 Mon Sep 17 00:00:00 2001 From: Matt Layman Date: Sun, 6 Oct 2024 00:11:32 -0400 Subject: [PATCH] Add Huey and task to clear db sessions. --- .gitignore | 2 +- Procfile.dev | 1 + config/deploy.yml | 2 +- homeschool/core/tasks.py | 12 ++++++++++++ homeschool/core/tests/test_tasks.py | 8 ++++++++ project/settings.py | 8 ++++++++ project/testing_settings.py | 6 ++++++ pyproject.toml | 1 + uv.lock | 8 ++++++++ 9 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 homeschool/core/tasks.py create mode 100644 homeschool/core/tests/test_tasks.py diff --git a/.gitignore b/.gitignore index 3e07dfca..8cb31bb2 100644 --- a/.gitignore +++ b/.gitignore @@ -54,7 +54,7 @@ coverage.xml # Django stuff: *.log local_settings.py -db.sqlite3* +*.sqlite3* # Flask stuff: instance/ diff --git a/Procfile.dev b/Procfile.dev index 992f4234..827a03f0 100644 --- a/Procfile.dev +++ b/Procfile.dev @@ -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 diff --git a/config/deploy.yml b/config/deploy.yml index 24a6fffe..ab83f585 100644 --- a/config/deploy.yml +++ b/config/deploy.yml @@ -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 diff --git a/homeschool/core/tasks.py b/homeschool/core/tasks.py new file mode 100644 index 00000000..bf9c9122 --- /dev/null +++ b/homeschool/core/tasks.py @@ -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() diff --git a/homeschool/core/tests/test_tasks.py b/homeschool/core/tests/test_tasks.py new file mode 100644 index 00000000..fbd96ec2 --- /dev/null +++ b/homeschool/core/tests/test_tasks.py @@ -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() diff --git a/project/settings.py b/project/settings.py index 4ea4d5cc..97ab0bc3 100644 --- a/project/settings.py +++ b/project/settings.py @@ -39,6 +39,7 @@ "djstripe", "hijack", "hijack.contrib.admin", + "huey.contrib.djhuey", "ordered_model", "simple_history", "tz_detect", @@ -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") diff --git a/project/testing_settings.py b/project/testing_settings.py index ec2f4d09..b75586ba 100644 --- a/project/testing_settings.py +++ b/project/testing_settings.py @@ -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" diff --git a/pyproject.toml b/pyproject.toml index caa1c675..0cc0c229 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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", diff --git a/uv.lock b/uv.lock index 59ac98bf..b61d2bb8 100644 --- a/uv.lock +++ b/uv.lock @@ -628,6 +628,7 @@ dependencies = [ { name = "django-waffle" }, { name = "environs" }, { name = "gunicorn" }, + { name = "huey" }, { name = "python-dateutil" }, { name = "requests" }, { name = "sentry-sdk" }, @@ -686,6 +687,7 @@ requires-dist = [ { name = "django-waffle", specifier = "==4.1.0" }, { name = "environs", specifier = ">=11.0.0" }, { name = "gunicorn", specifier = "==23.0.0" }, + { name = "huey", specifier = ">=2.5.2" }, { name = "python-dateutil", specifier = "==2.9.0.post0" }, { name = "requests", specifier = "==2.32.3" }, { name = "sentry-sdk", specifier = "==2.13.0" }, @@ -746,6 +748,12 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/6c/dd/a834df6482147d48e225a49515aabc28974ad5a4ca3215c18a882565b028/html5lib-1.1-py2.py3-none-any.whl", hash = "sha256:0d78f8fde1c230e99fe37986a60526d7049ed4bf8a9fadbad5f00e22e58e041d", size = 112173 }, ] +[[package]] +name = "huey" +version = "2.5.2" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/9c/fe/2e063984cdd512aa71e9c9c2a9200b58a830c532d25ca2c6cbc8e44bf7b7/huey-2.5.2.tar.gz", hash = "sha256:df33db474c05414ed40ee2110e9df692369871734da22d74ffb035a4bd74047f", size = 889357 } + [[package]] name = "identify" version = "2.6.0"