Skip to content

Commit

Permalink
updates cache
Browse files Browse the repository at this point in the history
  • Loading branch information
saxix committed Dec 9, 2024
1 parent b8f6d63 commit d73d9a7
Show file tree
Hide file tree
Showing 24 changed files with 230 additions and 99 deletions.
42 changes: 21 additions & 21 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -171,15 +171,16 @@ jobs:
redis1:
image: redis:7.4.0
ports:
- 26379:6379
- 5379:6379
db1:
image: postgres:14
env:
POSTGRES_HOST: db1
POSTGRES_DATABASE: country_workspace
POSTGRES_PASSWORD: postgres
POSTGRES_USERNAME: postgres
ports:
- 25432:5432
- 4432:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
Expand All @@ -188,9 +189,9 @@ jobs:
env:
DOCKER_DEFAULT_PLATFORM: linux/amd64
DOCKER_METADATA_ANNOTATIONS_LEVELS: manifest,index
DATABASE_URL: postgres://postgres:postgres@localhost:25432/country_workspace
CELERY_BROKER_URL: redis://localhost:26379/1
CACHE_URL: redis://localhost:26379/2
DATABASE_URL: postgres://postgres:postgres@localhost:4432/country_workspace
CELERY_BROKER_URL: redis://localhost:5379/1
CACHE_URL: redis://localhost:5379/2
DOCKER_BUILDKIT: 1
steps:
- name: Checkout code
Expand All @@ -206,27 +207,26 @@ jobs:
--target dist \
--cache-from "type=gha" \
--cache-to "type=gha,mode=max" \
--build-arg "VERSION=${{needs.test.outputs.commit}}" \
--build-arg "GIT_SHA=${{needs.test.outputs.commit}}" \
--build-arg "BUILD_DATE=${{needs.test.outputs.build_date}}" \
--build-arg "BRANCH=${{needs.test.outputs.branch}}" \
-t ${{needs.test.outputs.image}} \
-f docker/Dockerfile .
- name: Docker Integrity Check
run: |
docker run --rm \
--network host \
-e DATABASE_URL=${DATABASE_URL} \
-e CELERY_BROKER_URL=${CELERY_BROKER_URL} \
-e CACHE_URL=${CACHE_URL} \
-e SECRET_KEY=super-secret-key-just-for-testing \
-e HOPE_API_URL="https://dev-hope.unitst.org/api/rest/" \
-e HOPE_API_TOKEN=${{ secrets.HOPE_API_TOKEN }} \
-e AURORA_API_URL="https://uni-hope-ukr-sr-dev.unitst.org/api/" \
-e AURORA_API_TOKEN=${{ secrets.AURORA_API_TOKEN }} \
-t ${{needs.test.outputs.image}} \
django-admin upgrade
#
# - name: Docker Integrity Check
# run: |
# docker run --rm \
# --network host \
# -e DATABASE_URL=${DATABASE_URL} \
# -e CELERY_BROKER_URL=${CELERY_BROKER_URL} \
# -e CACHE_URL=${CACHE_URL} \
# -e SECRET_KEY=super-secret-key-just-for-testing \
# -e HOPE_API_URL="https://dev-hope.unitst.org/api/rest/" \
# -e HOPE_API_TOKEN=${{ secrets.HOPE_API_TOKEN }} \
# -e AURORA_API_URL="https://uni-hope-ukr-sr-dev.unitst.org/api/" \
# -e AURORA_API_TOKEN=${{ secrets.AURORA_API_TOKEN }} \
# -t ${{needs.test.outputs.image}} \
# django-admin upgrade

- name: Publish images
run: |
Expand Down
6 changes: 2 additions & 4 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ RUN pip install uv uwsgi
# ------- tests -------
FROM builder AS tests
ARG BUILD_DATE
ARG VERSION
ARG GIT_SHA

LABEL distro="tests"
LABEL org.opencontainers.image.created="$BUILD_DATE"
Expand Down Expand Up @@ -111,7 +111,6 @@ RUN --mount=type=cache,target=/root/.uv-cache \
# ------- dist -------
FROM base_os AS dist
ARG BUILD_DATE
ARG VERSION
ARG GIT_SHA
ARG BRANCH

Expand All @@ -120,7 +119,6 @@ ENV PATH=/app/.venv/bin:/usr/local/bin/:/usr/bin:/bin \
GIT_SHA=$GIT_SHA \
VERSION=$VERSION \
BRANCH=$BRANCH \
DJANGO_SETTINGS_MODULE=country_workspace.config.settings \
PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1 \
STATIC_URL="/static/" \
Expand Down Expand Up @@ -150,7 +148,7 @@ RUN --mount=type=cache,target=/root/.uv-cache \
--mount=type=bind,source=MANIFEST.in,target=/app/MANIFEST.in \
--mount=type=bind,source=README.md,target=/app/README.md \
--mount=type=bind,source=./src/country_workspace,target=/app/src/country_workspace \
uv --cache-dir=/root/.uv-cache pip install --no-deps .
uv --cache-dir=/root/.uv-cache pip install --link-mode=copy --no-deps .


