Home > Blockchain >  APScheduler is not starting on runserver in django
APScheduler is not starting on runserver in django

Time:07-23

I followed a particular tutorial to implement APScheduler in my application but when I run my project the job is not called at all and I am having a hard time figuring what I did wrong

The project directory looks like the following:

reports
  auto_mails
    __init__.py
    send_mail.py
  migrations
  __init__.py
  admin.py
  allotmentkeys.py
  apps.py
  models.py
  tests.py
  urls.py
  views.py

and views.py

class AutoMails(APIView):

    def send_mail_test(self):
        subject = 'Password Reset Request'
        message = ' Password Reset Request. OTP is 454545 '
        email_from = '[email protected]'
        recipient_list = ['[email protected]', ]
        print("called")
        send_mail(subject, message, email_from, recipient_list, fail_silently=False)

send_mail.py

from apscheduler.schedulers.background import BackgroundScheduler
from reports.views import AutoMails

def start():
    print("reached")
    scheduler = BackgroundScheduler()
    mail = AutoMails()
    scheduler.add_job(mail.send_mail_test(),"interval", seconds=10, id="test_mails_001", replace_existing=True )
    scheduler.start()

apps.py

from django.apps import AppConfig

class ReportsConfig(AppConfig):
    name = 'reports'

    def ready(self):
        print("Sending Mails ..")
        from auto_mails import send_mail
        send_mail.start()

urls.py

  path('auto-mails/', AutoMails.as_view()),

When I run the project using python manage.py runserevr I get the following:

C:\Users\Rahul Sharma\PycharmProjects\multitennant_v2\reports\views.py changed, reloading.
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
July 22, 2022 - 12:25:31
Django version 3.0.5, using settings 'multitennant_v2.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.

where I was expecting to see Sending Mails.. given the scheduler has started but it hasn't reached any function ` What is it that I am missing?

Update: I got it working by adding reports.apps.ReportsConfig in INSTALLED_APPS instead of 'reports`

However I Updated the code like following:

from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.triggers.combining import OrTrigger
from apscheduler.triggers.cron import CronTrigger

from reports.views import AutoMails

def start():
    print("reached")
    scheduler = BackgroundScheduler()
    mail = AutoMails()
    trigger = OrTrigger([CronTrigger(hour=17, minute=9)])
    scheduler.add_job(mail.send_mail_test, trigger, seconds=10, id="test_mails_001", replace_existing=True )
    scheduler.start()

Adding a Time for trigger and it working in my local server but as I deployed it to AWS it stopped working

CodePudding user response:

I need to ask simple question... Are you sure you include your reports app in INSTALLED_APPS under settings.py file?

You should write it this way:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites',
    'django.contrib.flatpages',
    'django.contrib.humanize',
    'reports.apps.ReportsConfig',
]

The ready method in AppConfig class is not called probably because of that.

CodePudding user response:

You have called the function --> mail.send_mail_test(). Just pass the reference of of the function ==> mail.send_mail_test

  • Related