Use hash of query in process lock id to standardize id format

- Using inferred_query directly was brittle (like previous job id)
- And process lock id had a limited size, so wouldn't work for larger
  inferred query strings
This commit is contained in:
Debanjum Singh Solanky
2024-04-27 01:55:08 +05:30
parent 3ce06a938c
commit 2c563ad280
2 changed files with 9 additions and 4 deletions

View File

@@ -407,6 +407,7 @@ async def websocket_endpoint(
continue
# Generate the job id from the hash of inferred_query and crontime
job_id = hashlib.md5(f"{inferred_query}_{crontime}".encode("utf-8")).hexdigest()
query_id = hashlib.md5(f"{inferred_query}".encode("utf-8")).hexdigest()
partial_scheduled_chat = functools.partial(
scheduled_chat, inferred_query, q, websocket.user.object, websocket.url
)
@@ -416,7 +417,7 @@ async def websocket_endpoint(
trigger=trigger,
args=(
partial_scheduled_chat,
f"{ProcessLock.Operation.SCHEDULED_JOB}_{user.uuid}_{inferred_query}",
f"{ProcessLock.Operation.SCHEDULED_JOB}_{user.uuid}_{query_id}",
),
id=f"job_{user.uuid}_{job_id}",
name=f"{inferred_query}",
@@ -682,12 +683,13 @@ async def chat(
# Generate the job id from the hash of inferred_query and crontime
job_id = hashlib.md5(f"{inferred_query}_{crontime}".encode("utf-8")).hexdigest()
query_id = hashlib.md5(f"{inferred_query}".encode("utf-8")).hexdigest()
partial_scheduled_chat = functools.partial(scheduled_chat, inferred_query, q, request.user.object, request.url)
try:
job = state.scheduler.add_job(
run_with_process_lock,
trigger=trigger,
args=(partial_scheduled_chat, f"{ProcessLock.Operation.SCHEDULED_JOB}_{user.uuid}_{inferred_query}"),
args=(partial_scheduled_chat, f"{ProcessLock.Operation.SCHEDULED_JOB}_{user.uuid}_{query_id}"),
id=f"job_{user.uuid}_{job_id}",
name=f"{inferred_query}",
max_instances=2, # Allow second instance to kill any previous instance with stale lock

View File

@@ -60,11 +60,14 @@ def send_task_email(name, email, query, result):
html_result = markdown_it.MarkdownIt().render(result)
html_content = template.render(name=name, query=query, result=html_result)
resend.Emails.send(
query_for_subject_line = query.replace("\n", " ").replace('"', "").replace("'", "")
r = resend.Emails.send(
{
"from": "Khoj <khoj@khoj.dev>",
"to": email,
"subject": f'✨ Your Task Results for "{query}"',
"subject": f'✨ Your Task Results for "{query_for_subject_line}"',
"html": html_content,
}
)
return r