From 282aa757199e5942f16915c1003b8018d7a2ecb8 Mon Sep 17 00:00:00 2001 From: xianfuxing Date: Mon, 13 Aug 2018 00:05:24 +0800 Subject: [PATCH] # up celery task for counter statistic --- apps/mosquito/models.py | 2 +- apps/mosquito/tasks.py | 23 +++++++++++++++++++++++ mosqkiller/__init__.py | 7 +++++++ mosqkiller/celery.py | 22 ++++++++++++++++++++++ mosqkiller/settings.py | 21 ++++++++++++++++++++- 5 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 apps/mosquito/tasks.py create mode 100644 mosqkiller/celery.py diff --git a/apps/mosquito/models.py b/apps/mosquito/models.py index adee27b..9b79fa4 100644 --- a/apps/mosquito/models.py +++ b/apps/mosquito/models.py @@ -43,7 +43,7 @@ class MosqPost(models.Model): pass -class MosqPostStatic(models.Model): +class MosqPostStatistic(models.Model): increment = models.PositiveIntegerField(verbose_name='增量') date = models.DateField(auto_now_add=True, verbose_name='日期') diff --git a/apps/mosquito/tasks.py b/apps/mosquito/tasks.py new file mode 100644 index 0000000..5d80caf --- /dev/null +++ b/apps/mosquito/tasks.py @@ -0,0 +1,23 @@ +import pytz +from datetime import datetime, time +from django.db.models import Max +from celery import task +from counter.models import DeviceCount +from mosquito.models import MosqPostStatistic + + +@task() +def update_daily_statistic(): + queryset = DeviceCount.objects.raw( + 'select id, device_id, max(data_time) as max_date ' + 'from device_count group by device_id, date(data_time) order by max_date') + + if queryset: + ret = [] + for q in queryset: + entry = DeviceCount.objects.get(device_id=q.device_id, data_time=q.data_time) + ret.append((entry.data_time, entry.device_id, entry.count)) + +@task() +def update_latest_statistic(): + pass \ No newline at end of file diff --git a/mosqkiller/__init__.py b/mosqkiller/__init__.py index e69de29..de98902 100644 --- a/mosqkiller/__init__.py +++ b/mosqkiller/__init__.py @@ -0,0 +1,7 @@ +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 app as celery_app + +__all__ = ['celery_app'] \ No newline at end of file diff --git a/mosqkiller/celery.py b/mosqkiller/celery.py new file mode 100644 index 0000000..8dccb91 --- /dev/null +++ b/mosqkiller/celery.py @@ -0,0 +1,22 @@ +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', 'stargazer.settings') + +app = Celery('stargazer') + +# 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') + +# Load task modules from all registered Django app configs. +app.autodiscover_tasks() + + +@app.task(bind=True) +def debug_task(self): + print('Request: {0!r}'.format(self.request)) diff --git a/mosqkiller/settings.py b/mosqkiller/settings.py index a35a5c4..c40839a 100644 --- a/mosqkiller/settings.py +++ b/mosqkiller/settings.py @@ -13,6 +13,7 @@ https://docs.djangoproject.com/en/2.0/ref/settings/ import os import sys import datetime +from celery.schedules import crontab # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) @@ -45,6 +46,7 @@ INSTALLED_APPS = [ 'mosquito', 'smart', 'counter', + 'django_celery_results', ] MIDDLEWARE = [ @@ -179,7 +181,7 @@ REST_FRAMEWORK = { } JWT_AUTH = { - 'JWT_EXPIRATION_DELTA': datetime.timedelta(seconds=1800), + 'JWT_EXPIRATION_DELTA': datetime.timedelta(seconds=3600), } # Custom auth backend @@ -196,4 +198,21 @@ CACHES = { "CLIENT_CLASS": "django_redis.client.DefaultClient", } } +} + +# Celery settings +CELERY_BROKER_URL = 'redis://127.0.0.1:6379/2' +CELERY_RESULT_BACKEND = 'django-db' +CELERY_ACCEPT_CONTENT = ['json'] +CELERY_TASK_SERIALIZER = 'json' +CELERY_TIMEZONE = 'Asia/Shanghai' +CELERY_BEAT_SCHEDULE = { + 'update-daily-statistic': { + 'task': 'counter.tasks.update_daily_statistic', + 'schedule': crontab(minute='*/1'), + }, + 'update-latest-statistic': { + 'task': 'counter.tasks.update_latest_statistic', + 'schedule': crontab(minute='*/1'), + }, } \ No newline at end of file