def run(self): """ Runs the main loop of the scheduler. """ self.wakeup.clear() while not self.stopped: # Execute any jobs scheduled to be run right now for job in self._get_current_jobs(): logger.debug('Executing job "%s"', job) job.run() # Figure out when the next job should be run, and # adjust the wait time accordingly now = datetime.now() next_wakeup_time = self._get_next_wakeup_time(now) # Sleep until the next job is scheduled to be run, # or a new job is added, or the scheduler is stopped if next_wakeup_time is not None: wait_seconds = time_difference(next_wakeup_time, now) logger.debug('Next wakeup is due at %s (in %f seconds)', next_wakeup_time, wait_seconds) self.wakeup.wait(wait_seconds) else: logger.debug('No jobs; waiting until a job is added') self.wakeup.wait() self.wakeup.clear()
def _main_loop(self): print "get into the main loop" self._wakeup.clear() while not self._stopped: print 'check again' now = self.now() next_wakeup_time = self._process_jobs(now) print "next_wakeup_time:", next_wakeup_time if next_wakeup_time is not None: wait_seconds = time_difference(next_wakeup_time, now) self._wakeup.wait(wait_seconds) self._wakeup.clear() else: self._wakeup.wait() self._wakeup.clear() print "get out the main loop"
def _get_current_jobs(self): """ Determines which jobs should be executed right now. """ current_jobs = [] now = datetime.now() start = now - timedelta(seconds=self.misfire_grace_time) self.jobs_lock.acquire() try: for job in self.jobs: next_run = job.trigger.get_next_fire_time(start) if next_run: time_diff = time_difference(now, next_run) if next_run < now and time_diff <= self.misfire_grace_time: current_jobs.append(job) finally: self.jobs_lock.release() return current_jobs
def _main_loop(self): """Executes jobs on schedule.""" logger.info('Scheduler started') self._notify_listeners(SchedulerEvent(EVENT_SCHEDULER_START)) self._wakeup.clear() while not self._stopped: logger.debug('Looking for jobs to run') now = timezone.now() next_wakeup_time = self._process_jobs(now) # Sleep until the next job is scheduled to be run, # a new job is added or the scheduler is stopped if next_wakeup_time is not None: wait_seconds = time_difference(next_wakeup_time, now) logger.debug('Next wakeup is due at %s (in %f seconds)', next_wakeup_time, wait_seconds) try: self._wakeup.wait(wait_seconds) except IOError: # Catch errno 514 on some Linux kernels pass self._wakeup.clear() elif self.standalone: logger.debug('No jobs left; shutting down scheduler') self.shutdown() break else: logger.debug('No jobs; waiting until a job is added') try: self._wakeup.wait() except IOError: # Catch errno 514 on some Linux kernels pass self._wakeup.clear() logger.info('Scheduler has been shut down') self._notify_listeners(SchedulerEvent(EVENT_SCHEDULER_SHUTDOWN))