def apply_async(cls, method, model=None, instance=None, args=None, kwargs=None, **celery_options): """Wrapper for painless asynchronous dispatch of method inside given model. .. note:: * Works only when called within a transaction. * Required only if the menthod is not already decorated as a async_sqs_task :param model: String representing global name of the model or reference to model class itself. :param method: Name or method object :param instance: The instance on which the method call should happen if it is an instance :param args: positional arguments passed on to method as list/tuple. :param kwargs: keyword arguments passed on to method as dict. :returns :class:`AsyncResult`: """ payload = cls.build_payload(method, model, instance, args, kwargs) if isinstance(payload, MockResult): # Test async call will return MockResult return payload return execute.apply_async( # Args for the call (Transaction().database.name, Transaction().user, cls.serialize_payload(payload)), # Additional celery options **celery_options )
def apply_async( cls, method, model=None, instance=None, args=None, kwargs=None, **celery_options): """Wrapper for painless asynchronous dispatch of method inside given model. .. note:: * Works only when called within a transaction. * Required only if the menthod is not already decorated as a async_sqs_task :param model: String representing global name of the model or reference to model class itself. :param method: Name or method object :param instance: The instance on which the method call should happen if it is an instance :param args: positional arguments passed on to method as list/tuple. :param kwargs: keyword arguments passed on to method as dict. :returns :class:`AsyncResult`: """ payload = cls.build_payload(method, model, instance, args, kwargs) if isinstance(payload, MockResult): # Test async call will return MockResult return payload return execute.apply_async( # Args for the call ( Transaction().database.name, Transaction().user, cls.serialize_payload(payload) ), # Additional celery options **celery_options )
def apply_async( cls, method, model=None, instance=None, args=None, kwargs=None, **celery_options): """Wrapper for painless asynchronous dispatch of method inside given model. .. note:: * Works only when called within a transaction. * Required only if the menthod is not already decorated as a async_sqs_task :param model: String representing global name of the model or reference to model class itself. :param method: Name or method object :param instance: The instance on which the method call should happen if it is an instance :param args: positional arguments passed on to method as list/tuple. :param kwargs: keyword arguments passed on to method as dict. :returns :class:`AsyncResult`: """ if isinstance(method, basestring): method_name = method else: method_name = method.__name__ if isinstance(model, basestring): model_name = model elif model: model_name = model.__name__ else: model_name = None if isinstance(instance, Model): model_name = instance.__name__ args = args or [] kwargs = kwargs or {} if current_app.conf.get('TEST_MODE', False): if instance: return MockResult( getattr(instance, method_name)(*args, **kwargs) ) else: CurrentModel = Pool().get(model_name) return MockResult( getattr(CurrentModel, method_name)(*args, **kwargs) ) payload = { 'model_name': model_name, 'instance': instance, 'method_name': method_name, 'args': args, 'kwargs': kwargs, 'context': Transaction().context, } return execute.apply_async( # Args for the call ( Transaction().cursor.database_name, Transaction().user, cls.serialize_payload(payload) ), # Additional celery options **celery_options )