Skip to content

Commit

Permalink
OpenConceptLab/ocl_issues#703 | Flower tasks api to query with taskna…
Browse files Browse the repository at this point in the history
…me filter
  • Loading branch information
snyaggarwal committed Apr 29, 2021
1 parent f22d86d commit 12eeaef
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 20 deletions.
6 changes: 0 additions & 6 deletions core/common/tasks.py
Expand Up @@ -193,12 +193,6 @@ def bulk_import_parts_inline(self, input_list, username, update_if_exists):
).run()


@app.task(base=QueueOnce)
def bulk_priority_import(to_import, username, update_if_exists):
from core.importers.models import BulkImport
return BulkImport(content=to_import, username=username, update_if_exists=update_if_exists).run()


@app.task
def send_user_verification_email(user_id):
from core.users.models import UserProfile
Expand Down
17 changes: 8 additions & 9 deletions core/importers/tests.py
Expand Up @@ -6,7 +6,7 @@
from celery_once import AlreadyQueued
from django.core.files.uploadedfile import SimpleUploadedFile
from django.db.models import F
from mock import patch, Mock, ANY
from mock import patch, Mock, ANY, call

from core.collections.models import Collection
from core.common.tests import OCLAPITestCase, OCLTestCase
Expand Down Expand Up @@ -533,14 +533,10 @@ def setUp(self):
@patch('core.importers.views.flower_get')
def test_get_without_task_id(self, flower_get_mock):
task_id1 = "{}-{}~{}".format(str(uuid.uuid4()), 'ocladmin', 'priority')
task_id2 = "{}-{}~{}".format(str(uuid.uuid4()), 'ocladmin', 'priority')
task_id3 = "{}-{}~{}".format(str(uuid.uuid4()), 'foobar', 'normal')
task_id4 = "{}-{}".format(str(uuid.uuid4()), 'foobar')
task_id2 = "{}-{}~{}".format(str(uuid.uuid4()), 'foobar', 'normal')
flower_tasks = {
task_id1: dict(name='core.common.tasks.bulk_import', state='success'),
task_id2: dict(name='foo-task', state='failed'),
task_id3: dict(name='core.common.tasks.bulk_import', state='failed'),
task_id4: dict(name='foo-task', state='pending')
task_id2: dict(name='core.common.tasks.bulk_import', state='failed'),
}
flower_get_mock.return_value = Mock(json=Mock(return_value=flower_tasks))

Expand All @@ -560,7 +556,7 @@ def test_get_without_task_id(self, flower_get_mock):
)

self.assertEqual(response.status_code, 200)
self.assertEqual(response.data, [dict(queue='normal', state='failed', task=task_id3, username='foobar')])
self.assertEqual(response.data, [dict(queue='normal', state='failed', task=task_id2, username='foobar')])

response = self.client.get(
'/importers/bulk-import/priority/?username=ocladmin',
Expand All @@ -579,7 +575,10 @@ def test_get_without_task_id(self, flower_get_mock):

self.assertEqual(response.status_code, 200)
self.assertEqual(response.data, [])
flower_get_mock.assert_called_with('api/tasks')
calls = flower_get_mock.mock_calls
self.assertTrue(call('api/tasks?taskname=core.common.tasks.bulk_import') in calls)
self.assertTrue(call('api/tasks?taskname=core.common.tasks.bulk_import_parallel_inline') in calls)
self.assertTrue(call('api/tasks?taskname=core.common.tasks.bulk_import_inline') in calls)

@patch('core.importers.views.AsyncResult')
def test_get_with_task_id_success(self, async_result_klass_mock):
Expand Down
10 changes: 5 additions & 5 deletions core/importers/views.py
Expand Up @@ -144,8 +144,11 @@ def get(
)

try:
response = flower_get('api/tasks')
flower_tasks = response.json()
flower_tasks = {
**flower_get('api/tasks?taskname=core.common.tasks.bulk_import').json(),
**flower_get('api/tasks?taskname=core.common.tasks.bulk_import_parallel_inline').json(),
**flower_get('api/tasks?taskname=core.common.tasks.bulk_import_inline').json()
}
except Exception as ex:
return Response(
dict(detail='Flower service returned unexpected result. Maybe check healthcheck.', exception=str(ex)),
Expand All @@ -154,9 +157,6 @@ def get(

tasks = []
for task_id, value in flower_tasks.items():
if not value.get('name', None) or not value['name'].startswith('core.common.tasks.bulk_import'):
continue

task = parse_bulk_import_task_id(task_id)

if user.is_staff or user.username == task['username']:
Expand Down

0 comments on commit 12eeaef

Please sign in to comment.