Skip to content

Commit d792214

Browse files
authored
Merge pull request #148 from SmoFlaDru/dev-benno
Add celery + Redis for cronjobs in docker. Docker improvements. Fix remaining viz queries.
2 parents b668e20 + 5baf56c commit d792214

14 files changed

+352
-145
lines changed

.dockerignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ venv
22
.venv
33
.git
44
.idea
5-
node_modules
5+
node_modules
6+
docker-compose.override.yml

Dockerfile

+7-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ RUN npm run package
77

88
FROM python:3.12-slim-bookworm
99

10-
WORKDIR /app
10+
RUN groupadd -g 1234 spybot && useradd -m -u 1234 -g spybot spybot
11+
12+
WORKDIR /home/spybot
1113

1214
ENV PYTHONDONTWRITEBYTECODE=1
1315
ENV PYTHONUNBUFFERED=1
@@ -17,8 +19,10 @@ EXPOSE 8000
1719
RUN pip install uv
1820
COPY pyproject.toml pyproject.toml
1921
RUN uv sync
20-
COPY . .
22+
COPY --chown=spybot:spybot . .
2123

22-
COPY --from=frontend-build frontend/output frontend/output
24+
COPY --chown=spybot:spybot --from=frontend-build frontend/output frontend/output
2325

26+
USER spybot
27+
RUN mkdir spybot_static
2428
CMD ["sh", "run.sh"]

Spybot2/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from .celery import app as celery_app
2+
3+
__all__ = ("celery_app",)

Spybot2/celery.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import os
2+
3+
from celery import Celery
4+
5+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Spybot2.settings")
6+
7+
app = Celery("Spybot2")
8+
app.config_from_object("django.conf:settings", namespace="CELERY")
9+
app.autodiscover_tasks()

Spybot2/settings.py

+22-13
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import environ
1515
import os
1616
import sentry_sdk
17+
from celery.schedules import crontab
1718

1819
# Build paths inside the project like this: BASE_DIR / 'subdir'.
1920
BASE_DIR = Path(__file__).resolve().parent.parent
@@ -50,7 +51,7 @@
5051
# SECURITY WARNING: don't run with debug turned on in production!
5152
DEBUG = env.bool('DEBUG', False)
5253

53-
ALLOWED_HOSTS = [SERVER_IP, TS_IP, 'localhost', '127.0.0.1', 'spybot.localhost.direct', '192.168.59.100']
54+
ALLOWED_HOSTS = [SERVER_IP, TS_IP, 'localhost', '127.0.0.1', 'spybot.localhost.direct', '192.168.59.100', '192.168.59.100:20001']
5455

5556
CSRF_TRUSTED_ORIGINS = [f"https://{SERVER_IP}"]
5657

@@ -64,7 +65,6 @@
6465

6566
INSTALLED_APPS = [
6667
'spybot',
67-
'django_crontab',
6868
'django.contrib.admin',
6969
'django.contrib.auth',
7070
'django.contrib.contenttypes',
@@ -117,10 +117,6 @@
117117
'CONN_MAX_AGE': 3600,
118118
'CONN_HEALTH_CHECKS': True,
119119
},
120-
#'default': {
121-
# 'ENGINE': 'django.db.backends.sqlite3',
122-
# 'NAME': BASE_DIR / 'db.sqlite3',
123-
#}
124120
}
125121

126122

@@ -172,7 +168,7 @@
172168
# https://docs.djangoproject.com/en/4.1/howto/static-files/
173169

174170
STATIC_URL = 'static/'
175-
STATIC_ROOT = '../spybot_static/'
171+
STATIC_ROOT = 'spybot_static/'
176172

