背景
需求为工作流审批,到达某状态后,超过一段时间没有审批,则流程自动审批到下一个状态
方案
想到使用计划任务来自动流转流程,因为项目本身是Django,这里使用djcelery来完成
实例
项目依赖
1 2
| pip install django-celery pip install flower
|
settings.py配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| INSTALLED_APPS = ( ... 'djcelery', ) import djcelery djcelery.setup_loader() BROKER_URL = 'redis://127.0.0.1:6379/1' CELERYBEAT_SCHEDULER = 'djcelery.schedulers.DatabaseScheduler' CELERY_RESULT_BACKEND = 'redis://127.0.0.1:6379/1' CELERY_ACCEPT_CONTENT = ['application/json'] CELERY_TASK_SERIALIZER = 'json' CELERY_RESULT_SERIALIZER = 'json' CELERY_TIMEZONE = 'Asia/Shanghai' CELERY_TASK_RESULT_EXPIRES = 3600 CELERYD_FORCE_EXECV = True CELERYD_PREFETCH_MULTIPLIER = 1 CELERYD_MAX_TASKS_PER_CHILD = 100
|
接下来在settings.py的同级目录中新建 celery.py文件
1 2 3 4 5 6 7 8 9 10 11
| from __future__ import absolute_import, unicode_literals import os from celery import Celery from django.conf import settings os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'Server.settings') app = Celery('Server') app.config_from_object('django.conf:settings') app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
|
修改init.py添加
1 2 3
| from __future__ import absolute_import, unicode_literals from .celery import app as celery_app
|
在对应的app中添加tasks.py文件(这里必须为tasks.py文件,不然在admin中添加任务时找不到task)
1 2 3 4 5 6 7 8 9 10 11
| from __future__ import absolute_import, unicode_literals from celery import shared_task, task @shared_task def auto_audit(): print('func auto audit') @task() def add(x, y): print('x + y = ', x + y) return x + y
|
在views.py中调用
1 2 3
| from .tasks import add add.delay(3, 5)
|
celery启动,当然实际环境中应该使用supervisor启动
1 2 3 4
| python manage.py celery worker -l info python manage.py celery beat -l info python manage.py celery worker -B -l info python manage.py celery flower --address=127.0.0.1 --port=5555 --basic_auth=admin:admin
|
Admin后台添加计划任务
在DJCELERY中Periodic tasks添加计划任务即可
Flower监控celery
主要用于监控任务执行是否成功,和broker和worker对应的状态