Python celery.exceptions.TimeoutError() Examples

The following are 19 code examples of celery.exceptions.TimeoutError(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module celery.exceptions , or try the search function .
Example #1
Source File: ajax.py    From dissemin with GNU Affero General Public License v3.0 6 votes vote down vote up
def waitForConsolidatedField(request):
    success = False
    try:
        paper = Paper.objects.get(pk=int(request.GET["id"]))
    except (KeyError, ValueError, Paper.DoesNotExist):
        return {'success': success, 'message': 'Invalid paper id'}, 404
    field = request.GET.get('field')
    value = None
    try:
        paper.consolidate_metadata(wait=True)
    except TimeoutError:
        # Zotero instance is down / slow / failing, consolidation failed. Not
        # a big deal.
        pass
    if field == 'abstract':
        value = kill_html(paper.abstract)
        success = len(paper.abstract) > 64
    else:
        return {'success': success, 'message': 'Invalid field'}, 401
    return {'success': success, 'value': value} 
Example #2
Source File: views.py    From bridge-adaptivity with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def sync_collection(request, slug):
    """
    Sync collection - engine API.

    :param slug: collection_slug identifier.
    :return: JSON response with the sync result
        {
            "engines": [
                 {"<engine_name_1>": {"success": true}},
                 {"<engine_name_2>": {"success": false, "message": "Message of error description"}}
            ]
        }
    """
    try:
        result = [v for obj, v in module_views.sync_collection(request, slug, api_request=True)]
    except TimeoutError:
        log.debug(f"The Collection sync task failed because of timeout error.")
        return HttpResponseBadRequest(reason="Collection sync was failed, the reason is: TimeoutError")
    log.debug(f"The result of the sync task is: {result}")
    return JsonResponse(data={'engines': result}) 
Example #3
Source File: test_inbox.py    From Inboxen with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_get_with_before_and_after_param(self, task_mock):
        task_mock.return_value.id = "abc"
        task_mock.return_value.get.side_effect = exceptions.TimeoutError

        response = self.client.get(self.url + "?after=blahblah&before=bluhbluh")
        self.assertEqual(response.status_code, 200)

        self.assertEqual(task_mock.call_count, 1)
        self.assertEqual(task_mock.return_value.get.call_count, 1)

        self.assertEqual(task_mock.call_args, ((), {"kwargs": {
            "user_id": self.user.id,
            "search_term": u"cheddär",
            "inbox": str(self.inbox),
            "before": "bluhbluh",
            "after": "blahblah",
        }}))
        self.assertEqual(response.context["waiting"], True)
        self.assertNotIn("has_next", response.context)
        self.assertNotIn("has_previous", response.context)
        self.assertNotIn("last", response.context)
        self.assertNotIn("first", response.context) 
Example #4
Source File: views.py    From Inboxen with GNU Affero General Public License v3.0 6 votes vote down vote up
def results(self):
        """Fetch result from either the cache or the queue

        Raises TimeoutError if results aren't ready by self.timeout
        """
        if self.query == "":
            return {}

        result = cache.get(self.get_cache_key())
        if result is None or settings.CELERY_TASK_ALWAYS_EAGER:
            search_task = self.search_task()
            result = {"task": search_task.id}
            cache.set(self.get_cache_key(), result, settings.SEARCH_TIMEOUT)
        elif "task" in result:
            search_task = AsyncResult(result["task"])
        else:
            return result

        try:
            return search_task.get(self.timeout)
        except exceptions.TimeoutError:
            return result or {}
        except Exception:
            # some error in the search task, so return an empty dict
            return {} 
Example #5
Source File: views.py    From Inboxen with GNU Affero General Public License v3.0 6 votes vote down vote up
def search_api(request):
    try:
        key = request.GET["token"]
    except KeyError:
        raise http.Http404

    result = cache.get(key)

    if result is not None and "task" in result:
        search_task = AsyncResult(result["task"])
        try:
            search_task.get(TIMEOUT)
        except exceptions.TimeoutError:
            return http.HttpResponse(status=202)  # 202: still waiting for task
        except Exception:
            return http.HttpResponseBadRequest()  # 400: search task errored
        return http.HttpResponse(status=201)  # 201: search results ready
    elif result is not None:
        return http.HttpResponse(status=201)  # 201: search results ready
    else:
        return http.HttpResponseBadRequest()  # 400: no search is being performed 
Example #6
Source File: test_inbox.py    From Inboxen with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_get_with_before_param(self, task_mock):
        task_mock.return_value.id = "abc"
        task_mock.return_value.get.side_effect = exceptions.TimeoutError

        response = self.client.get(self.url + "?before=blahblah")
        self.assertEqual(response.status_code, 200)

        self.assertEqual(task_mock.call_count, 1)
        self.assertEqual(task_mock.return_value.get.call_count, 1)

        self.assertEqual(task_mock.call_args, ((), {"kwargs": {
            "user_id": self.user.id,
            "search_term": u"cheddär",
            "inbox": str(self.inbox),
            "before": "blahblah",
            "after": None,
        }}))
        self.assertEqual(response.context["waiting"], True)
        self.assertNotIn("has_next", response.context)
        self.assertNotIn("has_previous", response.context)
        self.assertNotIn("last", response.context)
        self.assertNotIn("first", response.context) 
Example #7
Source File: test_home.py    From Inboxen with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_get_task_run(self, task_mock):
        task_mock.return_value.id = "abc"
        task_mock.return_value.get.side_effect = exceptions.TimeoutError

        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 200)

        self.assertEqual(task_mock.call_count, 1)
        self.assertEqual(task_mock.return_value.get.call_count, 1)

        self.assertEqual(task_mock.call_args, ((), {"kwargs": {
            "user_id": self.user.id,
            "search_term": u"cheddär",
            "before": None,
            "after": None,
        }}))
        self.assertEqual(response.context["waiting"], True)
        self.assertNotIn("has_next", response.context)
        self.assertNotIn("has_previous", response.context)
        self.assertNotIn("last", response.context)
        self.assertNotIn("first", response.context) 
