Skip to content

Commit

Permalink
Override remotecontentimport task
Browse files Browse the repository at this point in the history
In order to make use of the vendored `FileDownload` class, the
`remotecontentimport` task and its associated import manager classes
need to be changed. This attempts to subclass the existing classes as
much as possible. With those changes, content item downloads go back to
single requests instead of many 128kB range requests per file.

Unfortunately, `remotechannelimport` is still using the old process
where it calls the `remotechannelimport` management command. Patching
that would require duplicating the command. That doesn't seem worth it
and most channel databases fit within a few range requests.
  • Loading branch information
dbnicholson authored and dylanmccall committed Jun 19, 2023
1 parent f4ba4a8 commit 7a444d2
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
2 changes: 1 addition & 1 deletion kolibri_explore_plugin/collectionviews.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -24,6 +23,7 @@

from .tasks import applyexternaltags
from .tasks import QUEUE
from .tasks import remotecontentimport

logger = logging.getLogger(__name__)

Expand Down
76 changes: 76 additions & 0 deletions kolibri_explore_plugin/tasks.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
from django.core.management import call_command
from kolibri.core.content.tasks import get_status as get_content_task_status
from kolibri.core.content.tasks import RemoteChannelResourcesImportValidator
from kolibri.core.content.utils.paths import get_content_storage_remote_url
from kolibri.core.content.utils.resource_import import (
RemoteChannelResourceImportManager,
)
from kolibri.core.content.utils.resource_import import (
RemoteChannelUpdateManager,
)
from kolibri.core.content.utils.resource_import import (
RemoteResourceImportManagerBase,
)
from kolibri.core.serializers import HexOnlyUUIDField
from kolibri.core.tasks.decorators import register_task
from kolibri.core.tasks.permissions import CanManageContent
from kolibri.core.tasks.validation import JobValidator
from rest_framework.serializers import CharField
from rest_framework.serializers import ListField

from .vendor.file_transfer import FileDownload

QUEUE = "content"


Expand Down Expand Up @@ -35,3 +49,65 @@ def validate(self, data):
)
def applyexternaltags(node_id, tags=None):
call_command("applyexternaltags", node_id, tags=tags)


# Local remote resource import overrides to use vendored FileDownload.
class ExploreRemoteResourceImportManagerBase(RemoteResourceImportManagerBase):
def create_file_transfer(self, f, filename, dest):
url = get_content_storage_remote_url(filename, baseurl=self.baseurl)
return FileDownload(
url,
dest,
f["id"],
session=self.session,
cancel_check=self.is_cancelled,
timeout=self.timeout,
)


class ExploreRemoteChannelResourceImportManager(
ExploreRemoteResourceImportManagerBase, RemoteChannelResourceImportManager
):
pass


class ExploreRemoteChannelUpdateManager(
ExploreRemoteResourceImportManagerBase, RemoteChannelUpdateManager
):
pass


@register_task(
validator=RemoteChannelResourcesImportValidator,
track_progress=True,
cancellable=True,
permission_classes=[CanManageContent],
queue=QUEUE,
long_running=True,
status_fn=get_content_task_status,
)
def remotecontentimport(
channel_id,
baseurl=None,
peer_id=None,
node_ids=None,
exclude_node_ids=None,
update=False,
fail_on_error=False,
all_thumbnails=False,
):
manager_class = (
ExploreRemoteChannelUpdateManager
if update
else ExploreRemoteChannelResourceImportManager
)
manager = manager_class(
channel_id,
baseurl=baseurl,
peer_id=peer_id,
node_ids=node_ids,
exclude_node_ids=exclude_node_ids,
fail_on_error=fail_on_error,
all_thumbnails=all_thumbnails,
)
manager.run()

0 comments on commit 7a444d2

Please sign in to comment.