diff --git a/swh/deposit/api/common.py b/swh/deposit/api/common.py --- a/swh/deposit/api/common.py +++ b/swh/deposit/api/common.py @@ -18,11 +18,14 @@ from rest_framework.views import APIView from swh.model import hashutil +from swh.scheduler.utils import create_oneshot_task_dict +from swh.deposit.utils import origin_url_from from ..config import ( SWHDefaultConfig, EDIT_SE_IRI, EM_IRI, CONT_FILE_IRI, ARCHIVE_KEY, METADATA_KEY, RAW_METADATA_KEY, STATE_IRI, DEPOSIT_STATUS_DEPOSITED, DEPOSIT_STATUS_PARTIAL, + DEPOSIT_STATUS_VERIFIED, PRIVATE_CHECK_DEPOSIT, DEPOSIT_STATUS_LOAD_SUCCESS, ARCHIVE_TYPE, METADATA_TYPE ) from ..errors import ( @@ -125,7 +128,7 @@ h.update(chunk) return h.digest() - def _deposit_put(self, deposit_id=None, in_progress=False, + def _deposit_put(self, req, deposit_id=None, in_progress=False, external_id=None): """Save/Update a deposit in db. @@ -169,6 +172,27 @@ deposit.complete_date = complete_date deposit.status = status_type + if self.config['checks']: + deposit.save() # needed to have a deposit id + args = [deposit.collection.name, deposit.id] + scheduler = self.scheduler + if (deposit.status == DEPOSIT_STATUS_DEPOSITED and + not deposit.check_task_id): + check_url = req.build_absolute_uri( + reverse(PRIVATE_CHECK_DEPOSIT, args=args)) + task = create_oneshot_task_dict( + 'check-deposit', deposit_check_url=check_url) + check_task_id = scheduler.create_tasks([task])[0]['id'] + deposit.check_task_id = check_task_id + elif (deposit.status == DEPOSIT_STATUS_VERIFIED and + not deposit.load_task_id): + + url = origin_url_from(deposit) + task = create_oneshot_task_dict( + 'load-deposit', url=url, deposit_id=deposit.id) + load_task_id = scheduler.create_task([task])[0]['id'] + deposit.load_task_id = load_task_id + deposit.save() return deposit @@ -386,7 +410,7 @@ # actual storage of data archive_metadata = filehandler - deposit = self._deposit_put(deposit_id=deposit_id, + deposit = self._deposit_put(req, deposit_id=deposit_id, in_progress=headers['in-progress'], external_id=external_id) self._deposit_request_put( @@ -507,7 +531,7 @@ "Please ensure your metadata file is correctly formatted.") # actual storage of data - deposit = self._deposit_put(deposit_id=deposit_id, + deposit = self._deposit_put(req, deposit_id=deposit_id, in_progress=headers['in-progress'], external_id=external_id) deposit_request_data = { @@ -579,7 +603,7 @@ external_id = metadata.get('external_identifier', headers['slug']) - deposit = self._deposit_put(deposit_id=deposit_id, + deposit = self._deposit_put(req, deposit_id=deposit_id, in_progress=headers['in-progress'], external_id=external_id) diff --git a/swh/deposit/apps.py b/swh/deposit/apps.py --- a/swh/deposit/apps.py +++ b/swh/deposit/apps.py @@ -8,9 +8,3 @@ class DepositConfig(AppConfig): name = 'swh.deposit' - - def ready(self): - super().ready() - - # install the signal permitting to trigger the status' check - from .signals import post_deposit_save # noqa diff --git a/swh/deposit/signals.py b/swh/deposit/signals.py deleted file mode 100644 --- a/swh/deposit/signals.py +++ /dev/null @@ -1,104 +0,0 @@ -# Copyright (C) 2017-2019 The Software Heritage developers -# See the AUTHORS file at the top-level directory of this distribution -# License: GNU General Public License version 3, or any later version -# See top-level LICENSE file for more information - -"""Module in charge of defining some uncoupled actions on deposit. - - Typically, checking that the archives deposited are ok are not - directly testing in the request/answer to avoid too long - computations. - - So this is done in the deposit_on_status_ready_for_check callback. - -""" - -from swh.deposit import utils - -from django.db.models.signals import post_save -from django.dispatch import receiver - -from .models import Deposit -from .config import SWHDefaultConfig, DEPOSIT_STATUS_VERIFIED -from .config import DEPOSIT_STATUS_DEPOSITED - - -def schedule_task(scheduler, task): - """Schedule the task and return its identifier - - Args: - task (dict): Task to schedule - - Returns: - The task identifier - - """ - tasks = scheduler.create_tasks([task]) - if tasks: - created_task = tasks[0] - return created_task['id'] - - -@receiver(post_save, sender=Deposit) -def post_deposit_save(sender, instance, created, raw, using, - update_fields, **kwargs): - """When a deposit is saved, check for the deposit's status change and - schedule actions accordingly. - - When the status passes to deposited, schedule checks. - When the status pass to ready, schedule loading. Otherwise, do - nothing. - - Args: - sender (Deposit): The model class - instance (Deposit): The actual instance being saved - created (bool): True if a new record was created - raw (bool): True if the model is saved exactly as presented - (i.e. when loading a fixture). One should not - query/modify other records in the database as the - database might not be in a consistent state yet - using: The database alias being used - update_fields: The set of fields to update as passed to - Model.save(), or None if update_fields wasn’t - passed to save() - - """ - default_config = SWHDefaultConfig() - if not default_config.config['checks']: - return - - if instance.status not in {DEPOSIT_STATUS_DEPOSITED, - DEPOSIT_STATUS_VERIFIED}: - return - - from django.urls import reverse - from swh.scheduler.utils import create_oneshot_task_dict - - args = [instance.collection.name, instance.id] - - # In the following, we are checking the instance.*task_id are not already - # populated because the `instance.save()` call will also trigger a call to - # that very function. - - if (instance.status == DEPOSIT_STATUS_DEPOSITED and - not instance.check_task_id): - # schedule deposit's checks - from swh.deposit.config import PRIVATE_CHECK_DEPOSIT - check_url = reverse(PRIVATE_CHECK_DEPOSIT, args=args) - task = create_oneshot_task_dict('check-deposit', - deposit_check_url=check_url) - check_task_id = schedule_task(default_config.scheduler, task) - instance.check_task_id = check_task_id - instance.save() - - elif (instance.status == DEPOSIT_STATUS_VERIFIED and - not instance.load_task_id): - - url = utils.origin_url_from(instance) - task = create_oneshot_task_dict( - 'load-deposit', - url=url, deposit_id=instance.id) - - load_task_id = schedule_task(default_config.scheduler, task) - instance.load_task_id = load_task_id - instance.save()