def test_worker_status_timeout(self): dqsettings = dict( SQ_ADDITIONAL_SIGNALS=['signalqueue.tests'], SQ_RUNMODE='SQ_ASYNC_REQUEST') with self.settings(**dqsettings): import signalqueue signalqueue.autodiscover() from signalqueue.worker import queues queue = queues['db'] oldcount = queue.count() from signalqueue.models import log_exceptions with log_exceptions(queue_name='db'): yodogg = queue.retrieve() queue.dequeue(queued_signal=yodogg) print "Sleeping for 0.5 seconds..." import time time.sleep(0.5) self.http_client.fetch(self.get_url('/status'), self.stop) response = self.wait(timeout=10) phrase = "%s enqueued signals" % queue.count() self.assertTrue(phrase in response.body) newcount = queue.count() self.assertTrue(int(oldcount) > int(newcount))
def test_backend_total_exception_count(self): from signalqueue.models import log_exceptions with log_exceptions(queue_name="db", exc_type=ValueError): raise ValueError("Yo dogg: I hear you like logged exception messages") with self.queue.log_exceptions(exc_type=ValueError): raise ValueError("Yo dogg: I hear your queue also likes logged exception messages") self.assertTrue(self.queue.exceptions.totalcount() == 2)
def flush_signal_queue(self, apps, options): """ Flushes the named signal queue, executing all enqueued signals. """ from django.conf import settings from signalqueue import SQ_RUNMODES as runmodes from signalqueue.worker import backends from signalqueue.models import log_exceptions queue_name = options.get('queue_name') queues = backends.ConnectionHandler(settings.SQ_QUEUES, runmodes['SQ_ASYNC_MGMT']) if not queue_name in queues: self.echo("\n--- No definition found for a queue named '%s'" % (queue_name,), color=16) self.echo("\n--- Your defined queues have these names: '%s'" % ("', '".join(queues.keys()),), color=16) self.echo("\n>>> Exiting ...\n\n", color=16) sys.exit(2) queue = queues[queue_name] try: queue_available = queue.ping() except: self.echo("\n--- Can't ping the backend for %s named '%s'" % (queue.__class__.__name__, queue_name), color=16) self.echo("\n--- Is the server running?", color=16) self.echo("\n>>> Exiting ...\n\n", color=16) sys.exit(2) if not queue_available: self.echo("\n--- Can't ping the backend for %s named '%s'" % (queue.__class__.__name__, queue_name), color=16) self.echo("\n--- Is the server running?", color=16) self.echo("\n>>> Exiting ...\n\n", color=16) sys.exit(2) self.echo("\n>>> Flushing signal queue '%s' -- %s enqueued signals total" % ( queue.queue_name, queue.count()), color=31) from django.db.models.loading import cache if queue.count() > 0: for signalblip in queue: #self.echo("\n>>> Signal: ", color=31) #self.echo("\n%s" % pformat(signalblip), color=31) sender_dict = signalblip.get('sender') sender = cache.get_model(str(sender_dict['app_label']), str(sender_dict['modl_name'])) signal = signalblip.get('signal') self.echo(">>> Processing signal sent by %s.%s: %s.%s" % ( sender._meta.app_label, sender.__name__, signal.keys()[0], signal.values()[0]), color=31) with log_exceptions(queue_name=queue_name): queue.dequeue(queued_signal=signalblip) self.echo(">>> Done flushing signal queue '%s' -- %s enqueued signals remaining" % ( queue.queue_name, queue.count()), color=31) self.echo("\n")
def test_exception_log_context_manager(self): from signalqueue.models import log_exceptions with log_exceptions(queue_name="db", exc_type=ValueError): raise ValueError("Yo dogg: I hear you like logged exception messages") with self.assertRaises(TestException): with log_exceptions(queue_name="db", exc_type=ValueError): raise TestException("Yo dogg: I hear you like logged exception messages") exc_message = "Yo dogg: I hear you like logged exception messages" for exc in (ValueError, TestException): with log_exceptions(queue_name="db", exc_type=(ValueError, TestException)): raise exc(exc_message) from signalqueue.models import WorkerExceptionLog self.assertEqual(WorkerExceptionLog.objects.totalcount(), 3) self.assertEqual(WorkerExceptionLog.objects.withtype('ValueError').totalcount(), 2) self.assertEqual(WorkerExceptionLog.objects.like(ValueError).totalcount(), 2) self.assertEqual(WorkerExceptionLog.objects.withtype('TestException').totalcount(), 1) self.assertEqual(WorkerExceptionLog.objects.like(TestException).totalcount(), 1)