How to check if Celery/Supervisor is running using Python

时间:2015-10-30 23:34:31

标签: python flask celery supervisor

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, tasks.py from celery import Celery, task app = Celery('tasks') @app.task() def add_together(a, b): return a + b Supervisor: [program:celery_worker] 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... check_status.py from flask import Flask app = Flask(__name__) @app.route('/') def status_check(): #check supervisor is running if supervisor: return render_template('up.html') else: return render_template('down.html') if __name__ == '__main__': app.run()

7 个答案:

答案 0 :(得分:18)

您可以通过导入celery status

来通过代码运行celery.bin.celery命令
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.app = status.get_app()

def celery_is_up():
    try:
        status.run()
        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!')
    else:
        print('Celery not responding...')

答案 1 :(得分:2)

如何使用子进程,不确定它是否是一个好主意:

>>> import subprocess
>>> output = subprocess.check_output('ps aux'.split())
>>> 'supervisord' in output
True

答案 2 :(得分:2)

您可以从supervisorctl status输出

解析process state
import subprocess

def is_celery_worker_running():
    ctl_output = subprocess.check_output('supervisorctl status celery_worker'.split()).strip()
    if ctl_output == 'unix:///var/run/supervisor.sock no such file':
        # supervisord not running
        return False
    elif ctl_output == 'No such process celery_worker':
        return False
    else:
        state = ctl_output.split()[1]
        return state == 'RUNNING'

答案 3 :(得分:1)

A sparse web user interface comes with supervisor. May be you could use that. It can be enabled in the supervisor config. Key to look for is [inet_http_server]

You could even look at the source code of that piece to get ideas to implement your own.

答案 4 :(得分:0)

似乎Rotten194 's answer中的这一行:

status.app = status.get_app()

应该是

status.app = status.get_app(app)

答案 5 :(得分:0)

@vgel's answer启发,使用Celery 4.3.0。

def user_task_entry(request):
    title = 'Assign Task'
    form = TaskForm(request.POST or None) 
    if form.is_valid():
        instance = form.save(commit=False)
        instance.assigned_by = request.user
        instance.save()
        return redirect('taskmis:user_task_list')
    context = {
        "title": title,
        "form": form,
     }
    return render(request, "task_entry.html",context)

答案 6 :(得分:-2)

根据我的经验,我会设置一条消息来跟踪它是否完整,以便队列负责重试任务。

相关问题