M
M
Maxim Tarabrin2018-08-21 14:23:40
Django
Maxim Tarabrin, 2018-08-21 14:23:40

Why can't find the job id in the database?

Used the django-apscheduler package. Here is the scheduler code:

import time

from apscheduler.schedulers.background import BackgroundScheduler
from django.conf import settings
from django.contrib.sites.models import Site
from django.core.mail import send_mail
from django.template.loader import render_to_string
from django.urls import reverse
from django_apscheduler.jobstores import DjangoJobStore, register_events, register_job

from surveys import models

scheduler = BackgroundScheduler()
scheduler.add_jobstore(DjangoJobStore(), "default")


def send_survey_mail(survey_mail_sent):
    mail_context = {
        'survey_name': survey_mail_sent.survey.name,
        'survey_unique_url': ('' if Site.objects.get_current().domain.startswith(
            'http://') else 'http://') + Site.objects.get_current().domain + reverse('surveys', args=(
            survey_mail_sent.survey.unique_url,))
    }
    mail_new_survey_content = render_to_string('mail/new_survey.html', mail_context)
    send_mail(subject='У вас есть возможность пройти опрос',
              message=mail_new_survey_content,
              html_message=mail_new_survey_content,
              from_email='%[email protected]%s' % (settings.EMAIL_HOST_USER, settings.EMAIL_HOST),
              recipient_list=[survey_mail_sent.user.email])
    survey_mail_sent.is_sent = True
    survey_mail_sent.save()
    return True


@register_job(scheduler, "interval", seconds=settings.APS_INTERVAL_SENT_SECS)
def check_state_is_sent():
    surveys_mail_sent = models.SurveyMailSent.objects.filter(is_sent=False)
    # Начало загрузки операций для отправки сообщений
    for survey_mail_sent in surveys_mail_sent:
        job = scheduler.add_job(send_survey_mail, args=[survey_mail_sent])
    time.sleep(2)


register_events(scheduler)

scheduler.start()
print("Планировщик стартовал!")

When the scheduler is running, messages are poured into the console:
Job with id d30d9e376dad47418313d1bcc214d793 not found in database
Job with id 41af402db0d644d79237f6c6e6027ce5 not found in database
Job with id d30d9e376dad47418313d1bcc214d793 not found in database

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question