Skip to content

Commit

Permalink
feat: LEAP-1600: Add created_at for TaskLock (HumanSignal#6583)
Browse files Browse the repository at this point in the history
Co-authored-by: triklozoid <[email protected]>
  • Loading branch information
triklozoid and triklozoid authored Nov 4, 2024
1 parent 64c0794 commit dbddf4e
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
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-11-04 10:37

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'),
),
]
63 changes: 63 additions & 0 deletions label_studio/tasks/migrations/0052_auto_20241030_1757.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Generated by Django 4.2.16 on 2024-10-30 17:57

from django.db import migrations, models
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_20241030_1757'

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.AlterField(
model_name='tasklock',
name='created_at',
field=models.DateTimeField(auto_now_add=True, help_text='Creation time', null=True, verbose_name='created at'),
),
migrations.RunPython(forwards, backwards),
]

1 change: 1 addition & 0 deletions label_studio/tasks/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,7 @@ class TaskLock(models.Model):
on_delete=models.CASCADE,
help_text='User who locked this task',
)
created_at = models.DateTimeField(_('created at'), auto_now_add=True, help_text='Creation time', null=True)


class AnnotationDraft(models.Model):
Expand Down

0 comments on commit dbddf4e

Please sign in to comment.