服务器虚拟环境下supervisor启动django-celery

最近在弄django+python+uwsgi的微信部署, 因为要给python加入定时任务, 用以定期改变数据库数据, 所以加入了supervisor.

supervisor安装参考文章,  3.3.2版本我是用的默认配置的supervisord.conf.

重点内容:

  1. 项目文件建立在/etc/supervisor/conf.d/ 目录下, touch yourfilename.conf

  2. 我的项目文件celery_worker.conf
    # supervisor的进程名称为celery_worker
    [program:celery_worker]
    # 进入工作目录
    directory=/home/bosheng/work/projects/rxiiodev
    # 执行celery指令
    command=python manage.py celery worker -l info
    autorestart=true
    loglevel=info
    redirect_stderr=true
    stdout_logfile=/var/log/supervisor/celery_worker.log# 在项目在虚拟环境下, 需要配置虚拟环境库所在的位置, 如下:environment=PYTHONPATH="$PYTHONPATH:/home/bosheng/work/vrxiio/lib/python2.7/site-packages"
    

  3. celery_beat.conf同上, 区别如下:
    [program:celery_beat]
    command=python manage.py celery beat
    stdout_logfile=/var/log/supervisor/celery_beat.log

  4. 需要用supervisor运行celery的worker进程, 还需要在tasks.py 文件里加入:
    from celery import platforms
    platforms.C_FORCE_ROOT = True

    我的celery tasks.py:
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    from celery import task, platforms
    from wauth.models import wxsetting, order, product,booking,slot,contactinfo,userprofile,vouch,vouchdef, pagecontent
    from datetime import datetime, timedelta
    
    platforms.C_FORCE_ROOT = True
    
    
    @task(name='wauth.tasks.order_timeout')
    def order_timeout():
        yesterday = (datetime.now()-timedelta(days=1)).date()
        qry = order.objects.filter(place_date__gt=yesterday, active=True)
        for p in qry:
            c = p.bookings.filter(active=True)
            for q in c:
                t = q.service_date.strftime('%Y-%m-%d')
                # 处理订单时间
                if q.connected_order.id == p.id:
                    tmp_second = (
                        datetime.now() - p.place_date.replace(tzinfo=None) - timedelta(hours=8)
                    ).total_seconds()
                    remain_time = 900 - tmp_second
                    if remain_time <= 0 and not q.connected_order.ispaid:
                        if q.active is True and p.active is True:
                            q.active = False
                            q.save()
                            p.active = False
                            p.save()
                        else:
                            pass
        return 'timeout order clear'

  5. 启动supervisor:
    sudo supervisord -c /etc/supervisor/supervisord.conf
    sudo supervisorctl -c /etc/supervisor/supervisord.conf


  6. sudo supervisorctl
    进入supervisor交互指令, 参考:
    # 查看进程状态
    supervisorctl status
    # 停止某一个进程,program_name 为 [program:x] 里的 x
    supervisorctl stop program_name
    # 启动某个进程
    supervisorctl start program_name
    # 重启某个进程
    supervisorctl restart program_name
    # 结束所有属于名为 groupworker 这个分组的进程 (start,restart 同理)
    supervisorctl stop groupworker:
    # 结束 groupworker:name1 这个进程 (start,restart 同理)
    supervisorctl stop groupworker:name1
    # 停止全部进程,注:start、restart、stop 都不会载入最新的配置文件
    supervisorctl stop all
    # 载入最新的配置文件,停止原有进程并按新的配置启动、管理所有进程
    supervisorctl reload
    # 根据最新的配置文件,启动新配置或有改动的进程,配置没有改动的进程不会受影响而重启
    supervisorctl update

  7. 重新进入supervisorctl交互:
    sudo supervisorctl -c /etc/supervisor/supervisord.conf

  8. 一些疑问:
    (1) 为什么要用sudo来启动supervisor?
    (2) 怎样输出celery worker 和beat的日志文件?
    解决: supervisor和独立进程的日志所在在文件夹需要手动建立, 如在log下建立supervisor文件夹: /var/log/supervisor/
    (3) 加入C_FORCE_ROOT = True 会带来什么影响?
    疑似解决: 允许root权限运行celery




你可能感兴趣的:(python,django)