0

I have setup a scheduled task to run daily on PythonAnywhere.

The task uses the Django Commands as I found this was the preferred method to use with PythonAnywhere.

The tasks produces no errors but I don't get any output. 2022-06-16 22:56:13 -- Completed task, took 9.13 seconds, return code was 0.

I have tried uses Print() to debug areas of the code but I cannot produce any output in either the error or server logs. Even after trying print(date_today, file=sys.stderr).

I have set the path on the Scheduled Task as: (Not sure if this is correct but seems to be the only way I can get it to run without errors.)

workon advancementvenv && python3.8 /home/vicinstofsport/advancement_series/manage.py shell < /home/vicinstofsport/advancement_series/advancement/management/commands/schedule_task.py

I have tried setting the path as below but then it gets an error when I try to import from the models.py file (I know this is related to a relative import but cannot management to resolve it). Traceback (most recent call last): File "/home/vicinstofsport/advancement_series/advancement/management/commands/schedule_task.py", line 3, in <module> from advancement.models import Bookings ModuleNotFoundError: No module named 'advancement'

2022-06-17 03:41:22 -- Completed task, took 14.76 seconds, return code was 1.

Any ideas on how I can get this working? It all works fine locally if I use the command py manage.py scheduled_task just fails on PythonAnywhere.

Below is the task code and structure of the app.

from django.core.management.base import BaseCommand
import requests
from advancement.models import Bookings
from datetime import datetime, timedelta, date
import datetime
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail
from django.core.mail import send_mail
import os
from decouple import config


class Command(BaseCommand):
    help = 'Sends Program Survey'

def handle(self, *args, **kwargs):

# Get today's date
  date_today = datetime.datetime.now().date()


# Get booking data
  bookings = Bookings.objects.all()

# For each booking today, send survey email
  for booking in bookings:
    if booking.booking_date == date_today:
       if booking.program_type == "Sport Science":
       
        booking_template_id = 'd-bbc79704a31a4a62a5bfea90f6342b7a'
        email = booking.email
        booking_message = Mail(from_email=config('FROM_EMAIL'),
                        to_emails=[email],
                        )
        booking_message.template_id = booking_template_id
        try:
          sg = SendGridAPIClient(config('SG_API'))
          response = sg.send(booking_message)
        except Exception as e:
          print(e)

       else: 
        booking_template_id = 'd-3167927b3e2146519ff6d9035ab59256'
        email = booking.email
        booking_message = Mail(from_email=config('FROM_EMAIL'),
                        to_emails=[email],
                        )
        booking_message.template_id = booking_template_id

       try:
            sg = SendGridAPIClient(config('SG_API'))
            response = sg.send(booking_message)
       except Exception as e:
            print(e)

    else:
        print('No')

App Structure

Thanks in advance for any help.

Mark
  • 135
  • 1
  • 12
  • 1
    What do you mean by " just fails"? Have you tried to run it in the bash console on PythonAnywhere? – Filip Jun 17 '22 at 11:51
  • Thanks Filip for the help, if I try and run it from a bash console it works. (advancementvenv) 00:25 ~/advancement_series (master)$ python manage.py schedule_task" . When I mentioned it fails, it runs without error but it just does not seem to run the function, there is no output to say what has happened. – Mark Jun 18 '22 at 00:33
  • 1
    The most likely thing that may be happening is that your prints are not being reached (they're all in if statements and loops). Add prints in code that must be run and then you can use additional prints to debug why it's not going into the loops and the ifs. – Glenn Jun 18 '22 at 09:00

1 Answers1

1

Thanks Filip and Glenn, testing within the bash console and changing the directory in the task helped to fix the issue. Adding 'cd /home/vicinstofsport/advancement_series && to my task allowed the function to run.'

Mark
  • 135
  • 1
  • 12