Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Running send_queued_mail every 60s on Heroku - opens a new db connection every time - maxing out my connections #45

Closed
ldm616 opened this issue Jan 29, 2014 · 5 comments

Comments

@ldm616
Copy link

ldm616 commented Jan 29, 2014

Does send_queued_mail close it's db connection when done? Doesn't seem to be. Perhaps it is how I am calling it via a scheduler so it runs every 60s. That file below as an fyi


import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'anonqaproject.settings'
import logging
logging.basicConfig()
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

@selwin
Copy link
Collaborator

selwin commented Jan 30, 2014

You should cron send_queued_mail management command instead of calling send_queued_mail directly.

python manage.py send_queued_mail

@selwin selwin closed this as completed Jan 30, 2014
@ldm616
Copy link
Author

ldm616 commented Jan 30, 2014

Normally I would, but Heroku doesn't support cron jobs.

@selwin
Copy link
Collaborator

selwin commented Jan 30, 2014

This may not be the most up to date of instructions, but you can try something like this: http://guidovanoorschot.nl/adding-cron-jobs-to-a-django-project-with-heroku-scheduler/

Sent from my phone

On Jan 30, 2014, at 10:04 PM, ldm616 notifications@github.com wrote:

Normally I would, but Heroku doesn't support cron jobs.


Reply to this email directly or view it on GitHub.

@ldm616
Copy link
Author

ldm616 commented Jan 30, 2014

Thanks. Scheduler works - but the max frequency is once every 5 mins. I
need once every minute.

My pseudo cron job on Heroku using apscheduler works, but it seems to open
a new database connection each time it runs and starts to max out my
connections. I've tried using a database pooler, but that doesn't help.

On Thu, Jan 30, 2014 at 6:22 AM, Selwin Ong notifications@github.comwrote:

This may not be the most up to date of instructions, but you can try
something like this:
http://guidovanoorschot.nl/adding-cron-jobs-to-a-django-project-with-heroku-scheduler/

Sent from my phone

On Jan 30, 2014, at 10:04 PM, ldm616 notifications@github.com wrote:

Normally I would, but Heroku doesn't support cron jobs.

Reply to this email directly or view it on GitHub.

Reply to this email directly or view it on GitHubhttps://github.com//issues/45#issuecomment-33691461
.

@ldm616
Copy link
Author

ldm616 commented Jan 30, 2014

SOLVED.

I read somewhere that Django doesn't close db connections after running management commands. I tweaked my pseudo cron job as follows to explicitly close the db connection after send_queued_mail runs.

...
from django.core.management import call_command
from django import db

@sched.interval_schedule(minutes=1)
def timed_job():
    call_command('send_queued_mail')
    db.close_connection()

...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants