0

I'm new in Django and I am creating a web application for uni project. I have to send emails periodically, and to do so I'm using a management command, but I don't know how to make it automatically run when I start the server. I'm working on Pycharm in Windows 8.1

from django.core.mail import send_mail
from django.core.management.base import BaseCommand
from ProgettoDinamici.settings import EMAIL_HOST_USER
from products.models import Notification
from users.models import User

class Command(BaseCommand):
    help = 'Sends emails periodically'

    def handle(self, *args, **options):
        users = User.objects.all()
        for u in users:
            try:
                notify = Notification.objects.filter(receiver=u, read=False)
                count = notify.count()
            except:
                print("No notification found")
            try:
                if notify:
                    send_mail(
                        'E-Commerce',
                        'You have ' + str(count) + ' notifications.',
                        EMAIL_HOST_USER,
                        [u.email],
                        fail_silently=False,
                    )
            except:
                print("error")

For now I tried to use schedule and cron to repeat the send_email every n minutes, but nothing worked and searching online I found out that cron (and cron based) ins't supported by Windows. But this is another problem...

  • [This question](https://stackoverflow.com/questions/51785215/setup-a-cron-job-in-django-in-windows) might help – Alasdair Apr 24 '19 at 15:50
  • use a cronjob with the python command with full path. – Eddwin Paz Apr 24 '19 at 16:47
  • Thank you for the answer. I searched the internet and I found out that cron/cronjob/schedule/etc doesn't work on windows, that's why I'm having all these problems – malandrino95 Apr 24 '19 at 18:01
  • Alasdair, thank you too, but this doesn't solve my problem, because in this way I have to start the scheduled task giving a cmd or by windows scheduler, but this is not what I want. What I want is to run the scheduled task when the server of my web app is running (after I run python manage.py runserver to be clear) – malandrino95 Apr 24 '19 at 18:11

2 Answers2

1

You can use celery for periodic tasks. Just convert the function handle into a celery task and you can schedule cron jobs on that tasks.

You can refer: https://docs.celeryproject.org/en/latest/userguide/periodic-tasks.html

Aman Garg
  • 2,507
  • 1
  • 11
  • 21
0

I know this is an older question, but hopefully someone will find this useful.

It seems you just want to run a script on an interval on a windows machine.

The windows equivalent to a cron job is a scheduled task, there is a post about it here.

However, really you should be thinking about the environment your deployed code will live in. If it is a *nix environment, a windows dev environment will make things hard. I recommend using a linux dev environment, perhaps in a VM, or by running code in a docker container.

theannouncer
  • 1,148
  • 16
  • 28