def handle(self, *args, **options): self.repeat = options.get("repeat") self.queue_name = options.get("queue_name") self.default_delay = float(options.get("delay")) self.max_delay = float(options.get("max_delay")) self.backoff_factor = float(options.get("backoff")) if self.backoff_factor < 1.0: raise CommandError("backoff must be greater than or equal to 1") if not self.acquire_lock(): raise CommandError("lock file %s found, exiting" % self.lock_filename) self.delay = self.default_delay invoker = Invoker(self.queue_name) try: while self.repeat or len(invoker.queue): try: result = invoker.dequeue() except CueException, e: self.stderr.write(self.style.ERROR("error during command invocation: '%s'\n" % e)) result = False if result: self.delay = self.default_delay else: time.sleep(self.delay) if self.delay < self.max_delay: self.delay *= self.backoff_factor else: self.delay = self.default_delay except queues.QueueException, e: raise CommandError("error while consuming queue: '%s'" % e)
def setUp(self): self.dummy = User.objects.create_user('username', '*****@*****.**', 'password') self.invoker = Invoker() self.invoker.flush() self.invoker._stack = []
class SimpleTest(TestCase): def setUp(self): self.dummy = User.objects.create_user('username', '*****@*****.**', 'password') self.invoker = Invoker() self.invoker.flush() self.invoker._stack = [] def test_basic_processing(self): # reference locally for clarity invoker = self.invoker # make sure UserCommand got registered self.assertTrue(str(UserCommand) in invoker) self.assertEqual(invoker._registry[str(UserCommand)], UserCommand) # create a command command = UserCommand(self.dummy) command.set_data([self.dummy.email, '*****@*****.**']) # enqueueing the command won't execute it - it just hangs out invoker.enqueue(command) # did the message get enqueued? self.assertEqual(len(invoker.queue), 1) # dequeueing loads from the queue, creates a command and executes it invoker.dequeue() # make sure the command's execute() method got called self.dummy = User.objects.get(username='******') self.assertEqual(self.dummy.email, '*****@*****.**') # check the stack - should now show 1 item having been executed self.assertEqual(len(invoker._stack), 1) def test_decorated_function(self): invoker = self.invoker user_command(self.dummy, '*****@*****.**') self.assertEqual(len(invoker.queue), 1) # the user's email address hasn't changed yet self.assertEqual(self.dummy.email, '*****@*****.**') # dequeue invoker.dequeue() # make sure that the command was executed self.dummy = User.objects.get(username='******') self.assertEqual(self.dummy.email, '*****@*****.**') self.assertEqual(len(invoker.queue), 0) def test_stack(self): invoker = self.invoker # queue up commands, which should be bigger than the invoker's stack size size = invoker.stack_size + 10 stack_size = invoker.stack_size # queue them all up for x in xrange(1, size): command = UserCommand(self.dummy) command.set_data([self.dummy.email, '*****@*****.**' % x]) invoker.enqueue(command) self.assertEqual(len(invoker.queue), x) # dequeue them all, checking to see that the stack correctly reflects # the most recently executed commands for x in xrange(1, size): invoker.dequeue() tmp_dummy = User.objects.get(username='******') self.assertEqual(tmp_dummy.email, '*****@*****.**' % x) if x < stack_size: self.assertEqual(len(invoker._stack), x) else: self.assertEqual(len(invoker._stack), stack_size) stored_email = '*****@*****.**' % x self.assertTrue(stored_email in invoker._stack[stack_size - 1]) def test_undo(self): invoker = self.invoker command = UserCommand(self.dummy) command.set_data([self.dummy.email, '*****@*****.**']) invoker.enqueue(command) invoker.dequeue() dummy = User.objects.get(pk=self.dummy.pk) self.assertEqual(dummy.email, '*****@*****.**') command.set_data([dummy.email, '*****@*****.**']) invoker.enqueue(command) invoker.dequeue() dummy = User.objects.get(pk=self.dummy.pk) self.assertEqual(dummy.email, '*****@*****.**') invoker.undo() dummy = User.objects.get(pk=self.dummy.pk) self.assertEqual(dummy.email, '*****@*****.**') invoker.undo() dummy = User.objects.get(pk=self.dummy.pk) self.assertEqual(dummy.email, '*****@*****.**') self.assertRaises(CueException, invoker.undo)