Changeset View
Changeset View
Standalone View
Standalone View
swh/deposit/signals.py
Show All 15 Lines | |||||
from django.db.models.signals import post_save | from django.db.models.signals import post_save | ||||
from django.dispatch import receiver | from django.dispatch import receiver | ||||
from .models import Deposit | from .models import Deposit | ||||
from .config import SWHDefaultConfig, DEPOSIT_STATUS_VERIFIED | from .config import SWHDefaultConfig, DEPOSIT_STATUS_VERIFIED | ||||
from .config import DEPOSIT_STATUS_DEPOSITED | 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) | @receiver(post_save, sender=Deposit) | ||||
def post_deposit_save(sender, instance, created, raw, using, | def post_deposit_save(sender, instance, created, raw, using, | ||||
update_fields, **kwargs): | update_fields, **kwargs): | ||||
"""When a deposit is saved, check for the deposit's status change and | """When a deposit is saved, check for the deposit's status change and | ||||
schedule actions accordingly. | schedule actions accordingly. | ||||
When the status passes to deposited, schedule checks. | When the status passes to deposited, schedule checks. | ||||
When the status pass to ready, schedule loading. Otherwise, do | When the status pass to ready, schedule loading. Otherwise, do | ||||
Show All 20 Lines | def post_deposit_save(sender, instance, created, raw, using, | ||||
if instance.status not in {DEPOSIT_STATUS_DEPOSITED, | if instance.status not in {DEPOSIT_STATUS_DEPOSITED, | ||||
DEPOSIT_STATUS_VERIFIED}: | DEPOSIT_STATUS_VERIFIED}: | ||||
return | return | ||||
from django.urls import reverse | from django.urls import reverse | ||||
from swh.scheduler.utils import create_oneshot_task_dict | from swh.scheduler.utils import create_oneshot_task_dict | ||||
args = [instance.collection.name, instance.id] | args = [instance.collection.name, instance.id] | ||||
ardumont: This is because this function call will also be triggered by the `instance.save()` below... | |||||
Not Done Inline ActionsSo put this in a comment in the code instead of here (IMHO) douardda: So put this in a comment in the code instead of here (IMHO) | |||||
Done Inline ActionsRight, did not really know where to put it ;) ardumont: Right, did not really know where to put it ;) | |||||
if instance.status == DEPOSIT_STATUS_DEPOSITED: | # In the following, we are checking the instance.*task_id are not already | ||||
# schedule archive check | # 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 | from swh.deposit.config import PRIVATE_CHECK_DEPOSIT | ||||
check_url = reverse(PRIVATE_CHECK_DEPOSIT, args=args) | check_url = reverse(PRIVATE_CHECK_DEPOSIT, args=args) | ||||
task = create_oneshot_task_dict( | task = create_oneshot_task_dict( | ||||
'swh-deposit-archive-checks', | 'swh-deposit-archive-checks', | ||||
deposit_check_url=check_url) | deposit_check_url=check_url) | ||||
else: # instance.status == DEPOSIT_STATUS_VERIFIED: | check_task_id = schedule_task(default_config.scheduler, task) | ||||
# schedule loading | instance.check_task_id = check_task_id | ||||
instance.save() | |||||
elif (instance.status == DEPOSIT_STATUS_VERIFIED and | |||||
not instance.load_task_id): | |||||
# schedule deposit loading | |||||
from swh.deposit.config import PRIVATE_GET_RAW_CONTENT | from swh.deposit.config import PRIVATE_GET_RAW_CONTENT | ||||
from swh.deposit.config import PRIVATE_GET_DEPOSIT_METADATA | from swh.deposit.config import PRIVATE_GET_DEPOSIT_METADATA | ||||
from swh.deposit.config import PRIVATE_PUT_DEPOSIT | from swh.deposit.config import PRIVATE_PUT_DEPOSIT | ||||
archive_url = reverse(PRIVATE_GET_RAW_CONTENT, args=args) | archive_url = reverse(PRIVATE_GET_RAW_CONTENT, args=args) | ||||
meta_url = reverse(PRIVATE_GET_DEPOSIT_METADATA, args=args) | meta_url = reverse(PRIVATE_GET_DEPOSIT_METADATA, args=args) | ||||
update_url = reverse(PRIVATE_PUT_DEPOSIT, args=args) | update_url = reverse(PRIVATE_PUT_DEPOSIT, args=args) | ||||
task = create_oneshot_task_dict( | task = create_oneshot_task_dict( | ||||
'swh-deposit-archive-loading', | 'swh-deposit-archive-loading', | ||||
archive_url=archive_url, | archive_url=archive_url, | ||||
deposit_meta_url=meta_url, | deposit_meta_url=meta_url, | ||||
deposit_update_url=update_url) | deposit_update_url=update_url) | ||||
default_config.scheduler.create_tasks([task]) | load_task_id = schedule_task(default_config.scheduler, task) | ||||
instance.load_task_id = load_task_id | |||||
instance.save() |
This is because this function call will also be triggered by the instance.save() below...