Skip to content

Commit

Permalink
feat: LEAP-1600: Add created_at for TaskLock
Browse files Browse the repository at this point in the history
  • Loading branch information
triklozoid committed Oct 30, 2024
1 parent a7c38ed commit 2cf0533
Show file tree
Hide file tree
Showing 3 changed files with 71 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-10-30 17:56

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(auto_now_add=True, help_text='Creation time', null=True, verbose_name='created at'),
),
]
52 changes: 52 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,52 @@
# Generated by Django 4.2.16 on 2024-10-30 17:57

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_20241030_1757'

# SQL query to update tasks_tasklock.created_at
sql_update_created_at = """
UPDATE tasks_tasklock
SET created_at = expire_at - interval '%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}')

# Get db cursor
cursor = connection.cursor()
cursor.execute(sql_update_created_at)

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),
]

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 2cf0533

Please sign in to comment.