def update_task(self, id, name, status, command, cron, phonenum, email, errerkey): if id: update_status = False oldtask = Job.get(Job.id == id) if oldtask.cron != cron: update_status = True try: Job.update(name=name, status=status, command=command, cron=cron, phonenum=phonenum, email=email, errerkey=errerkey).where(Job.id == id).execute() return self.updateTaskRun(id, update_status) except Exception, e: print e
def addTaskRun(self, name, status, command, cron, phonenum, email, errerkey): try: s = Job.select().where(Job.name == name, Job.status == status, Job.command == command, Job.cron == cron, Job.phonenum == phonenum, Job.email == email, Job.errerkey == errerkey) addJob(s[0]) except Exception, e: print e
def addTask(self, name, status, command, cron, phonenum='', email='', errerkey=''): try: Job.insert(name=name, status=status, command=command, cron=cron, phonenum=phonenum, email=email, errerkey=errerkey).execute() except Exception, e: print e
def reStart(): """ 重启整个scheduler :return: """ # 重置scheduler if scheduler_runing: scheduler.shutdown(wait=False) # 重新添加各种job dbjobs = Job.select().where(Job.status == 1) for dbjob in dbjobs: addJob(dbjob) scheduler.start()
def run(item, cmd): stdout = u'' stderr = u'' begin = CommonUtils.get_unixtime() result = 1 with controltower_database.execution_context() as ctx: # 修改任务状态为 开始运行 try: Job.update(lastbegin=begin, lastend=0, lastresult=3).where(Job.id == str(item.id)).execute() except Exception, e: print e try: reload(sys) sys.setdefaultencoding('utf-8') child = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = child.communicate() except Exception, e: print e stderr += traceback.format_exc()
def taskList(self): data = Job.select() for d in data: s = d.status r = d.lastresult begin = int(d.lastbegin) end = int(d.lastend) if int(s): d.status = '开启' else: d.status = '关闭' if r == '1': d.lastresult = '成功' if r == '2': d.lastresult = '失败' if r == '3': d.lastresult = '运行中' if begin: x = time.localtime(begin) d.lastbegin = time.strftime('%Y-%m-%d %H:%M:%S', x) if end: x = time.localtime(end) d.lastend = time.strftime('%Y-%m-%d %H:%M:%S', x) return data
def del_task(self, id): if id: Job.delete().where(Job.id == id).execute() self.delTaskRun(id)
def updateTaskRun(self, id, update_status): s = Job.select().where(Job.id == id) return addJob(s[0], reschedule=update_status)
if item.phonenum: url = 'http://123.56.40.122:30003/sms/sendsms' content = "优办ct测试" # content = u"错错错,都是我的错" SendMessage.sendPhoneMessage(url, { "phone": '13351019032', "content": content }, {}) elif item.email: SendMessage().sendEmailMessage( item.email.split(";"), u'报表监控系统异常', u'报表监控系统,%s脚本出现运行异常,请尽快关注' % item.name) end = CommonUtils.get_unixtime() # 修改任务状态为 结束运行 Job.update(lastend=end, lastresult=result, runtime=end - begin).where(Job.id == str(item.id)).execute() # 记录脚本日志 # print(stderr) try: stderr = stderr.decode('gbk').encode('utf-8') stdout = stdout.decode('gbk').encode('utf-8') Log.create(begin=begin, end=end, job=item.id, msg=u'===============Print==========\n' + stdout + u'\n===============Error==========\n\n' + stderr, result=result) except Exception, e: print e