-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
175 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
# pycharm IDE | ||
.idea/ | ||
|
||
# celerybeat.pid | ||
celerybeat-schedule | ||
*.pid | ||
# vscode IDE | ||
.vscode/ | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.contrib import admin | ||
|
||
# Register your models here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class ScheduleConfig(AppConfig): | ||
name = 'schedule' |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.db import models | ||
|
||
# Create your models here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import logging | ||
from settings import celery_app | ||
|
||
log = logging.getLogger('scripts') | ||
|
||
|
||
@celery_app.task() | ||
def add(a, b): | ||
c = a + b | ||
print('{}'.format(c)) | ||
log.info('{}'.format(c)) | ||
return c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.test import TestCase | ||
|
||
# Create your tests here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from django.urls import path, include | ||
from rest_framework.routers import DefaultRouter | ||
from apps.schedule.views import * | ||
|
||
router = DefaultRouter() | ||
router.register('task', CreateScheduleTask, basename='task') | ||
|
||
|
||
urlpatterns = [ | ||
path('', include(router.urls)), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from apps.utils.view.viewsets import ModelViewSet | ||
from django_celery_beat.models import CrontabSchedule, PeriodicTask | ||
from rest_framework import serializers | ||
|
||
|
||
class CrontabScheduleSerializer(serializers.ModelSerializer): | ||
|
||
def create(self, validated_data): | ||
schedule, _ = CrontabSchedule.objects.get_or_create(**validated_data) | ||
return schedule | ||
|
||
class Meta: | ||
model = CrontabSchedule | ||
exclude = ['timezone', ] | ||
|
||
|
||
class CreateScheduleTask(ModelViewSet): | ||
permission_classes = () | ||
authentication_classes = () | ||
serializer_class = CrontabScheduleSerializer | ||
queryset = CrontabSchedule.objects.all() | ||
|
||
def perform_create(self, serializer): | ||
""" | ||
生成一个新的定时任务就是新建一条PeriodicTask记录, | ||
修改settings timezone 重新执行 | ||
>>> from django_celery_beat.models import PeriodicTasks | ||
>>> PeriodicTasks.changed() | ||
参考文档 https://django-celery-beat.readthedocs.io/en/latest/ | ||
:param serializer: | ||
:return: | ||
""" | ||
schedule = serializer.save() | ||
import uuid | ||
PeriodicTask.objects.create( | ||
crontab=schedule, | ||
name=uuid.uuid1().__str__(), | ||
task='apps.schedule.tasks.add', | ||
) | ||
|
||
|
||
""" | ||
post | ||
{ | ||
"minute": "*", | ||
"hour": 3, | ||
"day_of_week": "*", | ||
"day_of_month": "*", | ||
"month_of_year": "*" | ||
} | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from __future__ import absolute_import, unicode_literals | ||
|
||
# This will make sure the app is always imported when | ||
# Django starts so that shared_task will use this app. | ||
from .celery import celery_app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from __future__ import absolute_import, unicode_literals | ||
import os | ||
from celery import Celery | ||
|
||
# set the default Django settings module for the 'celery' program. | ||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'settings.settings') | ||
|
||
celery_app = Celery('django-project-template') | ||
|
||
# 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. | ||
celery_app.config_from_object('django.conf:settings', namespace='CELERY') | ||
|
||
# Load task modules from all registered Django app configs. | ||
celery_app.autodiscover_tasks() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters