From 5017480bb1263664194679d14ac426ee335c52bc Mon Sep 17 00:00:00 2001 From: Dan Nicholson Date: Wed, 16 Aug 2023 15:44:36 -0600 Subject: [PATCH] Override remotecontentimport task priority to high When we enqueue the `remotecontentimport` task from `CollectionDownloadManager`, we can specify the priority of the jobs as high. However, when a user initiates a content download from the frontend using the tasks API, that's not possible. Since the upstream default priority is regular, it can get blocked behind any other queued tasks such as the background thumbnail downloads. This adds a copy of upstream's `remotecontentimport` with the default priority set to high and arranges for it to be used throughout the plugin. Helps: #739 --- .../src/modules/manageContent/handlers.js | 4 ++-- kolibri_explore_plugin/collectionviews.py | 2 +- kolibri_explore_plugin/tasks.py | 18 ++++++++++++++++++ 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/kolibri_explore_plugin/assets/src/modules/manageContent/handlers.js b/kolibri_explore_plugin/assets/src/modules/manageContent/handlers.js index 49fae2f67..10a3c91c2 100644 --- a/kolibri_explore_plugin/assets/src/modules/manageContent/handlers.js +++ b/kolibri_explore_plugin/assets/src/modules/manageContent/handlers.js @@ -1,4 +1,4 @@ -import { TaskStatuses, TaskTypes } from 'kolibri.utils.syncTaskUtils'; +import { TaskStatuses } from 'kolibri.utils.syncTaskUtils'; import { constants } from 'ek-components'; import { TaskResource } from 'kolibri.resources'; import store from 'kolibri.coreVue.vuex.store'; @@ -48,7 +48,7 @@ export function startContentDownload(channelId, contentId) { channel_id: channelId, channel_name: 'foo', node_ids: [contentId], - type: TaskTypes.REMOTECONTENTIMPORT, + type: 'kolibri_explore_plugin.tasks.remotecontentimport', }; return TaskResource.startTask(taskParams) diff --git a/kolibri_explore_plugin/collectionviews.py b/kolibri_explore_plugin/collectionviews.py index a7d56ba8b..2c33f896c 100644 --- a/kolibri_explore_plugin/collectionviews.py +++ b/kolibri_explore_plugin/collectionviews.py @@ -8,7 +8,6 @@ from kolibri.core.content.errors import InsufficientStorageSpaceError from kolibri.core.content.models import ChannelMetadata from kolibri.core.content.tasks import remotechannelimport -from kolibri.core.content.tasks import remotecontentimport from kolibri.core.content.utils.content_manifest import ContentManifest from kolibri.core.content.utils.content_manifest import ( ContentManifestParseError, @@ -25,6 +24,7 @@ from .tasks import applyexternaltags from .tasks import BACKGROUND_QUEUE from .tasks import QUEUE +from .tasks import remotecontentimport logger = logging.getLogger(__name__) diff --git a/kolibri_explore_plugin/tasks.py b/kolibri_explore_plugin/tasks.py index 71276a5f9..b8a461ee3 100644 --- a/kolibri_explore_plugin/tasks.py +++ b/kolibri_explore_plugin/tasks.py @@ -1,8 +1,10 @@ import logging from django.core.management import call_command +from kolibri.core.content import tasks as content_tasks from kolibri.core.serializers import HexOnlyUUIDField from kolibri.core.tasks.decorators import register_task +from kolibri.core.tasks.job import Priority from kolibri.core.tasks.job import State from kolibri.core.tasks.main import job_storage from kolibri.core.tasks.permissions import CanManageContent @@ -44,6 +46,22 @@ def applyexternaltags(node_id, tags=None): call_command("applyexternaltags", node_id, tags=tags) +# This is a duplicate of the upstream remotecontentimport task with the +# priority raised to HIGH. +@register_task( + validator=content_tasks.RemoteChannelResourcesImportValidator, + track_progress=True, + cancellable=True, + permission_classes=[CanManageContent], + priority=Priority.HIGH, + queue=QUEUE, + long_running=True, + status_fn=content_tasks.get_status, +) +def remotecontentimport(*args, **kwargs): + return content_tasks.remotecontentimport.func(*args, **kwargs) + + def restart_failed_background_jobs(): for job in job_storage.get_all_jobs(queue=BACKGROUND_QUEUE): if job.state == State.FAILED: