Skip to content

Commit f6ee29f

Browse files
authored
Create django使用定时更新任务.md
1 parent 94bffab commit f6ee29f

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed

django使用定时更新任务.md

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# django使用定时更新任务
2+
3+
### step1:安装django-crontab模块
4+
`pip install django-crontab`
5+
注意:django的任务计划有两个模块,一个是django-crontab,另一个是django-cron
6+
我们使用django-crontab模块,这个配置起来非常简单
7+
8+
9+
### step2:在setting.py中添加为APP
10+
```python
11+
INSTALLED_APPS = [
12+
# ...
13+
"django_crontab", #注意:加载的模块名一定要注意
14+
]
15+
```
16+
17+
18+
### step3:自定义命令函数:(重点)
19+
在app项目下创建management目录,再在该目录下创建commands目录,在commands目录创建py文件,该文件为自定义命令
20+
例如:
21+
app目录名/management/commands/mysql_count.py
22+
23+
# coding:utf-8
24+
# 自定义django-admin命令
25+
# python manage.py mysql_count
26+
# 注意:在更新的时候为什么不能用get,只能用filter?
27+
# get获取的数据是一个字段,而filter获取的是一个列表,在更新中,应该是针对列表中的字段进行更新
28+
# 所以只能用filter
29+
```python
30+
__author__ = 'eycode'
31+
__date__ = '2017/4/11/011 11:33'
32+
33+
from django.core.management.base import BaseCommand
34+
from vpnmanage.models import VpnManage, VpnLogs
35+
36+
37+
class Command(BaseCommand): #继承BaseCommand类
38+
def handle(self, *args, **options): # 一定是handle函数
39+
vpnlist = VpnManage.objects.all()
40+
vpnlist = [ str(i).split(" ", 3)[1] for i in vpnlist ]
41+
42+
for user in vpnlist:
43+
chk_log = VpnLogs.objects.filter(user = user)
44+
if len(chk_log) != 0:
45+
# 统计登录成功次数
46+
success = VpnLogs.objects.filter(user=user, status = "Successful")
47+
user_success = success.count()
48+
49+
# 统计登录失败次数
50+
user_fail_list = VpnLogs.objects.filter(user=user, status="Incorrect")
51+
user_fail = user_fail_list.count()
52+
53+
# # 统计登录总次数
54+
user_counnt = int(user_success) + int(user_fail)
55+
56+
# 统计最近登录时间
57+
user_latetime = VpnLogs.objects.filter(user = user, status = "Successful").order_by('-time')[0]
58+
user_latetime = str(user_latetime)
59+
60+
VpnManage.objects.filter(user=user).update(scount=user_success, fcount=user_fail, logincount=user_counnt, latetime=user_latetime)
61+
# print "%s 更新" %user
62+
else:
63+
VpnManage.objects.filter(user=user).update(scount="0", fcount="0", logincount="0", latetime="no login")
64+
65+
```
66+
67+
68+
### step4:测试命令的执行是否正常
69+
python manage.py mysql_count
70+
注意:mysql_count为文件名。
71+
72+
73+
74+
### step5:添加计划任务:
75+
在setting.py文件中:
76+
```python
77+
CRONJOBS = [
78+
('*/5 * * * *', 'django.core.management.call_command', ['mysql_count']),
79+
]
80+
```
81+
82+
解析:
83+
*/5 * * * * :每5分钟执行一次
84+
django.core.management.call_command:相当于python manage.py
85+
mysql_count:自定义命令函数
86+
87+
88+
89+
### step6:执行任务计划
90+
```bash
91+
[root@9683705d680e operation]# python manage.py crontab add
92+
no crontab for root
93+
adding cronjob: (1eb1cfc7b7f566494684323c57922190) -> ('*/5 * * * *', 'django.core.management.call_command', ['mysql_count'])
94+
[root@9683705d680e operation]#
95+
[root@9683705d680e operation]# crontab -l
96+
*/5 * * * * /usr/bin/python /Operationsmanage/operation/manage.py crontab run 1eb1cfc7b7f566494684323c57922190 # django-cronjobs for operation
97+
[root@9683705d680e operation]#
98+
```
99+
100+
101+
注意:需要服务器上有安装crond服务才能使用
102+
```bash
103+
yum -y install vixie-cron
104+
yum -y install crontabs
105+
```

0 commit comments

Comments
 (0)