diff --git a/src/khoj/database/adapters/__init__.py b/src/khoj/database/adapters/__init__.py index 5e3bfb88..fac2ca27 100644 --- a/src/khoj/database/adapters/__init__.py +++ b/src/khoj/database/adapters/__init__.py @@ -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 diff --git a/src/khoj/database/migrations/0041_alter_processlock_name.py b/src/khoj/database/migrations/0041_alter_processlock_name.py new file mode 100644 index 00000000..80d988d6 --- /dev/null +++ b/src/khoj/database/migrations/0041_alter_processlock_name.py @@ -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), + ] diff --git a/src/khoj/database/models/__init__.py b/src/khoj/database/models/__init__.py index ad35be01..e654903d 100644 --- a/src/khoj/database/models/__init__.py +++ b/src/khoj/database/models/__init__.py @@ -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