EXPOSE 8000
Expand Down
2 changes: 1 addition & 1 deletion docker/bin/docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
export MEDIA_ROOT="${MEDIA_ROOT:-/var/run/app/media}"
export STATIC_ROOT="${STATIC_ROOT:-/var/run/app/static}"
export UWSGI_PROCESSES="${UWSGI_PROCESSES:-"4"}"
export DJANGO_SETTINGS_MODULE="${DJANGO_SETTINGS_MODULE:-country_workspace.config.settings}"
export DJANGO_SETTINGS_MODULE="country_workspace.config.settings"

case "$1" in
run)
Expand Down
4 changes: 4 additions & 0 deletions docs/src/license.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# License


--8<-- "LICENSE.md"
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ dependencies = [
"deprecation>=2.1.0",
"dictdiffer>=0.9.0",
"django-adminactions>=2.3.0",
"django-adminfilters==2.5.1",
"django-adminfilters>=2.5.2",
"django-cacheops>=7.1",
"django-celery-beat>=2.6.0",
"django-celery-boost>=0.5.0",
Expand Down
2 changes: 1 addition & 1 deletion src/country_workspace/admin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
from smart_admin.console import panel_migrations, panel_redis, panel_sentry, panel_sysinfo
from smart_admin.smart_auth.admin import ContentTypeAdmin, PermissionAdmin

from ..cache.smart_panel import panel_cache
from .batch import BatchAdmin # noqa
from .household import HouseholdAdmin # noqa
from .individual import IndividualAdmin # noqa
from .job import AsyncJobAdmin # noqa
from .locations import AreaAdmin, AreaTypeAdmin, CountryAdmin # noqa
from .office import OfficeAdmin # noqa
from .panels.cache import panel_cache
from .program import ProgramAdmin # noqa
from .role import UserRoleAdmin # noqa
from .sync import SyncLog # noqa
Expand Down
5 changes: 4 additions & 1 deletion src/country_workspace/admin/individual.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ class IndividualAdmin(BaseModelAdmin):
("batch", LinkedAutoCompleteFilter.factory(parent="batch__program")),
IsValidFilter,
)
autocomplete_fields = ("batch",)
autocomplete_fields = (
"batch",
"household",
)

