Reputation: 549
My clock.py below. I can use heroku run python manage.py send_queued_mail
all day long but including run python manage.py send_queued_mail
in a clock.py file causes a syntax error.
clock.py:
from apscheduler.scheduler import Scheduler
sched = Scheduler()
@sched.interval_schedule(minutes=1)
def timed_job():
python manage.py send_queued_mail
sched.start()
while True:
pass
Heroku error message (from log):
2014-01-24T01:31:47.256648+00:00 app[clock.1]: File "clock.py", line 7
2014-01-24T01:31:47.256648+00:00 app[clock.1]: run python manage.py send_queued_mail
2014-01-24T01:31:47.256648+00:00 app[clock.1]: ^
2014-01-24T01:31:47.256648+00:00 app[clock.1]: SyntaxError: invalid syntax
EDIT:
management.call_command('send_queued_mail')
works from the python shell on heroku, but fails with a "command not found" error when I run it from the clock.py file. I can clearly see send_queued_email.py file where it is supposed to be. And if I run python manage.py help
I can see 1send_queued_email` as an available command. Somehow running the command from clock.py is causing heroku to not fond the command.
Upvotes: 1
Views: 959
Reputation: 549
I finally figured this out. When calling my command from a python file, I needed to explicitly set DJANGO_SETTINGS_MODULE
to mysite.settings
. Below is the final file for anyone else who ever decides they want to schedule async email sends from a Django app on Heroku with greater frequency than the once-every-5-minutes supported by the Heroku Scheduler add-on.
# clock.py
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'anonqaproject.settings'
from apscheduler.scheduler import Scheduler
from post_office import utils
sched = Scheduler()
@sched.interval_schedule(minutes=1)
def timed_job():
utils.send_queued_mail()
sched.start()
while True:
pass
Upvotes: 1
Reputation: 410652
Because
python manage.py send_queued_mail
is not valid Python code.
You can call a management command programmatically using django.core.management.call_command
:
from django.core.management import call_command
call_command('send_queued_mail')
Upvotes: 2