Created
November 29, 2018 12:16
-
-
Save kgantsov/731406daccf2558f7a5e4f4c19cca39b to your computer and use it in GitHub Desktop.
Python function that prints currently running celery tasks that are running longer that number of seconds specified in threshold param
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
from datetime import datetime | |
import time | |
import kombu.five | |
from celery.task.control import inspect | |
def get_stuck_celery_tasks(threshold=600): | |
i = inspect() | |
for worker, tasks in i.active().items(): | |
for task in tasks: | |
# convert celery monotonic time_start to a time | |
# https://stackoverflow.com/a/20096342/696248 | |
task_started = time.time() - (kombu.five.monotonic() - task['time_start']) | |
if time.time() - task_started > threshold: | |
print( | |
'Worker: {} task: {} => {} args: {}, kwargs: {} started: {} running for: {}'.format( | |
worker, | |
task['name'], | |
task['id'], | |
task['args'], | |
task['kwargs'], | |
task_started, | |
time.time() - task_started | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment