Prometheus Prometheus - 1 year ago 161
Python Question

How to check if Celery/Supervisor is running using Python

How to write a script in Python that outputs if celery is running on a machine (Ubuntu)?

My use-case. I have a simple python file with some tasks. I'm not using Django or Flask. I use supervisor to run the task queue. For example,

from celery import Celery, task
app = Celery('tasks')
def add_together(a, b):
return a + b


directory = /var/app/
command=celery -A tasks worker info

This all works, I now want to have page which checks if celery/supervisor process is running. i.e. something like this maybe using Flask allowing me to host the page giving a 200 status allowing me to load balance.

For example...

from flask import Flask

app = Flask(__name__)

def status_check():

#check supervisor is running
if supervisor:
return render_template('up.html')
return render_template('down.html')

if __name__ == '__main__':

Answer Source

You can run the celery status command via code by importing the celery.bin.celery package:

import celery
import celery.bin.base
import celery.bin.celery
import celery.platforms

app = celery.Celery('tasks', broker='redis://')

status = celery.bin.celery.CeleryCommand.commands['status']() = status.get_app()

def celery_is_up():
        return True
    except celery.bin.base.Error as e:
        if e.status == celery.platforms.EX_UNAVAILABLE:
            return False
        raise e

if __name__ == '__main__':
    if celery_is_up():
        print('Celery up!')
        print('Celery not responding...')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download