Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: LEAP-1602: prereq for updating existing locks #6590

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions label_studio/tasks/migrations/0051_tasklock_created_at.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.16 on 2024-10-31 23:50

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('tasks', '0050_alter_predictionmeta_failed_prediction_and_more'),
]

operations = [
migrations.AddField(
model_name='tasklock',
name='created_at',
field=models.DateTimeField(blank=True, default=None, help_text='Creation time', null=True, verbose_name='created_at'),
),
]
55 changes: 55 additions & 0 deletions label_studio/tasks/migrations/0052_auto_20241101_1941.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from django.db import migrations
from django.db import connection
from django.conf import settings

from core.models import AsyncMigrationStatus
from core.redis import start_job_async_or_sync

import logging
logger = logging.getLogger(__name__)
migration_name = '0052_auto_20241101_1941'

if connection.vendor == 'sqlite':
sql_update_created_at = """
UPDATE tasks_tasklock
SET created_at = datetime(expire_at, %s);
"""
sql_params = (f'-{settings.TASK_LOCK_TTL} seconds',)
else:
sql_update_created_at = """
UPDATE tasks_tasklock
SET created_at = expire_at - INTERVAL %s;
"""
sql_params = ('%s seconds' % settings.TASK_LOCK_TTL,)

def forward_migration(migration_name):
migration = AsyncMigrationStatus.objects.create(
name=migration_name,
status=AsyncMigrationStatus.STATUS_STARTED,
)
logger.info(f'Start async migration {migration_name}')

with connection.cursor() as cursor:
cursor.execute(sql_update_created_at, sql_params)

migration.status = AsyncMigrationStatus.STATUS_FINISHED
migration.save()
logger.info(f'Async migration {migration_name} complete')

def forwards(apps, schema_editor):
# Dispatch migrations to rqworkers
start_job_async_or_sync(forward_migration, migration_name=migration_name)

def backwards(apps, schema_editor):
pass

class Migration(migrations.Migration):
atomic = False

dependencies = [
('tasks', '0051_tasklock_created_at'),
]

operations = [
migrations.RunPython(forwards, backwards),
]
11 changes: 11 additions & 0 deletions label_studio/tasks/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,8 @@
related_name='locks',
help_text='Locked task',
)
created_at = models.DateTimeField(_('created_at'), null=True, default=None, blank=True, help_text='Creation time')

expire_at = models.DateTimeField(_('expire_at'))
unique_id = models.UUIDField(default=uuid.uuid4, null=True, blank=True, unique=True, editable=False)
user = models.ForeignKey(
Expand All @@ -799,6 +801,15 @@
help_text='User who locked this task',
)

def save(self, *args, update_fields=None, **kwargs):
if update_fields is not None:
update_fields = {'created_at'}.union(update_fields)

Check warning on line 806 in label_studio/tasks/models.py

View check run for this annotation

Codecov / codecov/patch

label_studio/tasks/models.py#L806

Added line #L806 was not covered by tests

if not self.pk:
self.created_at = now()

return super().save(*args, update_fields=update_fields, **kwargs)


class AnnotationDraft(models.Model):
result = JSONField(_('result'), help_text='Draft result in JSON format')
Expand Down
Loading