Skip to content

Instantly share code, notes, and snippets.

@kgantsov
Created November 29, 2018 12:16
Show Gist options
  • Save kgantsov/731406daccf2558f7a5e4f4c19cca39b to your computer and use it in GitHub Desktop.
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
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