def __init__(self): self.interval = _server_conf[_eviron]['INTERVAL'] self.starthour = _server_conf[_eviron]['START_HOUR'] self.host = _server_conf[_eviron]['HOST'] self.port = _server_conf[_eviron]['PORT'] self.jobmanager = JobManager(self) self.lock_spawn = threading.Lock() self.next_batch_start_time = None self.socketserver = SocketServer(self.host, self.port, self.jobmanager)
class Server(object): ''' Daemon serves as schedule engine ''' clocks = [] def __init__(self): self.interval = _server_conf[_eviron]['INTERVAL'] self.starthour = _server_conf[_eviron]['START_HOUR'] self.host = _server_conf[_eviron]['HOST'] self.port = _server_conf[_eviron]['PORT'] self.jobmanager = JobManager(self) self.lock_spawn = threading.Lock() self.next_batch_start_time = None self.socketserver = SocketServer(self.host, self.port, self.jobmanager) def _fillClock(self, starthour, interval): ''' Fill clock triggers Server use clocks as triggers to schedule next batch of tasks. This clock values should be stable every day ,so interval causes variable clocks is not accepted. @param starthour:Integer as start hour @param interval:Integer as interval hours to generate clock triggers ''' self.clocks = [] if (24 % interval != 0): raise ValueError("Invalid Interval %d" % (interval)) else: for i in range(0, 24, interval): self.clocks.append((i + starthour) % 24) self.clocks.sort() def _spawnScheduleThread(self, next_batch_start_time): ''' Spawn schedule thread after one batch of task threads have been spawned Execute Time of the schedule thread is subtraction of next_batch_start_time and AHEAD_MINUTES of configuration.If execute time is gone,the thread will be started at once. @param next_batch_start_time: datetime,start time of next batch ''' next_schedule_time = next_batch_start_time - timedelta(minutes=_server_conf[_eviron]['AHEAD_MINUTES']) delay = 0 if next_schedule_time <= datetime.now() else (next_schedule_time - datetime.now()).seconds + 1 next_batch_end_time = next_batch_start_time + timedelta(hours=self.interval) schedule_thread = threading.Timer(delay, self._spawnTaskThreads, args=[next_batch_start_time, next_batch_end_time ], kwargs={}) schedule_thread.daemon = True schedule_thread.start() logger.info("Schedule Thread for %s to %s has been created." % (next_batch_start_time.strftime("%Y-%m-%d %H:%M:%S"), next_batch_end_time.strftime("%Y-%m-%d %H:%M:%S"))) def spawnTaskThread(self, jobid): ''' Spawn task threads of one job @param jobid:int job id to spawn ''' self.lock_spawn.acquire() try: now = datetime.now() self.jobmanager.spawnTask(jobid, now, self.next_batch_start_time) self.lock_spawn.release() except Exception, e: self.lock_spawn.release() raise e; logger.info("TaskThreads of Job(id:%d) have been spawned from %s to %s." % (jobid, now.strftime("%Y-%m-%d %H:%M:%S"), self.next_batch_start_time.strftime("%Y-%m-%d %H:%M:%S")))