• 欢迎访问蜷缩的蜗牛博客 蜷缩的蜗牛
  • 微信搜索: 蜷缩的蜗牛 | 联系站长 kbsonlong@qq.com
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

利用django admin后台配置celery定时任务

Django 蜷缩的蜗牛 1年前 (2017-04-20) 42次浏览 已收录 0个评论

1、安装 djcelery

 

pip install django-celery

 

2、在 Django 项目 setting 配置

A、配置 djcelery

# CELERY STUFF
import djcelery
djcelery.setup_loader()
BROKER_URL = 'redis://localhost:6379'
CELERYBEAT_SCHEDULER = 'djcelery.schedulers.DatabaseScheduler' # 定时任务
CELERY_RESULT_BACKEND = 'djcelery.backends.database:DatabaseBackend'
CELERY_RESULT_BACKEND = 'redis://localhost:6379'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = 'Asia/Shanghai'

 

B、配置 APPS

INSTALLED_APPS = (
    # 'bootstrap_admin',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'djcelery',    )

 

3、创建一个用 Saltstack API 收集主机信息 task

from celery import task
from saltstack.saltapi import SaltAPI

@task
def server_collects(tgt,server_id):
    contexts = {'server_list': server_list, 'server_id': server_id}
    try:
        sapi = SaltAPI(url="http://192.168.62.200:8000", username="kbson", password="kbson")
        print "http://192.168.62.200:8000"
        grains = sapi.SaltCmd(tgt=tgt, fun='grains.items', client='local')['return'][0]
        minions = sapi.key_list('manage.status', client='runner')['return'][0]
        if salt_server and grains:
            for i in grains.keys():
                try:
                    server = Servers.objects.get(local_ip=i)
                except:
                    server = Servers()
                if i in minions['up']:
                    minions_status = '0'
                else:
                    minions_status = '1'
                server.hostname = grains[i]['host']
                server.local_ip = grains[i]['id']
                server.OS = grains[i]['os'] + ' ' + grains[i]['osrelease'] + '-' + grains[i]['osarch']
                server.Mem = grains[i]['mem_total']
                server.Cpus = grains[i]['num_cpus']
                server.Cpu_type = grains[i]['cpu_model']
                server.minion_id = grains[i]['id']
                server.app = grains[i]['virtual']
                server.server_status = minions_status
                server.save()
                contexts.update({'success': u'%s 收集成功' % tgt})
        if not grains:
            contexts.update({'error': u'%s 主机不存在或者离线' % tgt})
    except Exception as e:
        contexts.update({'error': '%s %s' % (tgt, e)})
    return contexts

 

4、配置 supervisor 守护进程来启动 celery,启动 worker 与 beat 进程

worker:执行任务的消费者,通常会在多台服务器运行多个消费者来提高执行效率。

beat:任务调度器,Beat 进程会读取配置文件的内容,周期性地将配置中到期需要执行的任务发送给任务队列。 Celerybeat 会保持运行, 一旦到了某一定时任务需要执行时, Celerybeat 便将其加入到 queue 中. 不像 worker 进程, Celerybeat 只有需要一个即可.

[program:DJ.celeryd]
command=/usr/local/python27/bin/python  /data/PRG/saltruler/manage.py celery worker --loglevel=info
user=root
numprocs=1
directory=/data/PRG/saltruler
stdout_logfile=/var/log/celery_worker.log
stderr_logfile=/var/log/celery_worker.log
autostart=true
autorestart=true
startsecs=10
stopwaitsecs = 120
priority=998

[program:DJ.celerybeat]
command=/usr/local/python/bin/python /data/PRG/saltruler/manage.py celery beat --schedule=/tmp/celerybeat-schedule --pidfile=/tmp/django_celerybeat.pid --loglevel=INFO
user=root
numprocs=1
directory=/data/PRG/saltruler
stdout_logfile=/var/log/celery_beat.log
stderr_logfile=/var/log/celery_beat.log
autostart=true
autorestart=true
startsecs=10
stopwaitsecs = 120
priority=998

 

 

 

5、Django 后台配置 celery 定时任务

访问 Django 后台http://192.168.62.200:8000/admin/djcelery/periodictask/

image

 

添加 periodic task

image

查看添加的定时任务

image

 

查看后台日志是否有执行,正常执行日志输入类似如下内容

[2017-04-20 13:30:00,001: INFO/Beat] Scheduler: Sending due task update cmdb (saltstack.tasks.server_collects)
[2017-04-20 13:30:00,006: INFO/MainProcess] Received task: saltstack.tasks.server_collects[f46d2c70-1550-4dec-b5aa-036fbffd2a5f]
[2017-04-20 13:30:00,058: WARNING/Worker-18] https://192.168.62.200:8000
[2017-04-20 13:30:07,163: INFO/MainProcess] Task saltstack.tasks.server_collects[f46d2c70-1550-4dec-b5aa-036fbffd2a5f] succeeded in 7.15584068373s: {'server_id': 1L, 'success': u'* \u6536\u96c6\u6210\u529f', 'server_list': <QuerySet [<SaltServer: games -...
[2017-04-20 13:31:49,945: INFO/Beat] Writing entries (1)...

蜷缩的蜗牛 , 版权所有丨如未注明 , 均为原创丨 转载请注明利用 django admin 后台配置 celery 定时任务
喜欢 (0)
[]
分享 (0)

您必须 登录 才能发表评论!