-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.py
135 lines (110 loc) · 3.63 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import datetime
from flask import Flask, request, jsonify
import fileControl.fileCont as fileControl
app = Flask(__name__)
# 响应数组
errorCode = [
"operate failed", "operate successful", "An event is about to expire.",
"Cannot add job to scheduler", "Cannot del job to scheduler", "both cannot add and del job to scheduler"
]
def add_sched(id_job, end_time):
try:
# 添加任务调度
pos_time = (datetime.datetime.strptime(end_time, "%Y-%m-%d %H:%M:%S") -
datetime.timedelta(hours=1)).strftime("%Y-%m-%d %H:%M:%S")
# job = fileControl.fileControl().writeNote(id_job)
scheduler.add_job(job, 'date', run_date=pos_time,
args=id_job, id=id_job)
except:
return "3"
def del_sched(id_job):
# 取消任务调度
try:
scheduler.remove_job(id_job)
except:
return "4"
def update_sched(id_job, end_time):
# 更新任务调度
try:
del_sched(id_job)
add_sched(id_job, end_time)
except:
return "5"
@app.route('/')
def hello_world():
return 'hello world!'
@app.route('/add', methods=['GET', 'POST'])
def get_add():
if request.method == 'POST':
try:
id_user = str(request.json.get('id_user'))
id_job = str(request.json.get('id_job'))
id_class = str(request.json.get('id_class'))
end_time = str(request.json.get('end_time'))
job_content = str(request.json.get('job_content'))
fileControl.fileControl().writeJob(
id_user, id_job, id_class, end_time, job_content
)
add_sched(id_job, end_time)
return 'From POST'
except:
return 'ERROR'
else:
return '请使用POST请求'
@ app.route('/del', methods=['GET', 'POST'])
def get_del():
if request.method == 'POST':
try:
id_job = str(request.json.get('id_job'))
fileControl.fileControl().delJobbyID(id_job)
del_sched(id_job)
return 'From POST'
except:
return 'ERROR'
else:
return '请使用POST请求'
@ app.route('/query', methods=['GET', 'POST'])
def get_query():
if request.method == 'POST':
try:
id_job = str(request.json.get('id_job'))
ans = fileControl.fileControl().readJobbyID(id_job)
# 查询任务调度情况
jobs = scheduler.get_jobs()
print(jobs)
if ans == "0":
return jsonify({
"error": "Not found anything"
})
return jsonify({
'id_user': ans[0],
'id_job': ans[1],
'id_class': ans[2],
'start_time': ans[3],
'end_time': ans[4],
'job_content': ans[5],
})
except:
return 'ERROR'
else:
return '请使用POST请求'
@ app.route('/modify', methods=['GET', 'POST'])
def get_modify():
if request.method == 'POST':
try:
id_job = str(request.json.get('id_job'))
end_time = str(request.json.get('end_time'))
job_content = str(request.json.get('job_content'))
update_sched(id_job, end_time)
fileControl.fileControl().modifybyID(id_job, end_time, job_content)
return 'From POST'
except:
return 'ERROR'
else:
return '请使用POST请求'
if __name__ == '__main__':
# 启动任务调度器
scheduler = BackgroundScheduler()
scheduler.start()
# 启动Flask服务
app.run(debug=False, host='127.0.0.1', port=5000)