def scheduler_loop(self, interval=60): now = datetime.now() while not self.stopped(): event_schedules = get_relevant_schedules(now) for schedule in event_schedules: try: # call the callback function # possibly passing in args and kwargs schedule.run(self._router) except Exception, e: # Don't prevent exceptions from killing the thread exc_type, exc_value, exc_traceback = sys.exc_info() stacktrace = traceback.extract_tb(exc_traceback) self._router.error("Problem in scheduler for: %s. %s\n\nTRACEBACK: %s" % (schedule, e.message, stacktrace)) if self._speedup is not None: # debugging/testing only! now = now + self._speedup time.sleep(1) else: next_run = now + timedelta(seconds=interval) updated_now = datetime.now() while updated_now < next_run: # we add another second since system time runs at microsecond accuracy # whereas python time is only second accuracy time.sleep((next_run - updated_now + timedelta(seconds=1)).seconds) updated_now = datetime.now() now = next_run
def scheduler_heartbeat(): """ Check all scheduled tasks and run whatever is necessary. """ # this should get called every minute by celery failures = [] currtime = datetime.utcnow() relevant = get_relevant_schedules(currtime) for schedule in relevant: try: # call the callback function # possibly passing in args and kwargs schedule.run(currtime) except Exception, e: # Don't prevent exceptions from killing the rest logging.exception("Problem in the scheduler heartbeat for %s" % schedule) failures.append(e)