mirror of
https://github.com/khoaliber/khoj.git
synced 2026-03-08 05:39:13 +00:00
Fix edit job API. Use user timezone, pass all reqd. params to automation
- Pass user and calling_url to the scheduled chat too when modifying params of automation - Update to use user timezone even when update job via API - Move timezone string to timezone object calculation into the schedule automation method
This commit is contained in:
@@ -34,7 +34,6 @@ from khoj.routers.helpers import (
|
|||||||
ApiUserRateLimiter,
|
ApiUserRateLimiter,
|
||||||
CommonQueryParams,
|
CommonQueryParams,
|
||||||
ConversationCommandRateLimiter,
|
ConversationCommandRateLimiter,
|
||||||
create_automation,
|
|
||||||
schedule_automation,
|
schedule_automation,
|
||||||
update_telemetry_state,
|
update_telemetry_state,
|
||||||
)
|
)
|
||||||
@@ -457,10 +456,8 @@ async def post_automation(
|
|||||||
|
|
||||||
# Schedule automation with query_to_run, timezone, subject directly provided by user
|
# Schedule automation with query_to_run, timezone, subject directly provided by user
|
||||||
try:
|
try:
|
||||||
# Get user timezone
|
|
||||||
user_timezone = pytz.timezone(timezone)
|
|
||||||
# Use the query to run as the scheduling request if the scheduling request is unset
|
# Use the query to run as the scheduling request if the scheduling request is unset
|
||||||
automation = await schedule_automation(query_to_run, subject, crontime, user_timezone, q, user, request.url)
|
automation = await schedule_automation(query_to_run, subject, crontime, timezone, q, user, request.url)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error creating automation {q} for {user.email}: {e}")
|
logger.error(f"Error creating automation {q} for {user.email}: {e}")
|
||||||
return Response(
|
return Response(
|
||||||
@@ -518,14 +515,26 @@ def edit_job(
|
|||||||
|
|
||||||
# Construct updated automation metadata
|
# Construct updated automation metadata
|
||||||
automation_metadata = json.loads(automation.name)
|
automation_metadata = json.loads(automation.name)
|
||||||
|
automation_metadata["scheduling_request"] = q
|
||||||
automation_metadata["query_to_run"] = query_to_run
|
automation_metadata["query_to_run"] = query_to_run
|
||||||
automation_metadata["subject"] = subject.strip()
|
automation_metadata["subject"] = subject.strip()
|
||||||
|
automation_metadata["crontime"] = crontime
|
||||||
|
|
||||||
# Modify automation with updated query, subject, crontime
|
# Modify automation with updated query, subject
|
||||||
automation.modify(kwargs={"query_to_run": query_to_run, "subject": subject}, name=json.dumps(automation_metadata))
|
automation.modify(
|
||||||
|
name=json.dumps(automation_metadata),
|
||||||
|
kwargs={
|
||||||
|
"query_to_run": query_to_run,
|
||||||
|
"subject": subject,
|
||||||
|
"scheduling_request": q,
|
||||||
|
"user": user,
|
||||||
|
"calling_url": request.url,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
# Reschedule automation if crontime updated
|
# Reschedule automation if crontime updated
|
||||||
trigger = CronTrigger.from_crontab(crontime)
|
user_timezone = pytz.timezone(timezone)
|
||||||
|
trigger = CronTrigger.from_crontab(crontime, user_timezone)
|
||||||
if automation.trigger != trigger:
|
if automation.trigger != trigger:
|
||||||
automation.reschedule(trigger=trigger)
|
automation.reschedule(trigger=trigger)
|
||||||
|
|
||||||
|
|||||||
@@ -919,9 +919,8 @@ def scheduled_chat(query_to_run: str, scheduling_request: str, subject: str, use
|
|||||||
|
|
||||||
|
|
||||||
async def create_automation(q: str, timezone: str, user: KhojUser, calling_url: URL, meta_log: dict = {}):
|
async def create_automation(q: str, timezone: str, user: KhojUser, calling_url: URL, meta_log: dict = {}):
|
||||||
user_timezone = pytz.timezone(timezone)
|
|
||||||
crontime, query_to_run, subject = await schedule_query(q, meta_log)
|
crontime, query_to_run, subject = await schedule_query(q, meta_log)
|
||||||
job = await schedule_automation(query_to_run, subject, crontime, user_timezone, q, user, calling_url)
|
job = await schedule_automation(query_to_run, subject, crontime, timezone, q, user, calling_url)
|
||||||
return job, crontime, query_to_run, subject
|
return job, crontime, query_to_run, subject
|
||||||
|
|
||||||
|
|
||||||
@@ -929,11 +928,12 @@ async def schedule_automation(
|
|||||||
query_to_run: str,
|
query_to_run: str,
|
||||||
subject: str,
|
subject: str,
|
||||||
crontime: str,
|
crontime: str,
|
||||||
user_timezone,
|
timezone: str,
|
||||||
scheduling_request: str,
|
scheduling_request: str,
|
||||||
user: KhojUser,
|
user: KhojUser,
|
||||||
calling_url: URL,
|
calling_url: URL,
|
||||||
):
|
):
|
||||||
|
user_timezone = pytz.timezone(timezone)
|
||||||
trigger = CronTrigger.from_crontab(crontime, user_timezone)
|
trigger = CronTrigger.from_crontab(crontime, user_timezone)
|
||||||
# Generate id and metadata used by task scheduler and process locks for the task runs
|
# Generate id and metadata used by task scheduler and process locks for the task runs
|
||||||
job_metadata = json.dumps(
|
job_metadata = json.dumps(
|
||||||
|
|||||||
Reference in New Issue
Block a user