newmediamonitoring/polls/tasks.py

41 lines
1.5 KiB
Python

from background_task import background
from datetime import date, datetime, timedelta
import time
from polls.models import Notice, SMSNotifyRecord, VoiceNotifyRecord
from polls.utils import send_voice_notify, sent_sms_notify
@background(remove_existing_tasks=True)
def process_notify_task():
print('start task')
start = time.process_time()
notices = Notice.objects.filter(is_read=False)
for n in notices:
two_hours_later = n.added + timedelta(minutes=2)
four_hours_later = n.added + timedelta(minutes=4)
now = datetime.now()
phone = n.user.username
today = now.date()
hour = now.hour
if two_hours_later < now:
exists = SMSNotifyRecord.objects.filter(
phone=phone, added=today, notice_id=n.id).exists()
if exists:
break
if hour < 8 or hour > 20:
break
print('============', "sms ", phone, n.type, n.id)
SMSNotifyRecord.objects.create(phone=phone, notice_id=n.id)
sent_sms_notify(phone, n.type)
if four_hours_later < now:
exists = VoiceNotifyRecord.objects.filter(
phone=phone, added=today, notice_id=n.id).exists()
if exists:
break
if hour < 8 or hour > 20:
break
print('============', "voice ", phone, n.type, n.id)
VoiceNotifyRecord.objects.create(phone=phone, notice_id=n.id)
send_voice_notify(phone, n.type)
print('end task', time.process_time() - start)