Example #8
Source File: test_home.py    From Inboxen with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_get_with_before_param(self, task_mock):
        task_mock.return_value.id = "abc"
        task_mock.return_value.get.side_effect = exceptions.TimeoutError

        response = self.client.get(self.url + "?before=blahblah")
        self.assertEqual(response.status_code, 200)

        self.assertEqual(task_mock.call_count, 1)
        self.assertEqual(task_mock.return_value.get.call_count, 1)

        self.assertEqual(task_mock.call_args, ((), {"kwargs": {
            "user_id": self.user.id,
            "search_term": u"cheddär",
            "before": "blahblah",
            "after": None,
        }}))
        self.assertEqual(response.context["waiting"], True)
        self.assertNotIn("has_next", response.context)
        self.assertNotIn("has_previous", response.context)
        self.assertNotIn("last", response.context)
        self.assertNotIn("first", response.context) 
Example #9
Source File: test_home.py    From Inboxen with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_get_with_before_and_after_param(self, task_mock):
        task_mock.return_value.id = "abc"
        task_mock.return_value.get.side_effect = exceptions.TimeoutError

        response = self.client.get(self.url + "?after=blahblah&before=bluhbluh")
        self.assertEqual(response.status_code, 200)

        self.assertEqual(task_mock.call_count, 1)
        self.assertEqual(task_mock.return_value.get.call_count, 1)

        self.assertEqual(task_mock.call_args, ((), {"kwargs": {
            "user_id": self.user.id,
            "search_term": u"cheddär",
            "before": "bluhbluh",
            "after": "blahblah",
        }}))
        self.assertEqual(response.context["waiting"], True)
        self.assertNotIn("has_next", response.context)
        self.assertNotIn("has_previous", response.context)
        self.assertNotIn("last", response.context)
        self.assertNotIn("first", response.context) 
