Throw an error if trying to create a process lock that already exists. Names should be unique

This commit is contained in:
sabaimran
2024-05-04 19:03:53 +05:30
parent 7100614de5
commit 509a8a412c
3 changed files with 31 additions and 1 deletions

View File

@@ -16,6 +16,7 @@ from django.contrib.sessions.backends.db import SessionStore
from django.db import models
from django.db.models import Q
from django.db.models.manager import BaseManager
from django.db.utils import IntegrityError
from django_apscheduler.models import DjangoJob, DjangoJobExecution
from fastapi import HTTPException
from pgvector.django import CosineDistance
@@ -457,6 +458,9 @@ class ProcessLockAdapters:
with timer(f"🔒 Run {func} with {operation} process lock", logger):
func(**kwargs)
success = True
except IntegrityError as e:
logger.error(f"⚠️ Unable to create the process lock for {func} with {operation}: {e}", exc_info=True)
success = False
except Exception as e:
logger.error(f"🚨 Error executing {func} with {operation} process lock: {e}", exc_info=True)
success = False

View File

@@ -0,0 +1,26 @@
# Generated by Django 4.2.10 on 2024-05-04 13:12
from django.db import migrations, models
class Migration(migrations.Migration):
def delete_all_existing_process_locks(apps, schema_editor):
ProcessLock = apps.get_model("database", "ProcessLock")
ProcessLock.objects.all().delete()
dependencies = [
("database", "0040_merge_20240504_1010"),
]
operations = [
migrations.AlterField(
model_name="processlock",
name="name",
field=models.CharField(
choices=[("index_content", "Index Content"), ("scheduled_job", "Scheduled Job")],
max_length=200,
unique=True,
),
),
migrations.RunPython(delete_all_existing_process_locks),
]

View File

@@ -114,7 +114,7 @@ class ProcessLock(BaseModel):
# We need to make sure that some operations are thread-safe. To do so, add locks for potentially shared operations.
# For example, we need to make sure that only one process is updating the embeddings at a time.
name = models.CharField(max_length=200, choices=Operation.choices)
name = models.CharField(max_length=200, choices=Operation.choices, unique=True)
started_at = models.DateTimeField(auto_now_add=True)
max_duration_in_seconds = models.IntegerField(default=60 * 60 * 12) # 12 hours