def post(self, request): serializer = s.SignupSerializer(data=request.data) serializer.is_valid(raise_exception=True) user = serializer.save() if user is not None: verification_pin = random.randint(100000, 999999) user.verification_token = verification_pin user.verification_tries = 0 user.save() activation_url = '{}/auth/activate/{}/{}'.format( settings.GLUU_USER_APP, user.id, user.verification_token ) task = AsyncTask( user.email_user, 'emails/profiles/verify_email/verify_email_subject.txt', 'emails/profiles/verify_email/verify_email.txt', { 'activation_link': activation_url, 'user': user }, 'emails/profiles/verify_email/verify_email.html' ) task.run() user = s.UserSerializer(user) return Response( {'results': user.data}, status=status.HTTP_201_CREATED )
def post(self, request, *args, **kwargs): serializer = s.RequestEmailSerializer(data=request.data) serializer.is_valid(raise_exception=True) user = serializer.validated_data['user'] verification_pin = random.randint(100000, 999999) user.verification_token = verification_pin user.verification_tries = 0 user.save() activation_url = '{}/auth/activate/{}/{}'.format( settings.FRONTEND_URL, user.id, user.verification_token ) task = AsyncTask( user.email_user, 'emails/profiles/verify_email/verify_email_subject.txt', 'emails/profiles/verify_email/verify_email.txt', { 'activation_link': activation_url, 'user': user }, 'emails/profiles/verify_email/verify_email.html' ) task.run() return Response( { 'results': 'Code sent!' }, status=status.HTTP_201_CREATED )
def run(to, text): task = AsyncTask( 'apps.services.controller.sms_controller.send_otp_sms_task', to, text, group='sms', hook='apps.services.tasks.sms_task.hook') task.run()
def offload_task(taskname, force_sync=False, *args, **kwargs): """ Create an AsyncTask if workers are running. This is different to a 'scheduled' task, in that it only runs once! If workers are not running or force_sync flag is set then the task is ran synchronously. """ try: from django_q.tasks import AsyncTask except (AppRegistryNotReady): logger.warning("Could not offload task - app registry not ready") return import importlib from InvenTree.status import is_worker_running if is_worker_running() and not force_sync: # Running as asynchronous task try: task = AsyncTask(taskname, *args, **kwargs) task.run() except ImportError: logger.warning(f"WARNING: '{taskname}' not started - Function not found") else: # Split path try: app, mod, func = taskname.split('.') app_mod = app + '.' + mod except ValueError: logger.warning(f"WARNING: '{taskname}' not started - Malformed function path") return # Import module from app try: _mod = importlib.import_module(app_mod) except ModuleNotFoundError: logger.warning(f"WARNING: '{taskname}' not started - No module named '{app_mod}'") return # Retrieve function try: _func = getattr(_mod, func) except AttributeError: # getattr does not work for local import _func = None try: if not _func: _func = eval(func) except NameError: logger.warning(f"WARNING: '{taskname}' not started - No function named '{func}'") return # Workers are not running: run it as synchronous task _func()
def test_asynctask_class(broker, monkeypatch): broker.purge_queue() broker.cache.clear() a = AsyncTask("math.copysign") assert a.func == "math.copysign" a.args = (1, -1) assert a.started is False a.cached = True assert a.cached a.sync = True assert a.sync a.broker = broker assert a.broker == broker a.run() assert a.result() == -1 assert a.fetch().result == -1 # again with kwargs a = AsyncTask("math.copysign", 1, -1, cached=True, sync=True, broker=broker) a.run() assert a.result() == -1 # with q_options a = AsyncTask( "math.copysign", 1, -1, q_options={ "cached": True, "sync": False, "broker": broker }, ) assert not a.sync a.sync = True assert a.kwargs["q_options"]["sync"] is True a.run() assert a.result() == -1 a.group = "async_class_test" assert a.group == "async_class_test" a.save = False assert not a.save a.hook = "djq.tests.tasks.hello" assert a.hook == "djq.tests.tasks.hello" assert a.started is False a.run() assert a.result_group() == [-1] assert a.fetch_group() == [a.fetch()] # global overrides monkeypatch.setattr(Conf, "SYNC", True) monkeypatch.setattr(Conf, "CACHED", True) a = AsyncTask("math.floor", 1.5) a.run() assert a.result() == 1
def test_asynctask_class(broker, monkeypatch): broker.purge_queue() broker.cache.clear() a = AsyncTask('math.copysign') assert a.func == 'math.copysign' a.args = (1, -1) assert a.started is False a.cached = True assert a.cached a.sync = True assert a.sync a.broker = broker assert a.broker == broker a.run() assert a.result() == -1 assert a.fetch().result == -1 # again with kwargs a = AsyncTask('math.copysign', 1, -1, cached=True, sync=True, broker=broker) a.run() assert a.result() == -1 # with q_options a = AsyncTask('math.copysign', 1, -1, q_options={ 'cached': True, 'sync': False, 'broker': broker }) assert not a.sync a.sync = True assert a.kwargs['q_options']['sync'] is True a.run() assert a.result() == -1 a.group = 'async_class_test' assert a.group == 'async_class_test' a.save = False assert not a.save a.hook = 'djq.tests.tasks.hello' assert a.hook == 'djq.tests.tasks.hello' assert a.started is False a.run() assert a.result_group() == [-1] assert a.fetch_group() == [a.fetch()] # global overrides monkeypatch.setattr(Conf, 'SYNC', True) monkeypatch.setattr(Conf, 'CACHED', True) a = AsyncTask('math.floor', 1.5) a.run() assert a.result() == 1
def post(self, request, *args, **kwargs): serializer = self.serializer_class( instance=request.user, data=request.data, partial=True ) serializer.is_valid(raise_exception=True) user = serializer.save() task = AsyncTask( user.sync_data ) task.run() return Response( {'results': serializer.data}, status=status.HTTP_200_OK )
def offload_task(taskname, *args, **kwargs): """ Create an AsyncTask. This is different to a 'scheduled' task, in that it only runs once! """ try: from django_q.tasks import AsyncTask except (AppRegistryNotReady): logger.warning("Could not offload task - app registry not ready") return task = AsyncTask(taskname, *args, **kwargs) task.run()
def test_asynctask_class(broker, monkeypatch): broker.purge_queue() broker.cache.clear() a = AsyncTask('math.copysign') assert a.func == 'math.copysign' a.args = (1, -1) assert a.started is False a.cached = True assert a.cached is True a.sync = True assert a.sync is True a.broker = broker assert a.broker == broker a.run() assert a.result() == -1 assert a.fetch().result == -1 # again with kwargs a = AsyncTask('math.copysign', 1, -1, cached=True, sync=True, broker=broker) a.run() assert a.result() == -1 # with q_options a = AsyncTask('math.copysign', 1, -1, q_options={'cached': True, 'sync': False, 'broker': broker}) assert a.sync is False a.sync = True assert a.kwargs['q_options']['sync'] is True a.run() assert a.result() == -1 a.group = 'async_class_test' assert a.group == 'async_class_test' a.save = False assert a.save is False a.hook = 'djq.tests.tasks.hello' assert a.hook == 'djq.tests.tasks.hello' assert a.started is False a.run() assert a.result_group() == [-1] assert a.fetch_group() == [a.fetch()] # global overrides monkeypatch.setattr(Conf, 'SYNC', True) monkeypatch.setattr(Conf, 'CACHED', True) a = AsyncTask('math.floor', 1.5) a.run() assert a.result() == 1