Example #10
Source File: test_inbox.py    From Inboxen with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_get_task_run(self, task_mock):
        task_mock.return_value.id = "abc"
        task_mock.return_value.get.side_effect = exceptions.TimeoutError

        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 200)

        self.assertEqual(task_mock.call_count, 1)
        self.assertEqual(task_mock.return_value.get.call_count, 1)

        self.assertEqual(task_mock.call_args, ((), {"kwargs": {
            "user_id": self.user.id,
            "search_term": u"cheddär",
            "inbox": str(self.inbox),
            "before": None,
            "after": None,
        }}))
        self.assertEqual(response.context["waiting"], True)
        self.assertNotIn("has_next", response.context)
        self.assertNotIn("has_previous", response.context)
        self.assertNotIn("last", response.context)
        self.assertNotIn("first", response.context) 
Example #11
Source File: __init__.py    From girder_worker with Apache License 2.0 5 votes vote down vote up
def get_result(self, params):
        cid = params['celery_id']
        a1 = app.AsyncResult(cid)

        # Note: There is no reasonable way to validate a celery task
        # asyncresult id. See:
        # https://github.com/celery/celery/issues/3596#issuecomment-262102185
        # This means for ALL values of celery_id return either the
        # value or None. Note also that you will only be able to get
        # the result via this method once. All subsequent calls will
        # return None.
        try:
            return a1.get(timeout=0.2)
        except TimeoutError:
            return None 
Example #12
Source File: test_inbox.py    From Inboxen with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_task_running(self, result_mock):
        cache.set(self.key, {"task": "blahblahblah"})
        result_mock.return_value.get.side_effect = exceptions.TimeoutError

        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.context["waiting"], True)

        self.assertEqual(result_mock.call_count, 1)
        self.assertEqual(result_mock.call_args, (("blahblahblah",), {})) 
Example #13
Source File: test_inbox.py    From Inboxen with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_get_cached_result(self, task_mock):
        inbox = factories.InboxFactory(user=self.user)

        task_mock.return_value.id = "abc"
        task_mock.return_value.get.side_effect = exceptions.TimeoutError

        cache.set(self.key, {
            "results": [inbox.id],
            "has_next": True,
            "has_previous": False,
            "first": "some-randomstring",
            "last": "somerandom-string",
        })

        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 200)

        self.assertEqual(task_mock.call_count, 0)
        self.assertEqual(response.context["has_next"], True)
        self.assertEqual(response.context["last"], "somerandom-string")
        self.assertEqual(response.context["has_previous"], False)
        self.assertEqual(response.context["first"], "some-randomstring")
        self.assertEqual(response.context["waiting"], False)

        cache.set(self.key, {
            "results": [],
            "has_next": True,
            "has_previous": False,
            "first": "some-randomstring",
            "last": "somerandom-string",
        })

        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 200)

        self.assertEqual(task_mock.call_count, 0)
        self.assertEqual(response.context["waiting"], False)
        self.assertNotIn("has_next", response.context)
        self.assertNotIn("has_previous", response.context)
        self.assertNotIn("first", response.context)
        self.assertNotIn("last", response.context) 
Example #14
Source File: test_home.py    From Inboxen with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_task_running(self, result_mock):
        cache.set(self.key, {"task": "blahblahblah"})
        result_mock.return_value.get.side_effect = exceptions.TimeoutError

        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.context["waiting"], True)

        self.assertEqual(result_mock.call_count, 1)
        self.assertEqual(result_mock.call_args, (("blahblahblah",), {})) 
