最近在弄django+python+uwsgi的微信部署, 因为要给python加入定时任务, 用以定期改变数据库数据, 所以加入了supervisor.
supervisor安装参考文章, 3.3.2版本我是用的默认配置的supervisord.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"
[program:celery_beat]
command=python manage.py celery beat
stdout_logfile=/var/log/supervisor/celery_beat.log
from celery import platforms
platforms.C_FORCE_ROOT = True
#!/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'
sudo supervisord -c /etc/supervisor/supervisord.conf
sudo supervisorctl -c /etc/supervisor/supervisord.conf
# 查看进程状态
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
sudo supervisorctl -c /etc/supervisor/supervisord.conf