def test_constructor(self): s = beat.ScheduleEntry(DuePeriodicTask.name) self.assertEqual(s.name, DuePeriodicTask.name) self.assertIsInstance(s.last_run_at, datetime) self.assertEqual(s.total_run_count, 0) now = datetime.now() s = beat.ScheduleEntry(DuePeriodicTask.name, now, 300) self.assertEqual(s.name, DuePeriodicTask.name) self.assertEqual(s.last_run_at, now) self.assertEqual(s.total_run_count, 300)
def test_run_all_due_tasks_after_restart(self): scheduler_class, shelve = create_persistent_scheduler_w_call_logging() shelve['tz'] = 'UTC' shelve['utc_enabled'] = True shelve['__version__'] = __version__ cur_seconds = 20 def now_func(): return datetime(2018, 1, 1, 1, 11, cur_seconds) app_schedule = { 'first_missed': {'schedule': crontab( minute='*/10', nowfun=now_func), 'task': 'first_missed'}, 'second_missed': {'schedule': crontab( minute='*/1', nowfun=now_func), 'task': 'second_missed'}, 'non_missed': {'schedule': crontab( minute='*/13', nowfun=now_func), 'task': 'non_missed'} } shelve['entries'] = { 'first_missed': beat.ScheduleEntry( 'first_missed', 'first_missed', last_run_at=now_func() - timedelta(minutes=2), total_run_count=10, schedule=app_schedule['first_missed']['schedule']), 'second_missed': beat.ScheduleEntry( 'second_missed', 'second_missed', last_run_at=now_func() - timedelta(minutes=2), total_run_count=10, schedule=app_schedule['second_missed']['schedule']), 'non_missed': beat.ScheduleEntry( 'non_missed', 'non_missed', last_run_at=now_func() - timedelta(minutes=2), total_run_count=10, schedule=app_schedule['non_missed']['schedule']), } self.app.conf.beat_schedule = app_schedule scheduler = scheduler_class(self.app) max_iter_number = 5 for i in range(max_iter_number): delay = scheduler.tick() if delay > 0: break assert {'first_missed', 'second_missed'} == { item['task'] for item in scheduler.sent} # ensure next call on the beginning of next min assert abs(60 - cur_seconds - delay) < 1
def setup_schedule(self): """ This loads enabled schedules from the database and adds them to the "_schedule" dictionary as instances of celery.beat.ScheduleEntry """ if not Scheduler._mongo_initialized: _logger.debug('Initializing Mongo client connection to read celerybeat schedule') db_connection.initialize() Scheduler._mongo_initialized = True _logger.debug(_('loading schedules from app')) self._schedule = {} for key, value in self.app.conf.CELERYBEAT_SCHEDULE.iteritems(): self._schedule[key] = beat.ScheduleEntry(**dict(value, name=key)) # include a "0" as the default in case there are no schedules to load update_timestamps = [0] _logger.debug(_('loading schedules from DB')) ignored_db_count = 0 self._loaded_from_db_count = 0 for call in itertools.imap(ScheduledCall.from_db, utils.get_enabled()): if call.remaining_runs == 0: _logger.debug(_('ignoring schedule with 0 remaining runs: %(id)s') % {'id': call.id}) ignored_db_count += 1 else: self._schedule[call.id] = call.as_schedule_entry() update_timestamps.append(call.last_updated) self._loaded_from_db_count += 1 _logger.debug('loaded %(count)d schedules' % {'count': self._loaded_from_db_count}) self._most_recent_timestamp = max(update_timestamps)
def create_schedule_entry(self, schedule): entry = dict( name='celery.unittest.add', schedule=schedule, app=self.app, ) return beat.ScheduleEntry(**dict(entry))
def create_schedule_entry(self, schedule): entry = { 'name': 'celery.unittest.add', 'schedule': schedule, 'app': self.app, } return beat.ScheduleEntry(**dict(entry))
def create_schedule_entry(self, schedule=None, args=(), kwargs={}, options={}, task=None): entry = { 'name': 'celery.unittest.add', 'schedule': schedule, 'app': self.app, 'args': args, 'kwargs': kwargs, 'options': options, 'task': task } return beat.ScheduleEntry(**dict(entry))
def test_next(self): s = beat.ScheduleEntry(DuePeriodicTask.name, None, 300) n = s.next() self.assertEqual(n.name, s.name) self.assertEqual(n.total_run_count, 301) self.assertGreater(n.last_run_at, s.last_run_at)
def test_cleanup(self): self.scheduler.schedule["fbz"] = beat.ScheduleEntry("fbz") self.scheduler.cleanup() self.assertNotIn("fbz", self.scheduler.schedule)
def test_is_due(self): due = beat.ScheduleEntry(DuePeriodicTask.name) pending = beat.ScheduleEntry(PendingPeriodicTask.name) self.assertTrue(due.is_due(DuePeriodicTask())[0]) self.assertFalse(pending.is_due(PendingPeriodicTask())[0])