Skip to content

Commit

Permalink
feat: celery框架导入
Browse files Browse the repository at this point in the history
  • Loading branch information
LittleRewriter committed Aug 22, 2024
1 parent 529f676 commit 0eabae9
Show file tree
Hide file tree
Showing 10 changed files with 30,851 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ share/python-wheels/
.installed.cfg
*.egg
MANIFEST
.vscode/

# PyInstaller
# Usually these files are written by a python script from a template
Expand Down
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"python.analysis.typeCheckingMode": "basic",
"mypy.dmypyExecutable": "${workspaceFolder}/.venv/Scripts/dmypy.exe",
"mypy.dmypyExecutable": "${workspaceFolder}/.venv/Scripts/dmypy.exe"
}
31 changes: 31 additions & 0 deletions contest/contest/celery.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from __future__ import absolute_import #, unicode_literals
import os
from celery import Celery
from celery.schedules import crontab
from django.utils import timezone

# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'contest.settings')

app = Celery('contest')

# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
# should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings', namespace='CELERY')

# 解决时区问题,定时任务启动就循环输出
# app.now = timezone.now()

# Load task modules from all registered Django app configs.
app.autodiscover_tasks(["contest"], "tasks")

# import contest.contest.tasks

app.conf.beat_schedule = {
'clean-specific-time' : {
'task': 'contest.tasks.auto_save_redis_to_database',
'schedule': crontab()
}
}
11 changes: 11 additions & 0 deletions contest/contest/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ def _debug_only(*args) -> tuple:
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
'django_celery_beat',
"django_cas_ng",
"tailwind", # 因为含模板 tag,即使无需构建前端也必要
"theme", # 提供 static,开发时由 Django 负责,部署时需 collectstatic,都需要
Expand Down Expand Up @@ -244,3 +245,13 @@ def _debug_only(*args) -> tuple:
datetime(2023, 9, 1, tzinfo=ZoneInfo(TIME_ZONE)),
datetime(2023, 9, 3, tzinfo=ZoneInfo(TIME_ZONE)),
)

if DEBUG:
CELERY_BROKER_URL = 'redis://127.0.0.1:6379/0'
else:
CELERY_BROKER_URL = 'redis://localhost:6379/0' # Modify in Release

CELERY_TIMEZONE = TIME_ZONE
# DJANGO_CELERY_BEAT_TZ_AWARE = False
CELERY_BEAT_SCHEDULER = 'django_celery_beat.schedulers:DatabaseScheduler'
# CELERY_ENABLE_UTC = False
10 changes: 10 additions & 0 deletions contest/contest/tasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from celery import shared_task
from datetime import datetime
import logging
# Get an instance of a logger
logger = logging.getLogger('django')


@shared_task
def auto_save_redis_to_database():
logger.info("HITTTT")
Loading

0 comments on commit 0eabae9

Please sign in to comment.