def indexJob(self, job): ''' Index job by hour recording to its time pattern @param job:Job ''' for clock in range(24): if TimeMatcher.matchOneUnit(job.get_time_pattern().HOUR, clock): if clock not in self.hour_index: self.hour_index[clock] = [] self.hour_index[clock].append(job)
def indexJobs(self): ''' Create hour index for every job loaded. To increment efficiency, we select hour(total 24 keys) as a key to index jobs ''' for clock in range(24): for job in self._depository.itervalues(): if TimeMatcher.matchOneUnit(job.get_time_pattern().HOUR, clock): if clock not in self.hour_index: self.hour_index[clock] = [] self.hour_index[clock].append(job)
def spawnTasks(self, start_time, end_time): ''' Spawn thread based on Job's timer. According to start_time and end_time,Job satisfied the job will be wrapped as Task which launched by Timer thread.For Timer accept delay as its first parameter,we convert job's timer to delay seconds here. These Timer tasks will be started in main thread. ''' tasks = [] jobs = [] self._lock.acquire() try: for hour in range(start_time.hour, end_time.hour): if hour in self.hour_index: jobs.extend(self.hour_index[hour]) jobs = set(jobs) # Clear second and be care of start minute determine_time = start_time if start_time.second != 0: determine_time = start_time + timedelta(minutes=1) determine_time = datetime.combine(determine_time.date(), time(hour=determine_time.hour, minute=determine_time.minute)) while determine_time < end_time: for job in jobs: if TimeMatcher.matchTimePattern(job.get_time_pattern(), determine_time): task = Task(job.get_jobid(), uuid.uuid1().hex, determine_time, job.getCommandToExcute(), job.get_retry(), job, job.get_depend()) t = TaskRunner((determine_time - datetime.now()).seconds + 1, task, self._status, self) t.daemon = True t.start() task.save() tasks.append(task) self._status.add_waiting_task(task, t) determine_time = determine_time + timedelta(minutes=1) self._lock.release() except Exception, e: self._lock.release() raise e;
def spawnTask(self, jobid, start_time, end_time): ''' Spawn thread based on one Job's timer @param jobid:int @param start_time:datetime ,start time of time section @param end_time:datetime ,end time of time section ''' tasks = [] self._lock.acquire() try: job = Job.getByJobID(jobid) if not job: raise ValueError("Job(id:%d) is not exsited or not enabled." % (jobid)) # Clear second and be care of start minute determine_time = start_time if start_time.second != 0: determine_time = start_time + timedelta(minutes=1) determine_time = datetime.combine(determine_time.date(), time(hour=determine_time.hour, minute=determine_time.minute)) while determine_time < end_time: if TimeMatcher.matchTimePattern(job.get_time_pattern(), determine_time): task = Task(job.get_jobid(), uuid.uuid1().hex, determine_time, job.getCommandToExcute(), job.get_retry(), job, job.get_depend()) t = TaskRunner((determine_time - datetime.now()).seconds + 1, task, self._status, self) t.daemon = True t.start() task.save() tasks.append(task) self._status.add_waiting_task(task, t) determine_time = determine_time + timedelta(minutes=1) self._lock.release() except Exception, e: self._lock.release() raise e;