@link(change_list=True, change_form=False)
def view_in_workspace(self, btn: "LinkButton") -> None:
Expand Down
2 changes: 1 addition & 1 deletion src/country_workspace/admin/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
@admin.register(AsyncJob)
class AsyncJobAdmin(CeleryTaskModelAdmin, BaseModelAdmin):
list_display = ("program", "type", "verbose_status", "owner")
autocomplete_fields = ("program", "owner")
autocomplete_fields = ("program", "owner", "batch", "content_type")
list_filter = (
("program__country_office", LinkedAutoCompleteFilter.factory(parent=None)),
("program", LinkedAutoCompleteFilter.factory(parent="program__country_office")),
Expand Down
1 change: 1 addition & 0 deletions src/country_workspace/admin/program.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class ProgramAdmin(BaseModelAdmin):
search_fields = ("name",)
list_filter = (("country_office", AutoCompleteFilter), "status", "active", "sector")
ordering = ("name",)
autocomplete_fields = ("country_office",)

@button()
def invalidate_cache(self, request: HttpRequest, pk: str) -> None:
Expand Down
16 changes: 14 additions & 2 deletions src/country_workspace/cache/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
class CacheManager:
def __init__(self, prefix="cache"):
self.prefix = prefix
self.active = True
self.cw_version = "-"
self.cache_timeout = 86400
self.cache_by_version = False
Expand Down Expand Up @@ -59,6 +58,8 @@ def retrieve(self, key):

def store(self, key: str, value: Any, timeout: int = 0, **kwargs):
cache_set.send(self.__class__, key=key)
if not self.active:
timeout = 1
self.cache.set(key, value, timeout=timeout or self.cache_timeout, **kwargs)

def _get_version_key(self, office: "Optional[Office]" = None, program: "Optional[Program]" = None):
Expand Down Expand Up @@ -92,12 +93,23 @@ def incr_cache_version(self, *, office: "Optional[Office]" = None, program: "Opt
except ValueError:
return self.cache.set(key, 2)

@property
def active(self) -> bool:
return not bool(self.cache.get(f"{self.prefix}:cache_disabled"))

@active.setter
def active(self, value: bool):
if not value:
self.cache.set(f"{self.prefix}:cache_disabled", True)
else:
self.cache.delete(f"{self.prefix}:cache_disabled")

def build_key(self, prefix, *parts):
tenant = "t"
version = "v"
program = "p"
ts = "ts"
if self.cache.get("cache_disabled"):
if not self.active:
ts = str(timezone.now().toordinal())

if state.tenant and state.program:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from django import forms
from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.utils.translation import gettext_lazy as _

from country_workspace.cache.manager import cache_manager


class CacheManagerForm(forms.Form):
pattern = forms.CharField()
pattern = forms.CharField(required=False, initial="*")


def panel_cache(self, request):
Expand All @@ -21,17 +22,26 @@ def _get_keys():
form = CacheManagerForm(request.POST)
if form.is_valid():
limit_to = form.cleaned_data["pattern"]
if "_delete" in request.POST:
if "_disable" in request.POST:
cache_manager.active = False
return HttpResponseRedirect(".")
elif "_enable" in request.POST:
cache_manager.active = True
return HttpResponseRedirect(".")
elif "_delete" in request.POST:
to_delete = list(_get_keys())
if to_delete:
client.delete(*to_delete)
return HttpResponseRedirect(".")

else:
form = CacheManagerForm()

context["title"] = "Cache Manager"
context["form"] = form
cache_data = _get_keys()
context["cache_data"] = cache_data
context["active"] = cache_manager.active

return render(request, "smart_admin/panels/cache.html", context)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{% extends "smart_admin/console.html" %}{% load i18n static %}
{% block left %}
<div class="console">
<div>
<form method="post">
{% csrf_token %}
<div class="module">
<table>
{{ form.as_table }}
</table>
</div>
<div class="submit-row module">
{% if active %}
<input type="submit" style="background-color: #229515" name="_disable" value="Cache is Enabled">
{% else %}
<input type="submit" style="background-color: #c12727" name="_enable" value="Cache is Disabled">
{% endif %}
<div style="float: right;">
<input type="submit" value="Get keys">&nbsp;
<input type="submit" style="background-color: #951515" name="_delete"
value="Delete selected">
</div>
</div>
</form>
</div>
<div class="changelist-form-container">
{% for e in cache_data %}
<div>{{ e }}</div>
{% endfor %}
</div>
</div>
{% endblock left %}
9 changes: 8 additions & 1 deletion src/country_workspace/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def setting(anchor: str) -> str:


def celery_doc(anchor: str) -> str:
return f"@see https://docs.celeryq.dev/en/stable/" f"userguide/configuration.html#{anchor}"
return f"@see https://docs.celeryq.dev/en/stable/userguide/configuration.html#{anchor}"


class Group(Enum):
Expand Down Expand Up @@ -55,6 +55,13 @@ class Group(Enum):
"AURORA_API_TOKEN": (str, "", "", False, "Aurora API token"),
"AURORA_API_URL": (str, "", "", False, "Aurora API url"),
"CACHE_URL": (str, "", "redis://localhost:6379/0", True, setting("cache-url")),
"SELECT2_CACHE": (
str,
"",
"redis://localhost:6379/9",
False,
"https://django-select2.readthedocs.io/en/latest/django_select2.html#module-django_select2.conf",
),
"CELERY_BROKER_URL": (
str,
"",
Expand Down
3 changes: 2 additions & 1 deletion src/country_workspace/config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@
USE_TZ = True

CACHE_URL = env("CACHE_URL")
SELECT2_CACHE = env("SELECT2_CACHE")
# REDIS_URL = urlparse(CACHE_URL).hostname
CACHES = {
"default": {
Expand All @@ -140,7 +141,7 @@
},
"select2": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": CACHE_URL,
"LOCATION": SELECT2_CACHE or CACHE_URL,
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
},
Expand Down
2 changes: 2 additions & 0 deletions src/country_workspace/web/templates/admin/base_site.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

{% block title %}{{ title }} | {{ site_title|default:_('Django site admin') }}{% endblock %}

{% block extrastyle %}{{ block.super }}{{ media }}{% endblock %}

{% block userlinks %}{{ block.super }}
{# {% if sysinfo %} / <a href="{{ sysinfo }}">System Info</a> {% endif %} #}
{% if profile_link %} / <a href="{{ profile_link }}">Profile</a> {% endif %}
Expand Down
20 changes: 0 additions & 20 deletions src/country_workspace/web/templates/smart_admin/panels/cache.html

This file was deleted.

Loading

0 comments on commit d73d9a7

Please sign in to comment.