pushott.py
2.93 KB
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
#!/x/app/python3/bin/python3
# -*- coding: utf-8 -*-
import os
import re
import config
import socket
import taskmanage
import logging
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(filename)s %(levelname)s %(message)s',
datefmt='%a, %d %b %Y %H:%M:%S',
filename=config.PATH["LOG_PATH"],
filemode='a+')
class PushTasks:
#初始化属性,导入配置
def __init__(self):
self.__GetUrl=config.URL["GET"]
self.__Post_Push_Url=config.URL["PUSH_POST"]
self.__outPath = config.PATH["SLICE_OUTPUT"]
self.__CDNPath = config.PATH["CDN_SOURCE"]
self.__serverID = config.SERVERID["ID"]
self.__header = {"Content-Type": "application/json-rpc"}
#判断奇偶,判断运行主机
def job_balance(self,num):
int(num)
if (num % 2 == self.__serverID):
return True
else:
return False
#检查输入文件是否存在
def check_file(self,inputfile):
if os.path.exists(inputfile):
return (True,"")
else:
return (False,"丢失切片文件:{0}.".format(inputfile))
#复制迁移
def mv_CDN(self,url):
base=os.path.basename(url)
fileName=os.path.splitext(base)[0]
pattern=re.compile(r'\d+-\d+-\d+')
directory = pattern.findall(url)
pushPath=config.PATH["SLICE_OUTPUT"]+directory[0]+"/"+fileName
CDNPath=config.PATH["CDN_SOURCE"]+directory[0]+"/"+fileName
try:
os.makedirs(CDNPath)
except Exception as e:
logging.debug(str(e))
logging.debug("cp -vrf {0} {1}".format(pushPath,CDNPath))
os.system("cp -vrf {0} {1}".format(pushPath,CDNPath))
#主程序
def main(address):
while 1<2:
Job=PushTasks()
pushJob=taskmanage.TaskManage()
#get方法接任务
try:
ResponseGet=pushJob.get_request(config.URL["GET"],"?start=0&limit=1&status=500&source_cdn_status=0")
except Exception as e:
logging.error(str(e))
continue
resultSet=ResponseGet['resultSet']
if resultSet:
pass
else:
continue
#获取任务ID,待发布文件目录
TaskId=ResponseGet['resultSet'][0]['task_id']
url=ResponseGet['resultSet'][0]['url']
resultm3u8=Job.check_file(config.PATH["SLICE_OUTPUT"]+os.path.splitext(url)[0])
if resultm3u8[0]:
pass
else:
try:
pushJob.change_push_status(config.URL["PUSH_POST"],TaskId,-1,address,resultm3u8[1])
logging.error(resultm3u8[1])
except Exception as e:
logging.error(str(e))
continue
continue
#改任务状态为发布中
try:
pushJob.change_push_status(config.URL["PUSH_POST"],TaskId,10,address,"")
except Exception as e:
logging.error(str(e))
continue
Job.mv_CDN(url)
#改任务状态为发布完成
try:
pushJob.change_push_status(config.URL["PUSH_POST"],TaskId,100,address,"")
except Exception as e:
logging.error(str(e))
continue
#实例化
if __name__ == '__main__':
address=socket.gethostbyname(socket.getfqdn(socket.gethostname()))
try:
main(address)
except Exception as e:
pushJob.change_push_status(config.URL["PUSH_POST"],TaskId,-1,address,str(e))
logging.error(str(e))