失败job重启

#coding=utf-8
import json
import time
import os
import sys
import datetime

import time
from configobj import ConfigObj
config = ConfigObj("./conf.ini",encoding='UTF8')
host=config['host']['host']
port=config['port']['port']
kylin_home=config['path']['kylin_home']
job_time=config['sleep']['job_time']
request_time=config['sleep']['resquest_time']
user=config['user']['user']
passwd=config['password']['pass']
count=config['count']['count']
AT=config['Naenvvar']['AUTO']
enable=config['enable']['enable']
reload(sys)
sys.setdefaultencoding('utf-8')

shellres="curl -u  "+ user +":" + passwd + " -X GET http://"+ host +":"+ port + "/kylin/api/jobs" + " >> response.json"

os.popen(shellres)

print ("正在获取失败jobid")
time.sleep(int(request_time))
file_path=kylin_home + "response.json"
f=open(file_path,'r')
text=f.read()
items=[]
data=json.loads(text)
for itema in data:
    items.append(itema)

list_err=[]
list=[]
def decide_code():
    global list
    if ("code" in items and int(data["code"]) == 000):
        list = data["data"]["jobs"]
        append_error()
    elif ("code" in items and int(data["code"]) != 000):
        print("返回错误的json,请检查请求RestApi")
    else:
        list = data
        append_error()

def append_error():
    for item in list:
        # timestamp=item["last_modified"]
        # timestamp /=1000.0
        # date=time.localtime(timestamp)
        # new_date=time.strftime("%Y-%m-%d",date)
        # d1=datetime.datetime.strptime(str(new_date),'%Y-%m-%d')
        # d2=datetime.datetime.strptime(str(current_time),'%Y-%m-%d')
        # delta=d2-d1
        # days=delta.days
        if(item["job_status"]=="FINISHED" ):
            list_err.append(item["uuid"])

def draw():
    path = os.getenv(AT)
    if(enable=="true"):
        for i in range(0,int(count)):
            print(list_err)
            if (path == "true" ):
                for jobid in list_err:
                    print("=========================开始恢复"+ jobid +"==========================")
                    draw_shell = "curl -u  " + user + ":" + passwd + " -X PUT http://" + host + ":" + port + "/kylin/api/jobs/" + jobid + "/resume"
                    os.popen(draw_shell)
                    time.sleep(int(job_time))
                    print(jobid + "已恢复成功")
            print("\n")
        else:
            print("系统环境变量设置错误,请到/etc/profile文件下设置")
    else:
        for i in range(0,int(count)):
            print(list_err)
            for jobid in list_err:
                print("=========================开始恢复"+ jobid +"==========================")
                draw_shell = "curl -u  " + user + ":" + passwd + " -X PUT http://" + host + ":" + port + "/kylin/api/jobs/" + jobid + "/resume"
                os.popen(draw_shell)
                time.sleep(int(job_time))
                print(jobid + "已恢复成功")
            print("\n")



def decide_draw():
    decide_code()
    if(list_err.__len__() == 0 ):
        print("没有失败的job")
    else:
        print("已获取到" + str(list_err.__len__())+ "个失败的jobid")
        print("开始恢复失败的job")
        draw()


if __name__ == '__main__':
    os.remove(file_path)
    decide_draw()
    os.remove(file_path)



推荐阅读更多精彩内容