1 #!/usr/bin/env python3 2 import requests 3 import sys 4 import json 5 #第一行代码为了在cmd中调用时,不用写“python”关键字,lunix下需要配合命令“chmod a+x fileName.py”,windows下需要配合setup文件 6 7 8 class ControlJob(): 9 def __init__(self,end_url): 10 self.base_url = "http://192.168.1.144:8084/job/" 11 self.url = self.base_url + end_url + "Job" 12 if end_url == "get": 13 self.url = self.url + "s" 14 def add_reschedule(self,className,groupName,cron): 15 body = { 16 "jobClassName":className, 17 "jobGroupName":groupName, 18 "cronExpression":cron 19 } 20 response = requests.post(url = self.url,data = body) 21 print(response.status_code) 22 print(response.text) 23 def pause_resume_del(self,className,groupName): 24 body = { 25 "jobClassName": className, 26 "jobGroupName": groupName 27 } 28 response = requests.post(url = self.url, data = body) 29 print(response.status_code) 30 print(response.text) 31 def print_symbol(self): 32 print("+".ljust(52, "-"), end="") 33 print("+".ljust(12, "-"), end="") 34 print("+".ljust(26, "-") + "+") 35 def get(self): 36 response = requests.get(url = self.url) 37 text = json.loads(response.text) 38 data = list(text["data"]) 39 self.print_symbol() 40 print("| jobName".ljust(52," "),end="") 41 print("| jobGroup".ljust(12," "),end="") 42 print("| cronExpression".ljust(25," "),"|") 43 self.print_symbol() 44 for each_data in data: 45 print(("| " + each_data["jobName"]).ljust(52," "),end="") 46 print(("| " + each_data["jobGroup"]).ljust(12," "),end="") 47 print(("| " + each_data["cronExpression"]).ljust(25," "),"|") 48 self.print_symbol() 49 50 if __name__ == '__main__': 51 if sys.argv[1] in ("add","reschedule"): #sys.argv获取命令行下输入的参数 52 if len(sys.argv) != 5: 53 print(sys.argv[1],"job need three argvs") 54 # 比如add方法,cmd输入格式如下 :./ControlJob.py add "com.jiawa.scheduler.job.test.TestJob" "test" "0/10 * * * * ?" 55 else: 56 job = ControlJob(sys.argv[1]) 57 job.add_reschedule(sys.argv[2], sys.argv[3], sys.argv[4]) 58 elif sys.argv[1] in ("pause","resume","delete"): 59 if len(sys.argv) != 4: 60 print(sys.argv[1],"job need two argvs") 61 else: 62 job = ControlJob(sys.argv[1]) 63 job.pause_resume_del(sys.argv[2], sys.argv[3]) 64 elif sys.argv[1] == "get": 65 job = ControlJob(sys.argv[1]) 66 job.get() 67