Forums

Tasks email schedule problem

Hi!

I made an html page and if I refresh it, a new email sent to my chosen users. It works fine if I manually reach this page. If I add a new task to PA to refresh this page daily nothing happens. I can't find anything in the server or error log, nothing appears about this task. The url to this page: mysite/performance/emails I added this url to the tasks: /home/myusername/folder/performance/emails

This is the first time I try to use the task scheduler. Am I doing something wrong?

Scheduled tasks have their own logs -- the link is the first button in the "Actions" column on the task table. Is anything showing up in there?

Yes, I found the log, thank you. The path that I added is fine, it ran the file I made but it say: Permission denied.

Looking at your tasks' configuration, it looks like you've scheduled a .html file, which is not something I'd expect. A scheduled task will just run the file, just as if you'd typed the path into a Bash console, so HTML won't run there.

What you need to do is write a script which, if it's run from a Bash console, will achieve what you want to do. If you want (say) a particular view on your website to be accessed periodically, you could do that by using curl to hit the appropriate URL. But a more elegant solution would be to write a Python script to actually run the code directly without accessing your site. If you're using Django, then I would recommend that you read up on custom Django management commands, as they're the most elegant way to achieve that.

Thank you but I think my experience is under that I could write a management command. Curl looks easier to me.

I tried the same code as a py file without the html part. If I try to run it as a task I get this:

ImportError: attempted relative import with no known parent package

If I can't run this script as a task which cases could the task function be helpful?

Management commands make everything much easier.

What do you try to import there?

This is the whole code I try to use:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/python
# -*- coding: cp852 -*-
import os, sys
from django.contrib import messages
from .models import Projekt_megtartas
from django.contrib.auth.decorators import user_passes_test
from django.core.mail import send_mass_mail
import datetime
import calendar

today = datetime.date.today()
weekday = today.weekday()
month = datetime.date.today().month
year = datetime.date.today().year
cal = calendar.monthrange(year, month)[1] # az utolso nap az aktualis honapban
firstday = datetime.date.today().replace(day=4) # az elso nap az aktualis honapban
subject='helloooo'

for p in Projekt_megtartas.objects.raw('SELECT * FROM stressz_projekt_megtartas INNER JOIN auth_user ON auth_user.id = stressz_projekt_megtartas.jogosult_01_mm_id'):
    print(p.first_name)

message = 'some text here'
from_email='someone@xy.com'
recipient_list = Projekt_megtartas.objects.raw('SELECT * FROM stressz_projekt_megtartas INNER JOIN auth_user ON auth_user.id = stressz_projekt_megtartas.jogosult_01_mm_id')
print(recipient_list)

if (today == firstday):    
    messages = [(subject, message, from_email, [r.email]) for r in recipient_list]
    send_mass_mail(messages)

    print('Success')
else:
    print('Nope)

This is the error I get:

Traceback (most recent call last): File "/.../performance/emails.py", line 9, in <module> from .models import Profile, Projekt_megtartas ImportError: attempted relative import with no known parent package

The reference to the model is fine, it works well in other files in the same app.

Since it's a relative import, the import may work in one file and not in another depending on the location in the class hierarchy. The Python PEP 8 suggests that absolute imports should be used unless you have a good reason to use a relative import instead.

OK, thank you :)