mirror of
https://github.com/khoaliber/khoj.git
synced 2026-03-07 21:29:13 +00:00
Throw an error if trying to create a process lock that already exists. Names should be unique
This commit is contained in:
@@ -16,6 +16,7 @@ from django.contrib.sessions.backends.db import SessionStore
|
|||||||
from django.db import models
|
from django.db import models
|
||||||
from django.db.models import Q
|
from django.db.models import Q
|
||||||
from django.db.models.manager import BaseManager
|
from django.db.models.manager import BaseManager
|
||||||
|
from django.db.utils import IntegrityError
|
||||||
from django_apscheduler.models import DjangoJob, DjangoJobExecution
|
from django_apscheduler.models import DjangoJob, DjangoJobExecution
|
||||||
from fastapi import HTTPException
|
from fastapi import HTTPException
|
||||||
from pgvector.django import CosineDistance
|
from pgvector.django import CosineDistance
|
||||||
@@ -457,6 +458,9 @@ class ProcessLockAdapters:
|
|||||||
with timer(f"🔒 Run {func} with {operation} process lock", logger):
|
with timer(f"🔒 Run {func} with {operation} process lock", logger):
|
||||||
func(**kwargs)
|
func(**kwargs)
|
||||||
success = True
|
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:
|
except Exception as e:
|
||||||
logger.error(f"🚨 Error executing {func} with {operation} process lock: {e}", exc_info=True)
|
logger.error(f"🚨 Error executing {func} with {operation} process lock: {e}", exc_info=True)
|
||||||
success = False
|
success = False
|
||||||
|
|||||||
26
src/khoj/database/migrations/0041_alter_processlock_name.py
Normal file
26
src/khoj/database/migrations/0041_alter_processlock_name.py
Normal 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),
|
||||||
|
]
|
||||||
@@ -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.
|
# 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.
|
# 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)
|
started_at = models.DateTimeField(auto_now_add=True)
|
||||||
max_duration_in_seconds = models.IntegerField(default=60 * 60 * 12) # 12 hours
|
max_duration_in_seconds = models.IntegerField(default=60 * 60 * 12) # 12 hours
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user