def action(self, data): """实时任务执行后的回调函数""" try: result = { 's' :data[2], 'o' :'%s%s' % (data[0], data[1]), 'h' :getHostname(), } except Exception, e: result = { 's' :256, 'o' :e.message, 'h' :getHostname(), }
def refreshJobs(self): """ purpose:计划任务随配置的变化而变化 """ self.host = getHostname() cron_jobs, status = parseCrontab(self._cronfile) if status: running_jobs = self._ap_sched.get_jobs() new_jobs = set(cron_jobs.keys()) - set(running_jobs.keys()) old_jobs = set(running_jobs.keys()) - set(cron_jobs.keys()) samename_jobs = set(cron_jobs.keys()) & set(running_jobs.keys()) for key in old_jobs: self._ap_sched.unschedule_job(running_jobs[key]) for key in new_jobs: self._ap_sched.add_job(key, cron_jobs[key]['name'], cron_jobs[key]['trigger'], self.execCmd, args=cron_jobs[key]['args'], kwargs=None) for key in samename_jobs: trigger = None args = None new_name = None if str(cron_jobs[key]['trigger']) != str( running_jobs[key].trigger): trigger = cron_jobs[key]['trigger'] if cron_jobs[key]['args'] != running_jobs[key].args: args = cron_jobs[key]['args'] if cron_jobs[key]['name'] != running_jobs[key].name: new_name = cron_jobs[key]['name'] if trigger or args or new_name: self._ap_sched.update_job(running_jobs[key], trigger, args, new_name) log.info('job[%s] changed' % key)
def refreshJobs(self): """ purpose:计划任务随配置的变化而变化 """ self.host = getHostname() cron_jobs, status = parseCrontab(self._cronfile) if status: running_jobs = self._ap_sched.get_jobs() new_jobs = set(cron_jobs.keys()) - set(running_jobs.keys()) old_jobs = set(running_jobs.keys()) - set(cron_jobs.keys()) samename_jobs = set(cron_jobs.keys()) & set(running_jobs.keys()) for key in old_jobs: self._ap_sched.unschedule_job(running_jobs[key]) for key in new_jobs: self._ap_sched.add_job(key, cron_jobs[key]['name'], cron_jobs[key]['trigger'], self.execCmd, args=cron_jobs[key]['args'], kwargs=None) for key in samename_jobs: trigger = None args = None new_name = None if str(cron_jobs[key]['trigger']) != str(running_jobs[key].trigger): trigger = cron_jobs[key]['trigger'] if cron_jobs[key]['args'] != running_jobs[key].args: args = cron_jobs[key]['args'] if cron_jobs[key]['name'] != running_jobs[key].name: new_name = cron_jobs[key]['name'] if trigger or args or new_name: self._ap_sched.update_job(running_jobs[key], trigger, args, new_name) log.info('job[%s] changed' % key)
def errorAction(self, reason): """defer的异常处理函数""" result = { 's' :256, 'o' :reason.value.message, 'h' :getHostname(), } self.connectionEnd(result)
def doTask(task): '''function:在线程中执行某一任务 @params task:某一任务的相关信息,一个字典 @return:任务执行成功返回执行结果 ''' now = int(time()) status,output = getstatusoutput(task['task_content']) return json.dumps({'h':getHostname(), 'tk':task['task_name'], 's':status, 'o':output.strip(), 'tm':now, 'tp':2})
def doTask(task): '''function:在线程中执行某一任务 @params task:某一任务的相关信息,一个字典 @return:任务执行成功返回执行结果 ''' now = int(time()) status, output = getstatusoutput(task['task_content']) return json.dumps({ 'h': getHostname(), 'tk': task['task_name'], 's': status, 'o': output.strip(), 'tm': now, 'tp': 2 })
from twisted.application.service import Service from twisted.internet.protocol import ReconnectingClientFactory from twisted.internet.defer import Deferred from twisted.protocols.basic import NetstringReceiver from twisted.internet import task from collections import deque #standard Library import from random import randint from time import sleep, time from conf import DEFAULT from util.logger import log from util import getHostname hostname = getHostname() class ApolloSenderProtocol(NetstringReceiver): """这个协议负责发送数据到相对应的服务器端 """ heartbeat_interval = DEFAULT['heart_interval'] _heartbeat = None def connectionMade(self): """当连接建立后启动心跳机制 """ self._peer = self.transport.getPeer() log.info('connect to %s successfully' % self._peer) self.factory._paused = False self._connected = True self._heartsendtime = 0 self._heartreceivetime = 0 self.factory.resetDelay() self.startHeartbeat()
def __init__(self, cronfile): if not os.path.exists(cronfile): sys.exit(111) self._cronfile = cronfile self.host = getHostname() self.my_init()
def parseConf(): '''解析任务和主机列表配置文件 ''' host_cfg = ConfigParser() task_cfg = ConfigParser() try: host_cfg.read(DEFAULT['host_file_config']) task_cfg.read(DEFAULT['task_file_config']) except ParsingError, e: log.error(str(e)) return global task_list, host_list host = getHostname() for hg in host_cfg.sections(): for hl in host_cfg.items(hg): host_list[hl[0]] = [x.strip() for x in hl[1].split(',')] tk_sections = task_cfg.sections() all_task_list = [dict(task_cfg.items(sec)) for sec in tk_sections] task_list = [] for task in all_task_list: now = int(time()) hosts = task.get('hostlist') if hosts is None or host_list.get( hosts) is None or host not in host_list[hosts]: continue task_interval = task.get('task_interval') if task_interval is not None:
return 0 def parseConf(): '''解析任务和主机列表配置文件 ''' host_cfg = ConfigParser() task_cfg = ConfigParser() try: host_cfg.read(DEFAULT['host_file_config']) task_cfg.read(DEFAULT['task_file_config']) except ParsingError, e: log.error(str(e)) return global task_list, host_list host = getHostname() for hg in host_cfg.sections(): for hl in host_cfg.items(hg): host_list[hl[0]] = [x.strip() for x in hl[1].split(',')] tk_sections = task_cfg.sections() all_task_list = [dict(task_cfg.items(sec)) for sec in tk_sections] task_list = [] for task in all_task_list: now = int(time()) hosts = task.get('hostlist') if hosts is None or host_list.get(hosts) is None or host not in host_list[hosts]: continue task_interval = task.get('task_interval') if task_interval is not None: task['task_interval'] = strToInt(task_interval)