Example #15
Source File: test_home.py    From Inboxen with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_get_cached_result(self, task_mock):
        inbox = factories.InboxFactory(user=self.user)

        task_mock.return_value.id = "abc"
        task_mock.return_value.get.side_effect = exceptions.TimeoutError

        cache.set(self.key, {
            "results": [inbox.id],
            "has_next": True,
            "has_previous": False,
            "first": "some-randomstring",
            "last": "somerandom-string",
        })

        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 200)

        self.assertEqual(task_mock.call_count, 0)
        self.assertEqual(response.context["has_next"], True)
        self.assertEqual(response.context["last"], "somerandom-string")
        self.assertEqual(response.context["has_previous"], False)
        self.assertEqual(response.context["first"], "some-randomstring")
        self.assertEqual(response.context["waiting"], False)

        cache.set(self.key, {
            "results": [],
            "has_next": True,
            "has_previous": False,
            "first": "some-randomstring",
            "last": "somerandom-string",
        })

        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 200)

        self.assertEqual(task_mock.call_count, 0)
        self.assertEqual(response.context["waiting"], False)
        self.assertNotIn("has_next", response.context)
        self.assertNotIn("has_previous", response.context)
        self.assertNotIn("first", response.context)
        self.assertNotIn("last", response.context) 
Example #16
Source File: test_views.py    From Inboxen with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_search_running_and_timeout(self, result_mock):
        result_mock.return_value.get.side_effect = exceptions.TimeoutError
        cache.cache.set(self.key, {"task": "blahblahblah"})

        response = self.client.head(self.url)
        self.assertEqual(response.status_code, 202)

        self.assertEqual(result_mock.call_count, 1)
        self.assertEqual(result_mock.call_args, (("blahblahblah",), {})) 
Example #17
Source File: test_views.py    From bridge-adaptivity with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_sync_collection_timeout(self, mock_sync):
        expected_msg = 'Collection sync was failed, the reason is: TimeoutError'
        mock_sync.delay().collect.side_effect = TimeoutError
        url = reverse('api:sync_collection', kwargs={'slug': self.collection1.slug})
        response = self.client.get(url)
        self.assertEqual(response.status_code, 400)
        self.assertEqual(response.reason_phrase, expected_msg) 
Example #18
Source File: test_views.py    From bridge-adaptivity with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def mock_timeout(slug):
    if slug == 'col1':
        raise TimeoutError
    return [] 
Example #19
Source File: views.py    From mapstory with GNU General Public License v3.0 4 votes vote down vote up
def layer_remove(request, layername, template='layers/layer_remove.html'):
    layer = _resolve_layer(
        request,
        layername,
        'base.delete_resourcebase',
        _PERMISSION_MSG_DELETE)

    if (request.method == 'GET'):
        return render(request, template, context={
            "layer": layer
        })
    if (request.method == 'POST'):
        try:
            with transaction.atomic():
                # Using Tastypie
                # from geonode.api.resourcebase_api import LayerResource
                # res = LayerResource()
                # request_bundle = res.build_bundle(request=request)
                # layer_bundle = res.build_bundle(request=request, obj=layer)
                # layer_json = res.serialize(None,
                #                            res.full_dehydrate(layer_bundle),
                #                            "application/json")
                # delete_layer.delay(instance=layer_json)
                result = delete_layer.delay(layer_id=layer.id)
                result.wait(10)
        except TimeoutError:
            # traceback.print_exc()
            pass
        except Exception as e:
            traceback.print_exc()
            message = '{0}: {1}.'.format(
                _('Unable to delete layer'), layer.alternate)

            if 'referenced by layer group' in getattr(e, 'message', ''):
                message = _(
                    'This layer is a member of a layer group, you must remove the layer from the group '
                    'before deleting.')

            messages.error(request, message)
            return render(
                request, template, context={"layer": layer})
        # MapStory Specific Change
        return HttpResponseRedirect(reverse("profile_detail", kwargs={'slug': layer.owner}))
        # End MapStory Specific Change
    else:
        return HttpResponse("Not allowed", status=403)