This commit is contained in:
xieshen 2020-11-18 15:48:54 +08:00
commit f67cffe12f
4 changed files with 18 additions and 9 deletions

View File

@ -1,4 +1,8 @@
from django.apps import AppConfig from django.apps import AppConfig
from . import tasks
class PollsConfig(AppConfig): class PollsConfig(AppConfig):
name = 'polls' name = 'polls'
def ready(self):
tasks.process_notify_task(repeat=5)

View File

@ -222,3 +222,5 @@ class SMSNotifyRecord(models.Model):
id = models.UUIDField('id', primary_key=True, default=uuid.uuid4) id = models.UUIDField('id', primary_key=True, default=uuid.uuid4)
phone = models.CharField('phone', max_length=32) phone = models.CharField('phone', max_length=32)
added = models.DateField(auto_now_add=True) added = models.DateField(auto_now_add=True)
notice_id = models.CharField('notice_id', max_length=64)
updated = models.DateTimeField(auto_now=True)

View File

@ -1,12 +1,14 @@
from background_task import background from background_task import background
from datetime import date, datetime, timedelta from datetime import date, datetime, timedelta
import time
from polls.models import Notice, SMSNotifyRecord from polls.models import Notice, SMSNotifyRecord
from polls.utils import sent_sms_notify from polls.utils import sent_sms_notify
@background(remove_existing_tasks=True) @background(remove_existing_tasks=True)
def process_notify_task(): def process_notify_task():
print('start task')
start = time.process_time()
notices = Notice.objects.filter(is_read=False) notices = Notice.objects.filter(is_read=False)
for n in notices: for n in notices:
two_hours_later = n.added + timedelta(minutes=2) two_hours_later = n.added + timedelta(minutes=2)
@ -15,10 +17,13 @@ def process_notify_task():
phone = n.user.username phone = n.user.username
today = now.date() today = now.date()
hour = now.hour hour = now.hour
exists = SMSNotifyRecord.objects.filter(phone=phone, added=today).exists() exists = SMSNotifyRecord.objects.filter(
if exists: return phone=phone, added=today, notice_id=n.id).exists()
if hour < 8 or hour > 17: return if exists:
return
if hour < 8 or hour > 20:
return
print('============', phone, n.type) print('============', phone, n.type)
SMSNotifyRecord.objects.create(phone=phone) SMSNotifyRecord.objects.create(phone=phone)
sent_sms_notify(phone, n.type) sent_sms_notify(phone, n.type)
print('end task', time.process_time() - start)

View File

@ -74,5 +74,3 @@ urlpatterns = [
path('app/download/', views.download, name='polls_app_download'), path('app/download/', views.download, name='polls_app_download'),
path('app/has/update/', views.has_update, name='polls_app_has_update') path('app/has/update/', views.has_update, name='polls_app_has_update')
] ]
tasks.process_notify_task(repeat=5)