Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions etc/systemd/system/patchman-celery-beat.service
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ After=network-online.target

[Service]
Type=simple
Restart=on-failure
RestartSec=10
User=patchman
Group=patchman
Environment="REDIS_HOST=127.0.0.1"
Expand Down
3 changes: 3 additions & 0 deletions etc/systemd/system/patchman-celery-worker@.service
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ After=network-online.target

[Service]
Type=simple
Restart=on-failure
RestartSec=10
User=patchman
Group=patchman
Environment="REDIS_HOST=127.0.0.1"
Expand All @@ -19,6 +21,7 @@ ExecStart=/usr/bin/celery \
--task-events \
--pool ${CELERY_POOL_TYPE} \
--concurrency ${CELERY_CONCURRENCY} \
--loglevel info \
--hostname patchman-celery-worker%i@%%h

[Install]
Expand Down
14 changes: 14 additions & 0 deletions patchman/celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,24 @@
import os

from celery import Celery
from celery.signals import task_prerun

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'patchman.settings') # noqa
from django.conf import settings # noqa

app = Celery('patchman')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()


@task_prerun.connect
def close_stale_connections(**kwargs):
"""Close stale DB connections before each task.

Django does this automatically for HTTP requests but not for Celery
tasks. Without this, long-lived workers hit 'server has gone away'
(MySQL) or 'server closed the connection unexpectedly' (PostgreSQL)
when the DB server drops idle connections.
"""
from django import db
db.close_old_connections()
Loading