This repository has been archived by the owner on Aug 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathscheduler.py
101 lines (94 loc) · 3.65 KB
/
scheduler.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
# -*- coding: utf-8 -*-
import os
import sys
import logging
import importlib
import random
import time
import main
import json
import datetime
import pytz
import threading
from lxml.html import fromstring
#获取任务列表
def listdir(path, list_name):
for file in os.listdir(path):
if os.path.isdir(file):
listdir(file, list_name)
elif os.path.splitext(file)[1]=='.py':
list_name.append(os.path.splitext(file)[0])
#list_name.append(file_path)
def runscheduler(client,username,num):
# 检索任务数量
tasklist=[]
listdir(resource_path('TaskList'),tasklist)
#整理数组
tasklist.sort()
logging.info('【任务调度】: 当前任务数量' + str(len(tasklist)))
user = main.readJson()
if ('taskNum' in user[0]):
taskNum = user[0]['taskNum']
else:
logging.error('Json未配置taskNum,停止运行')
sys.exit()
#日期不同重置任务
if configdate() != str(datetime.datetime.now().month)+str(datetime.datetime.now().day):
resetJson('./','./',0)
logging.info('【任务调度】: 日期变更重置任务')
logging.info('【任务统计】: 已进行' + taskNum +'个任务')
forNum = 0
for task in tasklist:
if forNum >= int(taskNum):
logging.info('【任务分配】: ' + task)
i = importlib.import_module('TaskList.'+task)
#启动线程处理
thread = threading.Thread(target = i.main(client,username,num))
#i.main(client,username,num)
thread.start()
time.sleep(1)
thread.join()
resetJson('./','./',int(taskNum) + 1)
forNum = forNum + 1
if forNum < len(tasklist):
dtime =random.randint(3,10)
logging.info('【任务调度】: 延时进行' + str(dtime)+'秒')
time.sleep(dtime)
else:
print('【任务结束】: 正在尝试发送通知')
else:
logging.info('【任务跳过】: ' + task)
forNum = forNum + 1
#定义一个读取相对路径的函数
def resource_path(relative_path):
if hasattr(sys, "_MEIPASS"):
base_path = sys._MEIPASS
else:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
#修改json函数
def resetJson(file_old,file_new,num):
filename_rest = os.listdir(file_old) # 获取需要读取的文件的名字
L = []
for rest in filename_rest:
if os.path.splitext(rest)[1]=='.json':
L.append(os.path.join(file_old,rest)) #创建文件路径
for f11 in L:
with open(f11,'r') as f:
data = json.load(f)
data[0]['taskNum'] = str(num)
newpath = os.path.join(file_new,os.path.split(f11)[1])
with open(newpath,'w') as f2:
json.dump(data,f2) # 写入f2文件到本地
#获取config.json修改日期
def configdate():
file_name = './config.json'
file_times_modified = time.localtime(os.path.getmtime(file_name))
year_modified = file_times_modified.tm_year
month_modified = file_times_modified.tm_mon
day_modified = file_times_modified.tm_mday
hour_modified = file_times_modified.tm_hour
minute_modified = file_times_modified.tm_min
second_modified = file_times_modified.tm_sec
#print('文件的内容最近修改的时间(mtime): ', year_modified, '年', month_modified, '月', day_modified, '日', ' ', hour_modified, '时',minute_modified, '分', second_modified, '秒')
return str(month_modified) + str(day_modified)