pushott.py 2.93 KB
#!/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))