-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: LEAP-1602: prereq for updating existing locks
- Loading branch information
Showing
3 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters