def async_refresh(self, *args, **kwargs): """ Trigger an asynchronous job to refresh the cache """ # We trigger the task with the class path to import as well as the # (a) args and kwargs for instantiating the class # (b) args and kwargs for calling the 'refresh' method try: enqueue_task( kwargs=dict( klass_str=self.class_path, obj_args=self.get_constructor_args(), obj_kwargs=self.get_constructor_kwargs(), call_args=args, call_kwargs=kwargs ), **self.task_options ) except Exception as e: # Handle exceptions from talking to RabbitMQ - eg connection # refused. When this happens, we try to run the task # synchronously. logger.error("Unable to trigger task asynchronously - failing " "over to synchronous refresh", exc_info=True) try: return self.refresh(*args, **kwargs) except Exception as e: # Something went wrong while running the task logger.error("Unable to refresh data synchronously: %s", e, exc_info=True) else: logger.debug("Failover synchronous refresh completed successfully")
def async_refresh(self, *args, **kwargs): """ Trigger an asynchronous job to refresh the cache """ # We trigger the task with the class path to import as well as the # (a) args and kwargs for instantiating the class # (b) args and kwargs for calling the 'refresh' method try: enqueue_task(kwargs=dict(klass_str=self.class_path, obj_args=self.get_constructor_args(), obj_kwargs=self.get_constructor_kwargs(), call_args=args, call_kwargs=kwargs), **self.task_options) except Exception as e: # Handle exceptions from talking to RabbitMQ - eg connection # refused. When this happens, we try to run the task # synchronously. logger.error( "Unable to trigger task asynchronously - failing " "over to synchronous refresh", exc_info=True) try: return self.refresh(*args, **kwargs) except Exception as e: # Something went wrong while running the task logger.error("Unable to refresh data synchronously: %s", e, exc_info=True) else: logger.debug( "Failover synchronous refresh completed successfully")
def test_celery(self, celery_mock, rq_mock, settings): settings.CACHEBACK_TASK_QUEUE = 'celery' enqueue_task({'bar': 'baz'}, task_options={'foo': 'bar'}) assert celery_mock.apply_async.called is True assert celery_mock.apply_async.call_args[1] == { 'kwargs': {'bar': 'baz'}, 'foo': 'bar'} assert rq_mock.delay.called is False
def test_rq(self, celery_mock, rq_mock, settings): settings.CACHEBACK_TASK_QUEUE = 'rq' enqueue_task({'bar': 'baz'}, task_options={'foo': 'bar'}) assert celery_mock.apply_async.called is False assert rq_mock.called is True assert rq_mock.call_args[1] == {'foo': 'bar'} assert rq_mock.return_value.enqueue.called is True assert rq_mock.return_value.enqueue.call_args[1] == {'bar': 'baz'}
def test_rq_dont_store_result(self, celery_mock, rq_mock, settings): settings.CACHEBACK_TASK_QUEUE = 'rq' settings.CACHEBACK_TASK_IGNORE_RESULT = True enqueue_task({'bar': 'baz'}, task_options={'foo': 'bar'}) assert celery_mock.apply_async.called is False assert rq_mock.called is True assert rq_mock.call_args[1] == {'foo': 'bar'} assert rq_mock.return_value.enqueue_call.called is True assert rq_mock.return_value.enqueue_call.call_args[1] == { 'kwargs': { 'bar': 'baz' }, 'result_ttl': 0, }
def test_unkown(self, settings): settings.CACHEBACK_TASK_QUEUE = 'unknown' with pytest.raises(ImproperlyConfigured) as exc: enqueue_task('foo') assert 'Unkown task queue' in str(exc.value)
def test_rq(self, celery_mock, rq_mock, settings): settings.CACHEBACK_TASK_QUEUE = 'rq' enqueue_task(kwargs={'bar': 'baz'}) assert celery_mock.apply_async.called is False assert rq_mock.delay.called is True assert rq_mock.delay.call_args[1] == {'bar': 'baz'}