-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated documentation and added support for multiple queues.
- Loading branch information
Showing
5 changed files
with
122 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
celery_heartbeat/management/commands/trigger_celery_heartbeat.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
from __future__ import unicode_literals, print_function, absolute_import, division | ||
|
||
from django.core.management.base import BaseCommand | ||
from celery_heartbeat.tasks import update_heartbeat | ||
from celery_heartbeat.tasks import start_update_heartbeat | ||
|
||
|
||
class Command(BaseCommand): | ||
|
||
def handle(self, *args, **options): | ||
update_heartbeat.delay() | ||
start_update_heartbeat.delay() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,71 @@ | ||
from __future__ import unicode_literals, print_function, absolute_import, division | ||
|
||
from django.conf import settings | ||
from django.core.cache import cache | ||
from celery import Celery | ||
|
||
import time | ||
import logging | ||
|
||
def get_cache_key(): | ||
return getattr(settings, 'CELERY_HEARTBEAT_CACHE_KEY', 'CELERY_HEARTBEAT_CACHE_KEY') or 'CELERY_HEARTBEAT_CACHE_KEY' | ||
|
||
logger = logging.getLogger('celery_heartbeat') | ||
DEFAULT_CELERY_HEARTBEAT_DELAY_THRESHOLD = 5 * 60 | ||
|
||
|
||
def get_cache_key(queue_name): | ||
'''Returns the name of the cache key to use for the given queue.''' | ||
|
||
prefix = getattr(settings, 'CELERY_HEARTBEAT_CACHE_KEY', 'CELERY_HEARTBEAT_CACHE_KEY') | ||
prefix = prefix or 'CELERY_HEARTBEAT_CACHE_KEY' | ||
|
||
return '{}-{}'.format(prefix, queue_name) | ||
|
||
|
||
def get_known_queues(): | ||
'''Returns a list of monitored celery queues.''' | ||
|
||
app = Celery() | ||
app.config_from_object('django.conf:settings') | ||
|
||
queues = list(set(getattr(settings, 'CELERY_HEARTBEAT_MONITORED_QUEUES', []) or [])) | ||
if not queues: | ||
queues = list(app.amqp.queues.keys()) | ||
|
||
return queues | ||
|
||
|
||
def check_heartbeat(threshold): | ||
'''Checks if any of the queues have fallen behind by more than threshold | ||
seconds and returns human-readable status.''' | ||
|
||
errors = [] | ||
|
||
for queue in get_known_queues(): | ||
|
||
cache_key = get_cache_key(queue) | ||
heartbeat = cache.get(cache_key) | ||
if not heartbeat: | ||
continue | ||
|
||
delta = long(time.time() - heartbeat) | ||
|
||
if delta > threshold: | ||
errors.append('Celery queue "{}" has fallen behind by {} seconds.'.format(queue, delta)) | ||
|
||
return errors | ||
|
||
|
||
def check_and_log_heartbeat(): | ||
'''Checks if any of the queues have fallen behind by more than threshold | ||
seconds and logs any queues that did.''' | ||
|
||
errors = check_heartbeat(get_threshold()) | ||
if errors: | ||
logger.error('\n'.join(errors)) | ||
|
||
|
||
def get_threshold(): | ||
'''Returns the configured or default threshold for queues falling behind, in seconds.''' | ||
|
||
threshold = getattr(settings, 'CELERY_HEARTBEAT_DELAY_THRESHOLD', DEFAULT_CELERY_HEARTBEAT_DELAY_THRESHOLD) | ||
return threshold or DEFAULT_CELERY_HEARTBEAT_DELAY_THRESHOLD |