177173
STATICFILES_DIRS = [
178174
BASE_DIR / "frontend/output/",
@@ -183,13 +179,26 @@
183179

184180
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
185181

186-
# django-crontab config
182+
# Celery
183+
CELERY_BROKER_URL = "redis://redis:6379"
184+
CELERY_RESULT_BACKEND = "redis://redis:6379"
185+
186+
# Celery beat
187+
CELERY_BEAT_SCHEDULE = {
188+
'record_hourly_activity': {
189+
'task': 'spybot.tasks.record_hourly_activity',
190+
'schedule': crontab(minute=59), # every hour
191+
'args': (),
192+
'options': {},
193+
},
194+
'end_of_week_awards': {
195+
'task': 'spybot.tasks.end_of_week_awards',
196+
'schedule': crontab(minute=59, hour=23, day_of_week='sunday'), # every Sunday evening
197+
'args': (),
198+
'options': {},
199+
},
200+
}
187201

188-
CRONJOBS = [
189-
('59 23 * * SUN', 'spybot.recorder.cron.cron.end_of_week_awards'),
190-
('59 * * * *', 'spybot.recorder.cron.cron.record_hourly_activity')
191-
]
192-
CRONTAB_COMMAND_PREFIX = env.str('CRONTAB_COMMAND_PREFIX', '')
193202

194203

195204
# Testing

docker-compose-local.yml

-43
This file was deleted.

docker-compose.override.yml

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
services:
2+
spybot:
3+
build:
4+
dockerfile: Dockerfile
5+
caddy:
6+
ports:
7+
- "80:80"
8+
# - "443:443"
9+
# - "443:443/udp"
10+
volumes:
11+
- $PWD/infrastructure/Caddyfile:/etc/caddy/Caddyfile
12+
- caddy_data:/data
13+
- caddy_config:/config
14+
- static_files:/spybot_static
15+
db:
16+
ports:
17+
- "5432:5432"

infrastructure/docker-compose.yml

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ services:
33
image: 'ghcr.io/smofladru/spybot2:sha-${COMMIT_SHA}'
44
restart: unless-stopped
55
volumes:
6-
- static_files:/spybot_static
6+
- static_files:/home/spybot/spybot_static
77
- $PWD/.env:/app/.env
88
expose:
99
- 8000
@@ -28,6 +28,8 @@ services:
2828
- spybot_pg_data:/var/lib/postgresql/data
2929
secrets:
3030
- db_password
31+
redis:
32+
image: redis:7.4.2-bookworm
3133
volumes:
3234
caddy_data:
3335
caddy_config:

pyproject.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ dependencies = [
1313
"Django ~=5.1",
1414
"ts3 ~=2.0.0b3",
1515
"django-environ ~=0.11.2",
16-
"django-crontab ~=0.7.1",
1716
"num2words ~=0.5.12",
1817
"requests ~=2.31",
1918
"fido2 ~=1.1",
@@ -22,6 +21,8 @@ dependencies = [
2221
"sentry-sdk>=2.13.0",
2322
"gunicorn>=23.0.0",
2423
"psycopg2-binary>=2.9.10",
24+
"celery>=5.4.0",
25+
"redis>=5.2.1",
2526
]
2627

2728

run.sh

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
#!/bin/bash
22

3-
ls -a
4-
# activate venv
5-
#.venv/bin/activate
6-
73
# copy static files to directory for http server
84
.venv/bin/python manage.py collectstatic --noinput
95

106
# run DB migrations if necessary
117
.venv/bin/python manage.py migrate
128

9+
# run celery worker process
10+
.venv/bin/python -m celery -A Spybot2 worker -l info &
11+
CELERY_WORKER_JOB=$!
12+
trap 'kill CELERY_WORKER_JOB' EXIT HUP TERM INT
13+
14+
# run celery beat process
15+
.venv/bin/python -m celery -A Spybot2 beat -l info &
16+
CELERY_BEAT_JOB=$!
17+
trap 'kill CELERY_BEAT_JOB' EXIT HUP TERM INT
18+
1319
# start recorder in background and terminate on script exit
1420
.venv/bin/python manage.py recorder &
1521
RECORDER_JOB=$!

scripts/copy_to_local_db.sh

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
temp_file=$(mktemp)
2+
echo $temp_file
3+
ssh elcheapo -C "docker exec spybot-db-1 pg_dump -U postgres spybot" > $temp_file
4+
docker cp $temp_file spybot2-db-1:/import_dump.sql
5+
docker exec --user postgres spybot2-db-1 dropdb -f --if-exists spybot
6+
docker exec --user postgres spybot2-db-1 createdb spybot
7+
docker exec spybot2-db-1 psql -U postgres spybot -f /import_dump.sql
8+
rm "${temp_file}"

spybot/tasks.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from celery import shared_task
2+
3+
4+
@shared_task()
5+
def end_of_week_awards():
6+
print("shared week awards job")
7+
8+
@shared_task()
9+
def record_hourly_activity():
10+
print("record hourly activity job")

0 commit comments

Comments
 (0)