def enqueue_periodic_commands(self): while True: start = time.time() self.logger.info('Enqueueing periodic commands') try: invoker.enqueue_periodic_commands() except: self.logger.error('periodic command error, exiting', exc_info=1) raise end = time.time() time.sleep(60 - (end - start))
def test_periodic_command_enqueueing(self): on_time = datetime.datetime(2011, 1, 1, 1, 15) # matches */15 off_time = datetime.datetime(2011, 1, 1, 1, 16) # doesn't match */15 both_time = datetime.datetime(2011, 1, 1, 1, 30) # there should be nothing in the queue self.assertEqual(len(invoker.queue), 0) # no commands should be enqueued invoker.enqueue_periodic_commands(off_time) self.assertEqual(len(invoker.queue), 0) # running it at 1:15 will pick up the */15 command invoker.enqueue_periodic_commands(on_time) self.assertEqual(len(invoker.queue), 1) # dequeue and execute, should get a new user named 'fifteen' invoker.dequeue() # verify user created, then delete the user self.assertEqual(User.objects.filter(username="******").count(), 1) User.objects.all().delete() # make sure the queue is empty self.assertEqual(len(invoker.queue), 0) # running it at :30 will pick up both the */15 and the */30 commands invoker.enqueue_periodic_commands(both_time) self.assertEqual(len(invoker.queue), 2) # execute both commands invoker.dequeue() invoker.dequeue() # check that the users were created self.assertEqual(User.objects.all().count(), 2) self.assertEqual(User.objects.filter(username="******").count(), 1) self.assertEqual(User.objects.filter(username="******").count(), 1)