mirror of
https://github.com/seanmorley15/AdventureLog.git
synced 2025-07-24 15:29:36 +02:00
Auto schedule Django APScheduler
This commit is contained in:
parent
f5e721cd82
commit
590fa9edc0
5 changed files with 82 additions and 18 deletions
|
@ -13,7 +13,7 @@ WORKDIR /code
|
||||||
|
|
||||||
# Install system dependencies
|
# Install system dependencies
|
||||||
RUN apt-get update \
|
RUN apt-get update \
|
||||||
&& apt-get install -y git postgresql-client cron \
|
&& apt-get install -y git postgresql-client \
|
||||||
&& apt-get clean
|
&& apt-get clean
|
||||||
|
|
||||||
# Install Python dependencies
|
# Install Python dependencies
|
||||||
|
@ -27,18 +27,6 @@ COPY ./server /code/
|
||||||
# Collect static files
|
# Collect static files
|
||||||
RUN python3 manage.py collectstatic --noinput --verbosity 2
|
RUN python3 manage.py collectstatic --noinput --verbosity 2
|
||||||
|
|
||||||
# Setup cron to run python3 manage.py worldtravel-seed every day at 00:00
|
|
||||||
RUN echo "0 0 * * * python3 /code/manage.py worldtravel-seed --force >> /var/log/cron.log 2>&1" > /etc/cron.d/worldtravel-seed
|
|
||||||
|
|
||||||
# Give execution rights on the cron job
|
|
||||||
RUN chmod 0644 /etc/cron.d/worldtravel-seed
|
|
||||||
|
|
||||||
# Apply cron job
|
|
||||||
RUN crontab /etc/cron.d/worldtravel-seed
|
|
||||||
|
|
||||||
# Ensure cron is started in the foreground when the container starts
|
|
||||||
CMD ["cron", "-f"]
|
|
||||||
|
|
||||||
# Set the entrypoint script
|
# Set the entrypoint script
|
||||||
COPY ./entrypoint.sh /code/entrypoint.sh
|
COPY ./entrypoint.sh /code/entrypoint.sh
|
||||||
RUN chmod +x /code/entrypoint.sh
|
RUN chmod +x /code/entrypoint.sh
|
||||||
|
|
|
@ -1,6 +1,11 @@
|
||||||
from django.apps import AppConfig
|
from django.apps import AppConfig
|
||||||
|
from django.conf import settings
|
||||||
|
|
||||||
class AdventuresConfig(AppConfig):
|
class AdventuresConfig(AppConfig):
|
||||||
default_auto_field = "django.db.models.BigAutoField"
|
default_auto_field = 'django.db.models.BigAutoField'
|
||||||
name = "adventures"
|
name = 'adventures'
|
||||||
|
|
||||||
|
def ready(self):
|
||||||
|
if settings.SCHEDULER_AUTOSTART:
|
||||||
|
from .scheduler import start_scheduler
|
||||||
|
start_scheduler()
|
36
backend/server/adventures/scheduler.py
Normal file
36
backend/server/adventures/scheduler.py
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
# scheduler.py
|
||||||
|
|
||||||
|
import logging
|
||||||
|
from apscheduler.schedulers.background import BackgroundScheduler
|
||||||
|
from django_apscheduler.jobstores import DjangoJobStore
|
||||||
|
from django_apscheduler import util
|
||||||
|
from django.conf import settings
|
||||||
|
from django.core.management import call_command
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
def run_worldtravel_seed():
|
||||||
|
logger.info("Starting worldtravel-seed job")
|
||||||
|
try:
|
||||||
|
call_command('worldtravel-seed', '--force')
|
||||||
|
logger.info("worldtravel-seed job completed successfully")
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error in worldtravel-seed job: {str(e)}")
|
||||||
|
|
||||||
|
@util.close_old_connections
|
||||||
|
def start_scheduler():
|
||||||
|
scheduler = BackgroundScheduler(timezone=settings.TIME_ZONE)
|
||||||
|
scheduler.add_jobstore(DjangoJobStore(), "default")
|
||||||
|
|
||||||
|
scheduler.add_job(
|
||||||
|
run_worldtravel_seed,
|
||||||
|
trigger="interval",
|
||||||
|
hours=24,
|
||||||
|
id="worldtravel_seed",
|
||||||
|
max_instances=1,
|
||||||
|
replace_existing=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
logger.info("Starting scheduler...")
|
||||||
|
scheduler.start()
|
||||||
|
return scheduler
|
|
@ -58,6 +58,7 @@ INSTALLED_APPS = (
|
||||||
'adventures',
|
'adventures',
|
||||||
'worldtravel',
|
'worldtravel',
|
||||||
'users',
|
'users',
|
||||||
|
'django_apscheduler',
|
||||||
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -71,12 +72,18 @@ MIDDLEWARE = (
|
||||||
'django.contrib.messages.middleware.MessageMiddleware',
|
'django.contrib.messages.middleware.MessageMiddleware',
|
||||||
'django.middleware.clickjacking.XFrameOptionsMiddleware',
|
'django.middleware.clickjacking.XFrameOptionsMiddleware',
|
||||||
'allauth.account.middleware.AccountMiddleware',
|
'allauth.account.middleware.AccountMiddleware',
|
||||||
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# disable verifications for new users
|
# disable verifications for new users
|
||||||
ACCOUNT_EMAIL_VERIFICATION = 'none'
|
ACCOUNT_EMAIL_VERIFICATION = 'none'
|
||||||
|
|
||||||
|
CACHES = {
|
||||||
|
'default': {
|
||||||
|
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
# For backwards compatibility for Django 1.8
|
# For backwards compatibility for Django 1.8
|
||||||
MIDDLEWARE_CLASSES = MIDDLEWARE
|
MIDDLEWARE_CLASSES = MIDDLEWARE
|
||||||
|
|
||||||
|
@ -230,3 +237,30 @@ from os import getenv
|
||||||
|
|
||||||
CSRF_TRUSTED_ORIGINS = [origin.strip() for origin in getenv('CSRF_TRUSTED_ORIGINS', 'http://localhost').split(',') if origin.strip()]
|
CSRF_TRUSTED_ORIGINS = [origin.strip() for origin in getenv('CSRF_TRUSTED_ORIGINS', 'http://localhost').split(',') if origin.strip()]
|
||||||
DEFAULT_AUTO_FIELD = 'django.db.models.AutoField'
|
DEFAULT_AUTO_FIELD = 'django.db.models.AutoField'
|
||||||
|
|
||||||
|
LOGGING = {
|
||||||
|
'version': 1,
|
||||||
|
'disable_existing_loggers': False,
|
||||||
|
'handlers': {
|
||||||
|
'console': {
|
||||||
|
'class': 'logging.StreamHandler',
|
||||||
|
},
|
||||||
|
'file': {
|
||||||
|
'class': 'logging.FileHandler',
|
||||||
|
'filename': 'scheduler.log',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
'root': {
|
||||||
|
'handlers': ['console', 'file'],
|
||||||
|
'level': 'INFO',
|
||||||
|
},
|
||||||
|
'loggers': {
|
||||||
|
'django': {
|
||||||
|
'handlers': ['console', 'file'],
|
||||||
|
'level': 'INFO',
|
||||||
|
'propagate': False,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
SCHEDULER_AUTOSTART = True
|
|
@ -10,4 +10,5 @@ python-dotenv
|
||||||
psycopg2-binary
|
psycopg2-binary
|
||||||
Pillow
|
Pillow
|
||||||
whitenoise
|
whitenoise
|
||||||
django-resized
|
django-resized
|
||||||
|
django-apscheduler
|
Loading…
Add table
Add a link
Reference in a new issue