Skip to content

Commit 40eef68

Browse files
authored
Merge pull request #39 from OS2borgerPC/36-cronjobs-er-svære-at-deploye
Add HTTP endpoints for cron jobs
2 parents b147720 + b8add3e commit 40eef68

File tree

3 files changed

+50
-5
lines changed

3 files changed

+50
-5
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
"""
2+
WSGI config for OS2borgerPC admin project.
3+
4+
This module contains a WSGI application to run cron jobs.
5+
"""
6+
7+
import subprocess
8+
import uuid
9+
import logging
10+
import sys
11+
12+
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
13+
14+
def application(environ, start_response):
15+
path = environ.get('PATH_INFO', '/')
16+
17+
job_routes = {
18+
'/jobs/check_notifications': 'check_notifications',
19+
'/jobs/clean_up_database': 'clean_up_database',
20+
}
21+
command = job_routes.get(path, None)
22+
23+
if command:
24+
error = run(command)
25+
if error:
26+
trace_id = str(uuid.uuid4())
27+
logging.error(f"Error occured (trace ID '{trace_id}')\n{error}")
28+
response, status = (f"An internal server error occured. The error has been logged with trace ID '{trace_id}'.", "500 Internal Server Error")
29+
else:
30+
response, status = ("", "200 OK")
31+
else:
32+
response, status = "Not found", "404 Not Found"
33+
34+
start_response(status, [("Content-Type", "text/plain")])
35+
return iter([response.encode("utf-8")])
36+
37+
38+
def run(command):
39+
try:
40+
subprocess.run(['python', 'manage.py', command], check=True, text=True, capture_output=True)
41+
return None
42+
except subprocess.CalledProcessError as e:
43+
return "Internal server error: " + e.stderr.strip() if e.stderr else 'An error occurred without stderr output.'

compose.yaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ services:
4949
--reload-extra-file /code/admin_site/locale/da/LC_MESSAGES/djangojs.mo
5050
--reload-extra-file /code/admin_site/locale/sv/LC_MESSAGES/django.mo
5151
--reload-extra-file /code/admin_site/locale/sv/LC_MESSAGES/djangojs.mo
52-
--timeout 0 --config /code/docker/gunicorn-settings.py os2borgerpc_admin.wsgi"
52+
--timeout 0 --config /code/docker/gunicorn-settings.py os2borgerpc_admin.wsgi &
53+
gunicorn --bind 0.0.0.0:8080 os2borgerpc_admin.jobsWsgi"
5354
volumes:
5455
- .:/code/
5556
- ./dev-environment/dev-settings.ini:/user-settings.ini
@@ -59,6 +60,7 @@ services:
5960
- db
6061
ports:
6162
- 9999:9999
63+
- 8080:8080
6264
stdin_open: true
6365
tty: true
6466
container_name: bpc_admin_site_django
@@ -86,4 +88,4 @@ services:
8688
- postgres-data:/var/lib/postgresql/data
8789
container_name: bpc_admin_site_db
8890
volumes:
89-
postgres-data:
91+
postgres-data:

docker/Dockerfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ RUN set -ex \
109109
# Run the server as the bpc user on port 9999
110110
USER bpc:bpc
111111
EXPOSE 9999
112+
EXPOSE 8080
112113
ENTRYPOINT ["/code/docker/docker-entrypoint.sh"]
113-
CMD ["gunicorn", \
114-
"--config", "/code/docker/gunicorn-settings.py", \
115-
"os2borgerpc_admin.wsgi"]
114+
CMD bash -c "gunicorn --bind 0.0.0.0:8080 os2borgerpc_admin.jobsWsgi & \
115+
gunicorn --config /code/docker/gunicorn-settings.py os2borgerpc_admin.wsgi"

0 commit comments

Comments
 (0)