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:
Debanjum Singh Solanky 2024-05-01 09:32:15 +05:30
parent 19c5af3ebc
commit 89a8dbb81a
2 changed files with 19 additions and 10 deletions

View file

@ -34,7 +34,6 @@ from khoj.routers.helpers import (
ApiUserRateLimiter,
CommonQueryParams,
ConversationCommandRateLimiter,
create_automation,
schedule_automation,
update_telemetry_state,
)
@ -457,10 +456,8 @@ async def post_automation(
# Schedule automation with query_to_run, timezone, subject directly provided by user
try:
# Get user timezone
user_timezone = pytz.timezone(timezone)
# 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:
logger.error(f"Error creating automation {q} for {user.email}: {e}")
return Response(
@ -518,14 +515,26 @@ def edit_job(
# Construct updated automation metadata
automation_metadata = json.loads(automation.name)
automation_metadata["scheduling_request"] = q
automation_metadata["query_to_run"] = query_to_run
automation_metadata["subject"] = subject.strip()
automation_metadata["crontime"] = crontime
# Modify automation with updated query, subject, crontime
automation.modify(kwargs={"query_to_run": query_to_run, "subject": subject}, name=json.dumps(automation_metadata))
# Modify automation with updated query, subject
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
trigger = CronTrigger.from_crontab(crontime)
user_timezone = pytz.timezone(timezone)
trigger = CronTrigger.from_crontab(crontime, user_timezone)
if automation.trigger != trigger:
automation.reschedule(trigger=trigger)

View file

@ -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 = {}):
user_timezone = pytz.timezone(timezone)
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
@ -929,11 +928,12 @@ async def schedule_automation(
query_to_run: str,
subject: str,
crontime: str,
user_timezone,
timezone: str,
scheduling_request: str,
user: KhojUser,
calling_url: URL,
):
user_timezone = pytz.timezone(timezone)
trigger = CronTrigger.from_crontab(crontime, user_timezone)
# Generate id and metadata used by task scheduler and process locks for the task runs
job_metadata = json.dumps(